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.

We only need to watch for file creation, it was previously wrongly
watching for file changes for legacy reasons (we used to patch
compile_commands.json).
  • Loading branch information
xclaesse authored and tristan957 committed Nov 28, 2023
1 parent 3946f91 commit 28319fa
Show file tree
Hide file tree
Showing 2 changed files with 29 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,
whenFileExists,
} 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`;
whenFileExists(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`;
whenFileExists(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
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,12 @@ export async function rootMesonFiles(): Promise<vscode.Uri[]> {

return rootFiles;
}

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

0 comments on commit 28319fa

Please sign in to comment.