Skip to content

Commit eedadd9

Browse files
bugengineyngwe@farnsworthyngwe@bender
authored
Added system to store and query properties from the active C/C++ configuration (#5453)
* Added system to store and query properties from the active C/C++ configuration Co-authored-by: yngwe@farnsworth <yngwe@farnsworth> Co-authored-by: yngwe@bender <yngwe@bender>
1 parent b331179 commit eedadd9

7 files changed

Lines changed: 72 additions & 0 deletions

File tree

Extension/c_cpp_properties.schema.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@
142142
}
143143
},
144144
"additionalProperties": false
145+
},
146+
"customConfigurationVariables": {
147+
"type": "object",
148+
"description": "Custom variables that can be queried through the command ${cpptools:activeConfigCustomVariable} to use for the input variables in launch.json.",
149+
"patternProperties": {
150+
"(^.+$)": {
151+
"type": "string"
152+
}
153+
},
154+
"additionalProperties": false
145155
}
146156
},
147157
"additionalProperties": false

Extension/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,20 @@
470470
"description": "%c_cpp.configuration.default.systemIncludePath.description%",
471471
"scope": "machine-overridable"
472472
},
473+
"C_Cpp.default.customConfigurationVariables": {
474+
"type": [
475+
"object",
476+
"null"
477+
],
478+
"default": null,
479+
"patternProperties": {
480+
"(^.+$)": {
481+
"type": "string"
482+
}
483+
},
484+
"description": "%c_cpp.configuration.default.customConfigurationVariables.description%",
485+
"scope": "machine-overridable"
486+
},
473487
"C_Cpp.default.enableConfigurationSquiggles": {
474488
"type": "boolean",
475489
"default": true,

Extension/package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"c_cpp.configuration.default.browse.limitSymbolsToIncludedHeaders.description": "The value to use in a configuration if \"browse.limitSymbolsToIncludedHeaders\" is either not specified or set to \"${default}\".",
6161
"c_cpp.configuration.default.systemIncludePath.description": "The value to use for the system include path. If set, it overrides the system include path acquired via \"compilerPath\" and \"compileCommands\" settings.",
6262
"c_cpp.configuration.default.enableConfigurationSquiggles.description": "Controls whether the extension will report errors detected in c_cpp_properties.json.",
63+
"c_cpp.configuration.default.customConfigurationVariables.description": "The value to use in a configuration if \"customConfigurationVariables\" is not set, or the values to insert if \"${default}\" is present as a key in \"customConfigurationVariables\".",
6364
"c_cpp.configuration.updateChannel.description": "Set to \"Insiders\" to automatically download and install the latest Insiders builds of the extension, which include upcoming features and bug fixes.",
6465
"c_cpp.configuration.experimentalFeatures.description": "Controls whether \"experimental\" features are usable.",
6566
"c_cpp.configuration.suggestSnippets.description": "If true, snippets are provided by the language server.",

Extension/src/LanguageServer/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ export interface Client {
500500
toggleReferenceResultsView(): void;
501501
setCurrentConfigName(configurationName: string): Thenable<void>;
502502
getCurrentConfigName(): Thenable<string | undefined>;
503+
getCurrentConfigCustomVariable(variableName: string): Thenable<string>;
503504
getVcpkgInstalled(): Thenable<boolean>;
504505
getVcpkgEnabled(): Thenable<boolean>;
505506
getCurrentCompilerPathAndArgs(): Thenable<util.CompilerPathAndArgs | undefined>;
@@ -1778,6 +1779,10 @@ export class DefaultClient implements Client {
17781779
return this.queueTask(() => Promise.resolve(this.configuration.CurrentConfiguration?.name));
17791780
}
17801781

1782+
public getCurrentConfigCustomVariable(variableName: string): Thenable<string> {
1783+
return this.queueTask(() => Promise.resolve(this.configuration.CurrentConfiguration?.customConfigurationVariables?.[variableName] || ''));
1784+
}
1785+
17811786
public setCurrentConfigName(configurationName: string): Thenable<void> {
17821787
return this.queueTask(() => new Promise((resolve, reject) => {
17831788
let configurations: configs.Configuration[] = this.configuration.Configurations || [];
@@ -2658,6 +2663,7 @@ class NullClient implements Client {
26582663
toggleReferenceResultsView(): void {}
26592664
setCurrentConfigName(configurationName: string): Thenable<void> { return Promise.resolve(); }
26602665
getCurrentConfigName(): Thenable<string> { return Promise.resolve(""); }
2666+
getCurrentConfigCustomVariable(variableName: string): Thenable<string> { return Promise.resolve(""); }
26612667
getVcpkgInstalled(): Thenable<boolean> { return Promise.resolve(false); }
26622668
getVcpkgEnabled(): Thenable<boolean> { return Promise.resolve(false); }
26632669
getCurrentCompilerPathAndArgs(): Thenable<util.CompilerPathAndArgs | undefined> { return Promise.resolve(undefined); }

Extension/src/LanguageServer/configurations.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface Configuration {
6767
forcedInclude?: string[];
6868
configurationProvider?: string;
6969
browse?: Browse;
70+
customConfigurationVariables?: {[key: string]: string};
7071
}
7172

7273
export interface ConfigurationErrors {
@@ -123,6 +124,7 @@ export class CppProperties {
123124
private vcpkgIncludes: string[] = [];
124125
private vcpkgPathReady: boolean = false;
125126
private defaultIntelliSenseMode?: string;
127+
private defaultCustomConfigurationVariables?: { [key: string]: string };
126128
private readonly configurationGlobPattern: string = "c_cpp_properties.json";
127129
private disposables: vscode.Disposable[] = [];
128130
private configurationsChanged = new vscode.EventEmitter<Configuration[]>();
@@ -328,6 +330,9 @@ export class CppProperties {
328330
if (isUnset(settings.defaultIntelliSenseMode) || settings.defaultIntelliSenseMode === "") {
329331
configuration.intelliSenseMode = this.defaultIntelliSenseMode;
330332
}
333+
if (isUnset(settings.defaultCustomConfigurationVariables) || settings.defaultCustomConfigurationVariables === {}) {
334+
configuration.customConfigurationVariables = this.defaultCustomConfigurationVariables;
335+
}
331336
}
332337

333338
private get ExtendedEnvironment(): Environment {
@@ -527,6 +532,25 @@ export class CppProperties {
527532
return result;
528533
}
529534

535+
private resolveDefaultsDictionary(entries: { [key: string] : string }, defaultValue: { [key: string] : string } | undefined, env: Environment): { [key: string] : string } {
536+
let result: { [key: string] : string } = {};
537+
for (const property in entries) {
538+
if (property === "${default}") {
539+
if (defaultValue) {
540+
for (const defaultProperty in defaultValue) {
541+
if (!(defaultProperty in entries))
542+
{
543+
result[defaultProperty] = util.resolveVariables(defaultValue[defaultProperty], env);
544+
}
545+
}
546+
}
547+
} else {
548+
result[property] = util.resolveVariables(entries[property], env);
549+
}
550+
}
551+
return result;
552+
}
553+
530554
private resolveAndSplit(paths: string[] | undefined, defaultValue: string[] | undefined, env: Environment): string[] {
531555
let result: string[] = [];
532556
if (paths) {
@@ -572,6 +596,16 @@ export class CppProperties {
572596
return util.resolveVariables(property, env);
573597
}
574598

599+
private updateConfigurationStringDictionary(property: { [key: string]: string } | undefined, defaultValue: { [key: string]: string } | undefined, env: Environment): { [key: string]: string } | undefined {
600+
if (!property || property === {}) {
601+
property = defaultValue;
602+
}
603+
if (!property || property === {}) {
604+
return undefined;
605+
}
606+
return this.resolveDefaultsDictionary(property, defaultValue, env);
607+
}
608+
575609
private updateServerOnFolderSettingsChange(): void {
576610
if (!this.configurationJson) {
577611
return;
@@ -592,6 +626,7 @@ export class CppProperties {
592626
configuration.cStandard = this.updateConfigurationString(configuration.cStandard, settings.defaultCStandard, env);
593627
configuration.cppStandard = this.updateConfigurationString(configuration.cppStandard, settings.defaultCppStandard, env);
594628
configuration.intelliSenseMode = this.updateConfigurationString(configuration.intelliSenseMode, settings.defaultIntelliSenseMode, env);
629+
configuration.customConfigurationVariables = this.updateConfigurationStringDictionary(configuration.customConfigurationVariables, settings.defaultCustomConfigurationVariables, env);
595630
configuration.configurationProvider = this.updateConfigurationString(configuration.configurationProvider, settings.defaultConfigurationProvider, env);
596631

597632
if (!configuration.browse) {

Extension/src/LanguageServer/extension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ export function registerCommands(): void {
905905
disposables.push(vscode.commands.registerCommand('C_Cpp.VcpkgClipboardInstallSuggested', onVcpkgClipboardInstallSuggested));
906906
disposables.push(vscode.commands.registerCommand('C_Cpp.VcpkgOnlineHelpSuggested', onVcpkgOnlineHelpSuggested));
907907
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigName', onGetActiveConfigName));
908+
disposables.push(vscode.commands.registerCommand('cpptools.activeConfigCustomVariable', onGetActiveConfigCustomVariable));
908909
disposables.push(vscode.commands.registerCommand('cpptools.setActiveConfigName', onSetActiveConfigName));
909910
getTemporaryCommandRegistrarInstance().executeDelayedCommands();
910911
}
@@ -1167,6 +1168,10 @@ function onGetActiveConfigName(): Thenable<string | undefined> {
11671168
return clients.ActiveClient.getCurrentConfigName();
11681169
}
11691170

1171+
function onGetActiveConfigCustomVariable(variableName: string): Thenable<string> {
1172+
return clients.ActiveClient.getCurrentConfigCustomVariable(variableName);
1173+
}
1174+
11701175
function onLogDiagnostics(): void {
11711176
onActivationEvent();
11721177
clients.ActiveClient.logDiagnostics();

Extension/src/LanguageServer/settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export class CppSettings extends Settings {
155155
public get defaultLimitSymbolsToIncludedHeaders(): boolean | undefined { return super.Section.get<boolean>("default.browse.limitSymbolsToIncludedHeaders"); }
156156
public get defaultSystemIncludePath(): string[] | undefined { return super.Section.get<string[]>("default.systemIncludePath"); }
157157
public get defaultEnableConfigurationSquiggles(): boolean | undefined { return super.Section.get<boolean>("default.enableConfigurationSquiggles"); }
158+
public get defaultCustomConfigurationVariables(): { [key: string]: string } | undefined { return super.Section.get< { [key: string]: string } >("default.customConfigurationVariables"); }
158159
public get useBacktickCommandSubstitution(): boolean | undefined { return super.Section.get<boolean>("debugger.useBacktickCommandSubstitution"); }
159160
public get codeFolding(): boolean { return super.Section.get<string>("codeFolding") === "Enabled"; }
160161

0 commit comments

Comments
 (0)