Skip to content

Commit

Permalink
generate expression property for new field on the API (#15195)
Browse files Browse the repository at this point in the history
* generate expression property

* fix test
  • Loading branch information
amunger authored Feb 16, 2024
1 parent 7aa8d72 commit d9a06a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/kernels/variables/JupyterVariableProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ suite('JupyterVariablesProvider', () => {
assert.isNotEmpty(results);
assert.equal(results.length, 1);
assert.equal(results[0].variable.name, 'myObject');
assert.equal(results[0].variable.expression, 'myObject');
});

test('provideVariables with a parent should call get children correctly', async () => {
Expand All @@ -98,11 +99,13 @@ suite('JupyterVariablesProvider', () => {
const listItems = await provideVariables(listResult!.variable, 2);

assert.equal(listResult.variable.name, 'myList');
assert.equal(listResult.variable.expression, 'myObject.myList');
assert.isNotEmpty(listItems);
assert.equal(listItems.length, 3);
listItems.forEach((item, index) => {
assert.equal(item.variable.name, index.toString());
assert.equal(item.variable.value, `value${index}`);
assert.equal(item.variable.expression, `myObject.myList[${index}]`);
});
});

Expand Down
13 changes: 13 additions & 0 deletions src/kernels/variables/JupyterVariablesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class JupyterVariablesProvider implements NotebookVariableProvider {
const indexedChildrenCount = result.count ?? 0;
const variable = {
getChildren: (start: number, token: CancellationToken) => this.getChildren(variable, start, kernel, token),
expression: createExpression(result.root, result.propertyChain),
...result
} as Variable;
return { variable, hasNamedChildren, indexedChildrenCount };
Expand All @@ -110,3 +111,15 @@ export class JupyterVariablesProvider implements NotebookVariableProvider {
return await this.variables.getAllVariableDiscriptions(kernel, parent, start, token);
}
}

function createExpression(root: string, propertyChain: (string | number)[]): string {
let expression = root;
for (const property of propertyChain) {
if (typeof property === 'string') {
expression += `.${property}`;
} else {
expression += `[${property}]`;
}
}
return expression;
}

0 comments on commit d9a06a0

Please sign in to comment.