Skip to content

Commit

Permalink
Make sure rust-analyser uses rust-project.json Meson generates
Browse files Browse the repository at this point in the history
This is very similar case than compile_commands.json, Meson generates a
file in builddir that some extensions should be using. It makes static
analysers work out of the box instead of requiring user to configure
paths themself.
  • Loading branch information
xclaesse committed Nov 28, 2023
1 parent a6d44ce commit be4ca4f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
extensionConfiguration,
extensionConfigurationSet,
genEnvFile,
useCompileCommands,
clearCache,
checkMesonIsConfigured,
getOutputChannel,
getBuildDirectory,
rootMesonFiles,
watchFile,
} from "./utils";
import { DebugConfigurationProviderCppdbg } from "./debug/cppdbg";
import { DebugConfigurationProviderLldb } from "./debug/lldb";
Expand All @@ -26,7 +26,6 @@ export let extensionPath: string;
export let workspaceState: vscode.Memento;
let explorer: MesonProjectExplorer;
let watcher: vscode.FileSystemWatcher;
let compileCommandsWatcher: vscode.FileSystemWatcher;
let mesonWatcher: vscode.FileSystemWatcher;
let controller: vscode.TestController;

Expand Down Expand Up @@ -137,19 +136,25 @@ export async function activate(ctx: vscode.ExtensionContext) {
}),
);

const compileCommandsHandler = async () => {
await useCompileCommands(buildDir);
};
compileCommandsWatcher = vscode.workspace.createFileSystemWatcher(
`${buildDir}/compile_commands.json`,
false,
false,
true,
);
compileCommandsWatcher.onDidChange(compileCommandsHandler);
compileCommandsWatcher.onDidCreate(compileCommandsHandler);
ctx.subscriptions.push(compileCommandsWatcher);
await useCompileCommands(buildDir);
const compileCommandsFile = `${buildDir}/compile_commands.json`;
watchFile(ctx, compileCommandsFile, async () => {
try {
const conf = vscode.workspace.getConfiguration("C_Cpp");
conf.update("default.compileCommands", compileCommandsFile, vscode.ConfigurationTarget.Workspace);
} catch {
// Ignore, C/C++ extension might not be installed
}
});

const rustProjectFile = `${buildDir}/rust-project.json`;
watchFile(ctx, rustProjectFile, async () => {
try {
const conf = vscode.workspace.getConfiguration("rust-analyzer");
conf.update("linkedProjects", [rustProjectFile], vscode.ConfigurationTarget.Workspace);
} catch {
// Ignore, rust-analyzer extension might not be installed
}
});

ctx.subscriptions.push(
vscode.commands.registerCommand("mesonbuild.openBuildFile", async (node: TargetNode) => {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,13 @@ export async function rootMesonFiles(): Promise<vscode.Uri[]> {

return rootFiles;
}

export function watchFile(ctx: vscode.ExtensionContext, file: string, listener: () => void) {
const watcher = vscode.workspace.createFileSystemWatcher(file, false, false, true);
watcher.onDidChange(listener);
watcher.onDidCreate(listener);
ctx.subscriptions.push(watcher);
if (fs.existsSync(file)) {
listener();
}
}

0 comments on commit be4ca4f

Please sign in to comment.