Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding configuration variable replacement in c_cpp_properties.json. #1529

Merged
merged 3 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 20 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,24 @@ 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"; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

varType = "env"; } [](start = 37, length = 18)

I believe our linting rules require the body of the if to be on a new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran TSLint and got "vscode-tslint: Status is OK" (It did find some other lint issues though).
Happy to move it to new line, though!

let newValue: string = undefined;
switch (varType) {
case "env": { newValue = process.env[name]; break; }
case "config": {
let config = 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