Skip to content

Commit 93e2d59

Browse files
Update inline function to remove strins values
1 parent 6e1b07b commit 93e2d59

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

src/extension/debugger/inlineValue/pythonInlineValueProvider.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Range,
99
TextDocument,
1010
InlineValueVariableLookup,
11+
InlineValueEvaluatableExpression,
1112
} from 'vscode';
1213
import { customRequest } from '../../common/vscodeapi';
1314

@@ -59,14 +60,19 @@ export class PythonInlineValueProvider implements InlineValuesProvider {
5960
'if',
6061
'or',
6162
'yield',
63+
'self',
6264
];
6365

6466
const pythonVariables: any[] = variablesRequest.variables
6567
.filter((variable: any) => variable.type)
6668
.map((variable: any) => variable.name);
6769

68-
//VariableRegex for matching variables names in a python file, excluding strings
69-
let variableRegex = /(?<!['"\w])\b[a-zA-Z_][a-zA-Z0-9_]*\b(?![^"\n]*"(?:(?:[^"\n]*"){2})*[^"\n]*$)/g;
70+
let variableRegex = new RegExp(
71+
'(?:self.)?' + //match self. if present
72+
'[a-zA-Z_][a-zA-Z0-9_]*', //math variable name
73+
'g',
74+
);
75+
7076
const allValues: InlineValue[] = [];
7177
for (let l = viewPort.start.line; l <= viewPort.end.line; l++) {
7278
const line = document.lineAt(l);
@@ -75,18 +81,47 @@ export class PythonInlineValueProvider implements InlineValuesProvider {
7581
continue;
7682
}
7783

78-
for (let match = variableRegex.exec(line.text); match; match = variableRegex.exec(line.text)) {
84+
let code = removeCharsOutsideBraces(line.text);
85+
86+
for (let match = variableRegex.exec(code); match; match = variableRegex.exec(code)) {
7987
let varName = match[0];
8088
// Skip python keywords
8189
if (pythonKeywords.includes(varName)) {
8290
continue;
8391
}
84-
if (pythonVariables.includes(varName)) {
85-
const rng = new Range(l, match.index, l, match.index + varName.length);
86-
allValues.push(new InlineValueVariableLookup(rng, varName, false));
92+
if (pythonVariables.includes(varName.split('.')[0])) {
93+
if (varName.includes('self')) {
94+
const rng = new Range(l, match.index, l, match.index + varName.length);
95+
allValues.push(new InlineValueEvaluatableExpression(rng, varName));
96+
} else {
97+
const rng = new Range(l, match.index, l, match.index + varName.length);
98+
allValues.push(new InlineValueVariableLookup(rng, varName, false));
99+
}
87100
}
88101
}
89102
}
90103
return allValues;
91104
}
92105
}
106+
107+
function removeCharsOutsideBraces(code: string): string {
108+
// Regular expression to find Python strings
109+
const stringRegex = /(["'])(?:(?=(\\?))\2.)*?\1/g;
110+
111+
//Regular expression to match values inside {}
112+
const insideBracesRegex = /{[^{}]*}/g;
113+
114+
return code.replace(stringRegex, (match) => {
115+
const content = match.slice(1, -1);
116+
117+
let result = '';
118+
let tempMatch;
119+
120+
while ((tempMatch = insideBracesRegex.exec(content)) !== null) {
121+
result += tempMatch[0];
122+
}
123+
const processedContent = result || content;
124+
125+
return match[0] + processedContent + match[0];
126+
});
127+
}

0 commit comments

Comments
 (0)