Skip to content

Commit 0c3659a

Browse files
authored
fix outline after lsp4j api change (#68)
* fix outline after lsp4j api change
1 parent c106c1f commit 0c3659a

File tree

5 files changed

+119
-48
lines changed

5 files changed

+119
-48
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"explorer"
1313
],
1414
"engines": {
15-
"vscode": "^1.24.0"
15+
"vscode": "^1.28.0"
1616
},
1717
"repository": {
1818
"type": "git",

src/views/baseSymbolNode.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { Command, DocumentSymbol, Range, SymbolInformation, SymbolKind } from "vscode";
5+
import { Commands } from "../commands";
6+
import { Services } from "../services";
7+
import { ExplorerNode } from "./explorerNode";
8+
import { TypeRootNode } from "./typeRootNode";
9+
10+
export abstract class BaseSymbolNode extends ExplorerNode {
11+
12+
private static _iconMap: Map<SymbolKind, string> = new Map([
13+
[SymbolKind.Package, "Namespace"],
14+
[SymbolKind.Class, "Class"],
15+
[SymbolKind.Interface, "Interface"],
16+
[SymbolKind.Enum, "Enumerator"],
17+
[SymbolKind.EnumMember, "EnumItem"],
18+
[SymbolKind.Constant, "Constant"],
19+
[SymbolKind.Method, "Method"],
20+
[SymbolKind.Function, "Method"],
21+
[SymbolKind.Constructor, "Method"],
22+
[SymbolKind.Field, "Field"],
23+
[SymbolKind.Property, "Property"],
24+
[SymbolKind.Variable, "LocalVariable"],
25+
[SymbolKind.Constant, "Constant"],
26+
]);
27+
28+
constructor(public readonly symbolInfo: SymbolInformation | DocumentSymbol, private parent: TypeRootNode) {
29+
super(parent);
30+
}
31+
32+
protected get iconPath(): any {
33+
if (BaseSymbolNode._iconMap.has(this.symbolInfo.kind)) {
34+
const iconFileName = BaseSymbolNode._iconMap.get(this.symbolInfo.kind);
35+
return {
36+
light: Services.context.asAbsolutePath(`./images/symbols/${iconFileName}_16x.svg`),
37+
dark: Services.context.asAbsolutePath(`./images/symbols/${iconFileName}_inverse_16x.svg`),
38+
};
39+
}
40+
}
41+
42+
protected get command(): Command {
43+
return {
44+
title: "Go to outline",
45+
command: Commands.VIEW_PACKAGE_OUTLINE,
46+
arguments: [(this.getParent() as TypeRootNode).uri, this.range],
47+
};
48+
}
49+
50+
protected abstract get range(): Range;
51+
}

src/views/documentSymbolNode.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import { DocumentSymbol, Range, TreeItem, TreeItemCollapsibleState } from "vscode";
5+
import { BaseSymbolNode } from "./baseSymbolNode";
6+
import { ExplorerNode } from "./explorerNode";
7+
import { TypeRootNode } from "./typeRootNode";
8+
9+
export class DocumentSymbolNode extends BaseSymbolNode {
10+
11+
constructor(symbolInfo: DocumentSymbol, parent: TypeRootNode) {
12+
super(symbolInfo, parent);
13+
}
14+
15+
public getChildren(): ExplorerNode[] | Thenable<ExplorerNode[]> {
16+
const res: ExplorerNode[] = [];
17+
if (this.symbolInfo && (<DocumentSymbol>this.symbolInfo).children && (<DocumentSymbol>this.symbolInfo).children.length) {
18+
(<DocumentSymbol>this.symbolInfo).children.forEach((child) => {
19+
res.push(new DocumentSymbolNode(child, this.getParent() as TypeRootNode));
20+
});
21+
}
22+
return res;
23+
}
24+
25+
public getTreeItem(): TreeItem | Promise<TreeItem> {
26+
if (this.symbolInfo) {
27+
const item = new TreeItem(this.symbolInfo.name,
28+
((<DocumentSymbol>this.symbolInfo).children && (<DocumentSymbol>this.symbolInfo).children.length)
29+
? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None);
30+
item.iconPath = this.iconPath;
31+
item.command = this.command;
32+
return item;
33+
}
34+
}
35+
36+
protected get range(): Range {
37+
return (<DocumentSymbol>this.symbolInfo).range;
38+
}
39+
}

src/views/symbolNode.ts

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { Command, SymbolInformation, SymbolKind, TreeItem, TreeItemCollapsibleState } from "vscode";
5-
import { Commands } from "../commands";
4+
import { Range, SymbolInformation, TreeItem, TreeItemCollapsibleState } from "vscode";
65
import { ITypeRootNodeData } from "../java/typeRootNodeData";
7-
import { Services } from "../services";
6+
import { BaseSymbolNode } from "./baseSymbolNode";
87
import { ExplorerNode } from "./explorerNode";
98
import { TypeRootNode } from "./typeRootNode";
109

11-
export class SymbolNode extends ExplorerNode {
12-
13-
private static _iconMap: Map<SymbolKind, string> = new Map([
14-
[SymbolKind.Class, "Class"],
15-
[SymbolKind.Interface, "Interface"],
16-
[SymbolKind.Enum, "Enumerator"],
17-
[SymbolKind.EnumMember, "EnumItem"],
18-
[SymbolKind.Constant, "Constant"],
19-
[SymbolKind.Method, "Method"],
20-
[SymbolKind.Function, "Method"],
21-
[SymbolKind.Constructor, "Method"],
22-
[SymbolKind.Field, "Field"],
23-
[SymbolKind.Property, "Property"],
24-
[SymbolKind.Variable, "LocalVariable"],
25-
[SymbolKind.Constant, "Constant"],
26-
27-
]);
28-
10+
export class SymbolNode extends BaseSymbolNode {
2911
private _children: SymbolInformation[];
3012

31-
constructor(public readonly symbolInfo: SymbolInformation, private parent: TypeRootNode) {
32-
super(parent);
13+
constructor(symbolInfo: SymbolInformation, parent: TypeRootNode) {
14+
super(symbolInfo, parent);
3315
}
3416

3517
public getChildren(): ExplorerNode[] | Thenable<ExplorerNode[]> {
@@ -56,21 +38,7 @@ export class SymbolNode extends ExplorerNode {
5638
}
5739
}
5840

59-
private get iconPath(): any {
60-
if (SymbolNode._iconMap.has(this.symbolInfo.kind)) {
61-
const iconFileName = SymbolNode._iconMap.get(this.symbolInfo.kind);
62-
return {
63-
light: Services.context.asAbsolutePath(`./images/symbols/${iconFileName}_16x.svg`),
64-
dark: Services.context.asAbsolutePath(`./images/symbols/${iconFileName}_inverse_16x.svg`),
65-
};
66-
}
67-
}
68-
69-
protected get command(): Command {
70-
return {
71-
title: "Go to outline",
72-
command: Commands.VIEW_PACKAGE_OUTLINE,
73-
arguments: [(this.getParent() as TypeRootNode).uri, this.symbolInfo.location.range],
74-
};
41+
protected get range(): Range {
42+
return (<SymbolInformation>this.symbolInfo).location.range;
7543
}
7644
}

src/views/typeRootNode.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { Command, commands, SymbolInformation, TextDocument, ThemeIcon, Uri, workspace } from "vscode";
4+
import { Command, commands, DocumentSymbol, SymbolInformation, TextDocument, ThemeIcon, Uri, workspace } from "vscode";
55
import { Commands } from "../commands";
66
import { INodeData } from "../java/nodeData";
77
import { ITypeRootNodeData, TypeRootKind } from "../java/typeRootNodeData";
88
import { Services } from "../services";
99
import { Settings } from "../settings";
1010
import { DataNode } from "./dataNode";
11+
import { DocumentSymbolNode } from "./documentSymbolNode";
1112
import { ExplorerNode } from "./explorerNode";
1213
import { SymbolNode } from "./symbolNode";
1314

@@ -16,7 +17,7 @@ export class TypeRootNode extends DataNode {
1617
super(nodeData, parent);
1718
}
1819

19-
protected loadData(): Thenable<SymbolInformation[]> {
20+
protected loadData(): Thenable<SymbolInformation[] | DocumentSymbol[]> {
2021
return workspace.openTextDocument(Uri.parse(this.nodeData.uri)).then((doc) => {
2122
return this.getSymbols(doc);
2223
});
@@ -26,13 +27,25 @@ export class TypeRootNode extends DataNode {
2627
const data = <ITypeRootNodeData>this.nodeData;
2728
const result: ExplorerNode[] = [];
2829
if (this.nodeData.children && this.nodeData.children.length) {
29-
data.symbolTree = this.buildSymbolTree(this.nodeData.children);
30-
const directChildren = data.symbolTree.get(this.nodeData.name);
31-
if (directChildren && directChildren.length) {
32-
directChildren.forEach((symbolInfo) => {
33-
result.push(new SymbolNode(symbolInfo, this));
30+
// After DocumentSymbolProvider api change at
31+
// https://github.com/eclipse/eclipse.jdt.ls/issues/780, the vscode.executeDocumentSymbolProvider
32+
// will return DocumentSymbol[]
33+
if (this.nodeData.children && this.nodeData.children.length && this.nodeData.children[0].hasOwnProperty("children")) {
34+
// if the element in children is DocumentSymbol
35+
this.nodeData.children.forEach((symbolInfo: DocumentSymbol) => {
36+
result.push(new DocumentSymbolNode(symbolInfo, this));
3437
});
38+
} else {
39+
// if the element in children is SymbolInformation
40+
data.symbolTree = this.buildSymbolTree(this.nodeData.children);
41+
const directChildren = data.symbolTree.get(this.nodeData.name);
42+
if (directChildren && directChildren.length) {
43+
directChildren.forEach((symbolInfo) => {
44+
result.push(new SymbolNode(symbolInfo, this));
45+
});
46+
}
3547
}
48+
3649
}
3750
return result;
3851
}
@@ -49,7 +62,7 @@ export class TypeRootNode extends DataNode {
4962
return Settings.showOutline();
5063
}
5164

52-
private getSymbols(document: TextDocument): Thenable<SymbolInformation[]> {
65+
private getSymbols(document: TextDocument): Thenable<SymbolInformation[] | DocumentSymbol[]> {
5366
return commands.executeCommand<SymbolInformation[]>(
5467
"vscode.executeDocumentSymbolProvider",
5568
document.uri,

0 commit comments

Comments
 (0)