Skip to content

Improve variable detection #10

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick a Node version: 10, 12, 14
"args": {
"args": {
"VARIANT": "14"
}
},

// Set *default* container specific settings.json values on container create.
"settings": {
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},

Expand All @@ -29,4 +29,4 @@

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "node"
}
}
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
}
"typescript.tsc.autoDetect": "off",
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true
}
2 changes: 1 addition & 1 deletion .yarnrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--ignore-engines true
--ignore-engines true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
"vscode-test": "^1.5.0",
"vsce": "^1.87.1"
}
}
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerInlineValuesProvider('powershell', new PowerShellVariableInlineValuesProvider()));
}

export function deactivate() {}
export function deactivate() { }
39 changes: 26 additions & 13 deletions src/powerShellVariableInlineValuesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ import * as vscode from 'vscode';

export class PowerShellVariableInlineValuesProvider implements vscode.InlineValuesProvider {

provideInlineValues(document: vscode.TextDocument, viewport: vscode.Range, context: vscode.InlineValueContext) : vscode.ProviderResult<vscode.InlineValue[]> {
// Known constants
private readonly knownConstants = /^\$(?:true|false|null)$/i;

// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#scope-modifiers
private readonly supportedScopes = /^(?:global|local|script|private|using|variable)$/i;

// Variable patterns
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-5.1#variable-names-that-include-special-characters
private readonly alphanumChars = /(?:\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nd}|[_?])/.source;
private readonly variableRegex = new RegExp([
'(?:\\$\\{(?<specialName>.*?)(?<!`)\\})', // Special characters variables. Lazy match until unescaped }
`(?:\\$\\w+:${this.alphanumChars}+)`, // Scoped variables
`(?:\\$${this.alphanumChars}+)`, // Normal variables
].join('|'), 'giu'); // u flag to support unicode char classes

provideInlineValues(document: vscode.TextDocument, viewport: vscode.Range, context: vscode.InlineValueContext): vscode.ProviderResult<vscode.InlineValue[]> {
const allValues: vscode.InlineValue[] = [];

const ignoredVariables = /^\$(?:true|false|null)$/i;

for (let l = 0; l <= context.stoppedLocation.end.line; l++) {
const line = document.lineAt(l);

Expand All @@ -15,26 +28,26 @@ export class PowerShellVariableInlineValuesProvider implements vscode.InlineValu
continue;
}

const variableMatches = /(?:\${(.*)})|(?:\$\S+:\S+)|(?:\$\S+)/gi;
for (let match = variableMatches.exec(line.text); match; match = variableMatches.exec(line.text)) {
// If we're looking at an "anything goes" variable, that has a capture group so use that instead
for (let match = this.variableRegex.exec(line.text); match; match = this.variableRegex.exec(line.text)) {
// If we're looking at special characters variable, use the extracted variable name in capture group
let varName = match[0][1] === '{'
? '$' + match[1]
? '$' + match.groups?.specialName?.replace(/`(.)/g, '$1') // Remove backticks used as escape char for curly braces, unicode etc.
: match[0];

// If there's a scope, we need to remove it
const colon = varName.indexOf(':');
if (colon !== -1) {
varName = '$' + varName.substring(colon + 1);
}
// If invalid scope, ignore
const scope = varName.substring(1, colon);
if (!this.supportedScopes.test(scope)) {
continue;
}

// These characters need to be trimmed off
if ([';', ',', '-', '+', '/', '*'].includes(varName[varName.length - 1])) {
varName = varName.substring(0, varName.length - 1);
varName = '$' + varName.substring(colon + 1);
}

// If known PowerShell constant, ignore
if (ignoredVariables.test(varName)) {
if (this.knownConstants.test(varName)) {
continue;
}

Expand Down
Loading