Skip to content

Commit

Permalink
Merge setupCppTools and setupRustAnalyzer into a single setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xclaesse committed Dec 6, 2023
1 parent 26990c9 commit cdcdd91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,13 @@
],
"description": "Have VSCode download the language server automatically (MacOS/Windows only for Swift-MesonLSP)"
},
"mesonbuild.setupCppTools": {
"type": "boolean",
"default": true,
"markdownDescription": "Setup the [Microsoft C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) to work with Meson"
},
"mesonbuild.setupRustAnalyzer": {
"type": "boolean",
"mesonbuild.modifySettings": {
"type": [
"boolean",
"array"
],
"default": true,
"markdownDescription": "Setup the [rust-analyzer extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) to work with Meson"
"markdownDescription": "Automatically setup other extensions to use files generated by Meson. Disable this if for example .vscode/settings.json is committed into git and should not be modified. Can also be an array of settings that can be modified, currently [cppTools](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) or [rustAnalyser](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)."
},
"mesonbuild.Swift-MesonLSP.others.ignoreDiagnosticsFromSubprojects": {
"type": [
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getBuildDirectory,
whenFileExists,
mesonRootDirs,
modifySettings,
} from "./utils";
import { DebugConfigurationProviderCppdbg } from "./debug/cppdbg";
import { DebugConfigurationProviderLldb } from "./debug/lldb";
Expand Down Expand Up @@ -134,15 +135,15 @@ export async function activate(ctx: vscode.ExtensionContext) {

const compileCommandsFile = `${buildDir}/compile_commands.json`;
whenFileExists(ctx, compileCommandsFile, async () => {
if (extensionConfiguration("setupCppTools")) {
if (modifySettings("cppTools")) {
const conf = vscode.workspace.getConfiguration("C_Cpp");
conf.update("default.compileCommands", compileCommandsFile, vscode.ConfigurationTarget.Workspace);
}
});

const rustProjectFile = `${buildDir}/rust-project.json`;
whenFileExists(ctx, rustProjectFile, async () => {
if (extensionConfiguration("setupRustAnalyzer")) {
if (modifySettings("rustAnalyzer")) {
const conf = vscode.workspace.getConfiguration("rust-analyzer");
conf.update("linkedProjects", [rustProjectFile], vscode.ConfigurationTarget.Workspace);
}
Expand Down
6 changes: 2 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ export interface ExtensionConfiguration {
languageServerPath: string;
downloadLanguageServer: boolean | "ask";
selectRootDir: boolean;
setupCppTools: boolean;
setupRustAnalyzer: boolean;
modifySettings: boolean;
}

export interface TaskQuickPickItem extends vscode.QuickPickItem {
Expand Down Expand Up @@ -133,6 +132,5 @@ export enum SettingsKey {
languageServer = "languageServer",
configureOnOpen = "configureOnOpen",
selectRootDir = "selectRootDir",
setupCppTools = "setupCppTools",
setupRustAnalyzer = "setupRustAnalyzer",
modifySettings = "modifySettings",
}
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as vscode from "vscode";
import * as which from "which";

import { createHash, BinaryLike } from "crypto";
import { ExtensionConfiguration, Target } from "./types";
import { ExtensionConfiguration, Target, SettingsKey } from "./types";
import { getMesonBuildOptions } from "./introspection";
import { extensionPath, workspaceState } from "./extension";

Expand Down Expand Up @@ -123,6 +123,13 @@ export function extensionConfigurationSet<K extends keyof ExtensionConfiguration
return getConfiguration().update(key, value, target);
}

export function modifySettings(key: string) {
const modifySettings = extensionConfiguration(SettingsKey.modifySettings);
if (typeof modifySettings == "boolean") return modifySettings;
if (arrayIncludes(modifySettings, key)) return true;
return false;
}

export function arrayIncludes<T>(array: T[], value: T) {
return array.indexOf(value) !== -1;
}
Expand Down

0 comments on commit cdcdd91

Please sign in to comment.