Skip to content

Commit 49d0ce0

Browse files
Only show readable variable types (#409)
* Get variable info of children * Fix tests * Fix error in settings * fix lint
1 parent 36ec556 commit 49d0ce0

File tree

3 files changed

+92
-46
lines changed

3 files changed

+92
-46
lines changed

src/extension/debugger/inlineValue/pythonInlineValueProvider.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ export class PythonInlineValueProvider implements InlineValuesProvider {
6565
'self',
6666
];
6767

68-
const pythonVariables: any[] = variablesRequest.variables
68+
let includeTypes = ['int', 'float', 'str', 'list', 'dict', 'tuple', 'set', 'bool'];
69+
70+
const pythonVariables = variablesRequest.variables
6971
.filter((variable: any) => variable.type)
70-
.map((variable: any) => variable.name);
72+
.reduce((acc: { [key: string]: any }, variable: any) => {
73+
acc[variable.name] = {
74+
type: variable.type,
75+
variablesReference: variable.variablesReference,
76+
};
77+
return acc;
78+
}, {});
7179

7280
let variableRegex = new RegExp(
7381
'(?:[a-zA-Z_][a-zA-Z0-9_]*\\.)*' + //match any number of variable names separated by '.'
@@ -91,13 +99,26 @@ export class PythonInlineValueProvider implements InlineValuesProvider {
9199
if (pythonKeywords.includes(varName)) {
92100
continue;
93101
}
94-
if (pythonVariables.includes(varName.split('.')[0])) {
102+
let baseVarName = varName.split('.')[0];
103+
if (pythonVariables.hasOwnProperty(baseVarName)) {
95104
if (varName.includes('.')) {
96-
const rng = new Range(l, match.index, l, match.index + varName.length);
97-
allValues.push(new InlineValueEvaluatableExpression(rng, varName));
105+
//Find variable name in the variable children
106+
let foundVariable = (
107+
await customRequest('variables', {
108+
variablesReference: pythonVariables[baseVarName].variablesReference,
109+
})
110+
).variables.find((variable: any) => variable.evaluateName === varName);
111+
//Check the type of the variable before adding to the inline values
112+
if (foundVariable && includeTypes.includes(foundVariable.type)) {
113+
const rng = new Range(l, match.index, l, match.index + varName.length);
114+
allValues.push(new InlineValueEvaluatableExpression(rng, varName));
115+
}
98116
} else {
99-
const rng = new Range(l, match.index, l, match.index + varName.length);
100-
allValues.push(new InlineValueVariableLookup(rng, varName, false));
117+
//Check the type of the variable before adding to the inline values
118+
if (includeTypes.includes(pythonVariables[baseVarName].type)) {
119+
const rng = new Range(l, match.index, l, match.index + varName.length);
120+
allValues.push(new InlineValueVariableLookup(rng, varName, false));
121+
}
101122
}
102123
}
103124
}

src/extension/extensionInit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export async function registerDebugger(context: IExtensionContext): Promise<IExt
215215

216216
context.subscriptions.push(
217217
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
218-
if (event.affectsConfiguration('debugpy')) {
218+
if (event.affectsConfiguration('debugpy.showPythonInlineValues')) {
219219
const showInlineValues = getConfiguration('debugpy').get<boolean>('showPythonInlineValues', false);
220220
if (!showInlineValues) {
221221
registerInlineValuesProviderDisposable.dispose();

src/test/unittest/inlineValue/pythonInlineValueProvider.unit.test.ts

Lines changed: 63 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,35 @@ suite('Debugging - pythonInlineProvider', () => {
202202
});
203203

204204
test('ProvideInlineValues function should return all the vars in the python file with self in class', async () => {
205+
customRequestStub
206+
.withArgs('variables', sinon.match.any)
207+
.onFirstCall()
208+
.resolves({
209+
variables: [
210+
{
211+
name: 'self',
212+
value: '<__main__.Person object at 0x10b223310>',
213+
type: 'Person',
214+
evaluateName: 'self',
215+
variablesReference: 5,
216+
},
217+
],
218+
});
205219
customRequestStub.withArgs('variables', sinon.match.any).resolves({
206220
variables: [
207221
{
208-
name: 'self',
209-
value: '<__main__.Person object at 0x10b223310>',
210-
type: 'Person',
211-
evaluateName: 'self',
212-
variablesReference: 5,
222+
name: 'name',
223+
value: "'John'",
224+
type: 'str',
225+
evaluateName: 'self.name',
226+
variablesReference: 0,
227+
},
228+
{
229+
name: 'age',
230+
value: '25',
231+
type: 'int',
232+
evaluateName: 'self.age',
233+
variablesReference: 0,
213234
},
214235
],
215236
});
@@ -278,15 +299,46 @@ suite('Debugging - pythonInlineProvider', () => {
278299
expect(result).to.deep.equal(expected);
279300
});
280301

281-
test('ProvideInlineValues function should return all the vars in the python file with class variables', async () => {
302+
test('ProvideInlineValues function should return the vars in the python file with readable class variables', async () => {
303+
customRequestStub
304+
.withArgs('variables', sinon.match.any)
305+
.onFirstCall()
306+
.resolves({
307+
variables: [
308+
{
309+
name: 'person1',
310+
value: '<__main__.Person object at 0x1085c92b0>',
311+
type: 'Person',
312+
evaluateName: 'person1',
313+
variablesReference: 7,
314+
},
315+
],
316+
});
282317
customRequestStub.withArgs('variables', sinon.match.any).resolves({
283318
variables: [
284319
{
285-
name: 'person1',
286-
value: '<__main__.Person object at 0x1085c92b0>',
287-
type: 'Person',
288-
evaluateName: 'person1',
289-
variablesReference: 7,
320+
name: 'age',
321+
value: '30',
322+
type: 'int',
323+
evaluateName: 'person1.age',
324+
variablesReference: 0,
325+
},
326+
{
327+
name: 'id',
328+
value: '1',
329+
type: 'int',
330+
evaluateName: 'person1.id',
331+
variablesReference: 0,
332+
},
333+
{
334+
name: 'name',
335+
value: "'John Doe'",
336+
type: 'str',
337+
evaluateName: 'person1.name',
338+
variablesReference: 0,
339+
presentationHint: {
340+
attributes: ['rawString'],
341+
},
290342
},
291343
],
292344
});
@@ -299,33 +351,6 @@ suite('Debugging - pythonInlineProvider', () => {
299351

300352
const result = await inlineValueProvider.provideInlineValues(document, viewPort, context);
301353
const expected = [
302-
{
303-
range: {
304-
c: {
305-
c: 9,
306-
e: 0,
307-
},
308-
e: {
309-
c: 9,
310-
e: 7,
311-
},
312-
},
313-
variableName: 'person1',
314-
caseSensitiveLookup: false,
315-
},
316-
{
317-
range: {
318-
c: {
319-
c: 10,
320-
e: 0,
321-
},
322-
e: {
323-
c: 10,
324-
e: 13,
325-
},
326-
},
327-
expression: 'person1.greet',
328-
},
329354
{
330355
range: {
331356
c: {

0 commit comments

Comments
 (0)