Skip to content

Commit

Permalink
fix node.children is not iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
ZainChen committed Sep 4, 2020
1 parent 251393f commit 27df483
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/json/jsonOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider<number> {

private getChildrenOffsets(node: json.Node): number[] {
const offsets: number[] = [];
for (const child of node.children) {
const childPath = json.getLocation(this.text, child.offset).path;
const childNode = json.findNodeAtLocation(this.tree, childPath);
if (childNode) {
offsets.push(childNode.offset);
if (node && node.children) {
for (const child of node.children) {
const childPath = json.getLocation(this.text, child.offset).path;
const childNode = json.findNodeAtLocation(this.tree, childPath);
if (childNode) {
offsets.push(childNode.offset);
}
}
}
return offsets;
Expand Down Expand Up @@ -164,25 +166,33 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider<number> {
if (node.parent.type === 'array') {
const prefix = node.parent.children.indexOf(node).toString();
if (node.type === 'object') {
return prefix + ': { '+ node.children.length +' }';
return prefix + ': { '+ this.getNodeChildrenCount(node) +' }';
}
if (node.type === 'array') {
return prefix + ': [ '+ node.children.length +' ]';
return prefix + ': [ '+ this.getNodeChildrenCount(node) +' ]';
}
return prefix + ':' + node.value.toString();
}
else {
const property = node.parent.children[0].value.toString();
if (node.type === 'array' || node.type === 'object') {
if (node.type === 'object') {
return '{ '+ node.children.length +' } ' + property;
return '{ '+ this.getNodeChildrenCount(node) +' } ' + property;
}
if (node.type === 'array') {
return '[ '+ node.children.length +' ] ' + property;
return '[ '+ this.getNodeChildrenCount(node) +' ] ' + property;
}
}
const value = this.editor.document.getText(new vscode.Range(this.editor.document.positionAt(node.offset), this.editor.document.positionAt(node.offset + node.length)));
return `${property}: ${value}`;
}
}

private getNodeChildrenCount(node: json.Node): string {
let count = '';
if (node && node.children) {
count = node.children.length + '';
}
return count;
}
}

0 comments on commit 27df483

Please sign in to comment.