Skip to content

Commit

Permalink
Adding configuration variable replacement in c_cpp_properties.json. (#…
Browse files Browse the repository at this point in the history
…1529)

* Adding configuration variable replacement in c_cpp_properties.json. #314

* Fixing issues brought up by linter
  • Loading branch information
thejcannon authored and bobbrow committed Feb 7, 2018
1 parent e6d8d1f commit d5af9a1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions Extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fix duplicate `cannot open source file` errors. [#1469](https://github.com/Microsoft/vscode-cpptools/issues/1469)
* Fix error popup appearing with non-workspace files when using `compile_commands.json`. [#1475](https://github.com/Microsoft/vscode-cpptools/issues/1475)
* Add more macros to `cpp.hint` (fixing missing symbols).
* Added support for config variables to `c_cpp_properties.json` [#314](https://github.com/Microsoft/vscode-cpptools/issues/314)

## Version 0.14.6: January 17, 2018
* Fix tag parser failing (and continuing to fail after edits) when it shouldn't. [#1367](https://github.com/Microsoft/vscode-cpptools/issues/1367)
Expand Down
27 changes: 22 additions & 5 deletions Extension/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import HttpsProxyAgent = require('https-proxy-agent');
import * as url from 'url';
import { PlatformInformation } from './platform';
import { getOutputChannelLogger, showOutputChannel } from './logger';
import * as assert from 'assert';

export let extensionContext: vscode.ExtensionContext;
export function setExtensionContext(context: vscode.ExtensionContext): void {
Expand Down Expand Up @@ -45,7 +46,7 @@ export const extensionNotReadyString: string = 'The C/C++ extension is still ins
export function displayExtensionNotReadyPrompt(): void {

if (!isExtensionNotReadyPromptDisplayed) {
isExtensionNotReadyPromptDisplayed = true;
isExtensionNotReadyPromptDisplayed = true;
showOutputChannel();

getOutputChannelLogger().showInformationMessage(extensionNotReadyString).then(
Expand Down Expand Up @@ -123,10 +124,26 @@ export function resolveVariables(input: string): string {
return "";
}

// Replace environment variables. (support both ${env:VAR} and ${VAR} syntax)
let regexp: RegExp = /\$\{(env:|env.)?(.*?)\}/g;
let ret: string = input.replace(regexp, (match: string, ignored: string, name: string) => {
let newValue: string = process.env[name];
// Replace environment and configuration variables.
let regexp: RegExp = /\$\{((env|config)(.|:))?(.*?)\}/g;
let ret: string = input.replace(regexp, (match: string, ignored1: string, varType: string, ignored2: string, name: string) => {
// Historically, if the variable didn't have anything before the "." or ":"
// it was assumed to be an environment variable
if (varType === undefined) {
varType = "env";
}
let newValue: string = undefined;
switch (varType) {
case "env": { newValue = process.env[name]; break; }
case "config": {
let config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration();
let keys: string[] = name.split('.');
keys.forEach((key: string) => { config = (config) ? config.get(key) : config; });
newValue = (config) ? config.toString() : undefined;
break;
}
default: { assert.fail("unknown varType matched"); }
}
return (newValue) ? newValue : match;
});

Expand Down

0 comments on commit d5af9a1

Please sign in to comment.