-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathPrimaryTypeNode.ts
151 lines (129 loc) · 5.13 KB
/
PrimaryTypeNode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { Command, commands, DocumentSymbol, SymbolInformation, SymbolKind, TextDocument, ThemeIcon, Uri, workspace } from "vscode";
import { createUuid, sendOperationEnd, sendOperationStart } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { Explorer } from "../constants";
import { INodeData, TypeKind } from "../java/nodeData";
import { Settings } from "../settings";
import { isTest } from "../utility";
import { DataNode } from "./dataNode";
import { DocumentSymbolNode } from "./documentSymbolNode";
import { ExplorerNode } from "./explorerNode";
import { ProjectNode } from "./projectNode";
export class PrimaryTypeNode extends DataNode {
public static K_TYPE_KIND = "TypeKind";
constructor(nodeData: INodeData, parent: DataNode, protected _rootNode?: DataNode) {
super(nodeData, parent);
}
public getPackageRootPath(): string {
if (this._rootNode?.uri) {
return Uri.parse(this._rootNode.uri).fsPath;
}
const unmanagedFolder = this.getUnmanagedFolderAncestor();
if (unmanagedFolder?.uri) {
return Uri.parse(unmanagedFolder.uri).fsPath;
}
return "";
}
public getLabel(): string {
return this._nodeData.displayName ?? this._nodeData.name;
}
protected async loadData(): Promise<SymbolInformation[] | DocumentSymbol[] | undefined> {
if (!this.hasChildren() || !this.nodeData.uri) {
return undefined;
}
return workspace.openTextDocument(Uri.parse(this.nodeData.uri)).then((doc) => {
return this.getSymbols(doc);
});
}
protected createChildNodeList(): ExplorerNode[] {
const result: ExplorerNode[] = [];
if (this.nodeData.children && this.nodeData.children.length) {
for (const child of this.nodeData.children) {
const documentSymbol: DocumentSymbol = child as DocumentSymbol;
// Do not show the package declaration
if (documentSymbol.kind === SymbolKind.Package) {
continue;
}
if (documentSymbol.name === this.nodeData.name) {
for (const childSymbol of documentSymbol.children) {
result.push(new DocumentSymbolNode(childSymbol, this));
}
}
}
}
return result;
}
protected get iconPath(): string | ThemeIcon {
switch (this.nodeData.metaData?.[PrimaryTypeNode.K_TYPE_KIND]) {
case TypeKind.Enum:
return new ThemeIcon("symbol-enum");
case TypeKind.Interface:
return new ThemeIcon("symbol-interface");
default:
return new ThemeIcon("symbol-class");
}
}
protected hasChildren(): boolean {
return Settings.showMembers();
}
private async getSymbols(document: TextDocument): Promise<SymbolInformation[] | DocumentSymbol[] | undefined> {
let error;
const operationId = createUuid();
const startAt: number = Date.now();
sendOperationStart(operationId, "vscode.executeDocumentSymbolProvider");
try {
return await commands.executeCommand<SymbolInformation[]>(
"vscode.executeDocumentSymbolProvider",
document.uri,
);
} catch (err) {
error = err;
throw err;
} finally {
const duration = Date.now() - startAt;
sendOperationEnd(operationId, "vscode.executeDocumentSymbolProvider", duration, error);
}
}
protected get command(): Command {
return {
title: "Open source file content",
command: Commands.VSCODE_OPEN,
arguments: [Uri.parse(this.uri || ""), { preserveFocus: true }],
};
}
protected get contextValue(): string {
let contextValue: string = Explorer.ContextValueType.Type;
const type = this.nodeData.metaData?.[PrimaryTypeNode.K_TYPE_KIND];
if (type === TypeKind.Enum) {
contextValue += "+enum";
} else if (type === TypeKind.Interface) {
contextValue += "+interface";
} else {
contextValue += "+class";
}
if (isTest(this._rootNode?.nodeData)) {
contextValue += "+test";
}
if (this._rootNode?.getParent() instanceof ProjectNode
&& (this._rootNode.getParent() as ProjectNode).nodeData?.metaData?.MaxSourceVersion >= 16) {
contextValue += "+allowRecord";
}
return contextValue;
}
/**
* @returns ProjectNode if the current node is under an unmanaged folder,
* otherwise undefined.
*/
private getUnmanagedFolderAncestor(): ProjectNode | undefined {
let ancestor = this.getParent();
while (ancestor && !(ancestor instanceof ProjectNode)) {
ancestor = ancestor.getParent();
}
if (ancestor?.isUnmanagedFolder()) {
return ancestor;
}
return undefined;
}
}