Repository navigation
Expand file tree
/
Copy pathdataNode.ts
More file actions
133 lines (112 loc) · 4.16 KB
/
Copy pathdataNode.ts
File metadata and controls
133 lines (112 loc) · 4.16 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as _ from "lodash";
import { ThemeIcon, TreeItem, TreeItemCollapsibleState, Uri } from "vscode";
import { INodeData, NodeKind } from "../java/nodeData";
import { explorerLock } from "../utils/Lock";
import { ExplorerNode } from "./explorerNode";
export abstract class DataNode extends ExplorerNode {
protected _childrenNodes: ExplorerNode[];
constructor(protected _nodeData: INodeData, parent?: DataNode) {
super(parent);
}
public getTreeItem(): TreeItem | Promise<TreeItem> {
const item = new TreeItem(
this._nodeData.displayName || this._nodeData.name,
this.hasChildren() ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None,
);
item.description = this.description;
item.iconPath = this.iconPath;
item.command = this.command;
item.contextValue = this.computeContextValue();
if (this.uri) {
switch (this._nodeData.kind) {
case NodeKind.Project:
case NodeKind.PackageRoot:
case NodeKind.Package:
case NodeKind.PrimaryType:
case NodeKind.CompilationUnit:
case NodeKind.ClassFile:
case NodeKind.Folder:
case NodeKind.File:
item.resourceUri = Uri.parse(this.uri);
break;
}
}
return item;
}
public get nodeData(): INodeData {
return this._nodeData;
}
public get uri() {
return this._nodeData.uri;
}
public get path() {
return this._nodeData.path;
}
public get handlerIdentifier() {
return this._nodeData.handlerIdentifier;
}
public get name() {
return this._nodeData.name;
}
public async revealPaths(paths: INodeData[]): Promise<DataNode | undefined> {
if (_.isEmpty(paths)) {
return this;
}
const childNodeData = paths.shift();
const children: ExplorerNode[] = await this.getChildren();
const childNode = <DataNode>children?.find((child: DataNode) =>
child.nodeData.name === childNodeData?.name && child.path === childNodeData?.path);
return (childNode && paths.length) ? childNode.revealPaths(paths) : childNode;
}
public async getChildren(): Promise<ExplorerNode[]> {
try {
await explorerLock.acquireAsync();
if (!this._nodeData.children) {
const data = await this.loadData();
this._nodeData.children = data;
this._childrenNodes = this.createChildNodeList() || [];
this.sort();
return this._childrenNodes;
}
return this._childrenNodes;
} finally {
explorerLock.release();
}
}
public computeContextValue(): string | undefined {
let contextValue = this.contextValue;
if (this.uri && this.uri.startsWith("file:")) {
contextValue = `${contextValue || ""}+uri`;
}
if (contextValue) {
contextValue = `java:${contextValue}`;
}
return contextValue;
}
protected sort() {
this._childrenNodes.sort((a: ExplorerNode, b: ExplorerNode) => {
if (a instanceof DataNode && b instanceof DataNode) {
if (a.nodeData.kind === b.nodeData.kind) {
return a.nodeData.name < b.nodeData.name ? -1 : 1;
} else {
return a.nodeData.kind - b.nodeData.kind;
}
}
return 0;
});
}
protected hasChildren(): boolean {
return true;
}
protected get description(): string | boolean | undefined {
return undefined;
}
protected get contextValue(): string | undefined {
return undefined;
}
protected abstract get iconPath(): string | Uri | { light: string | Uri; dark: string | Uri } | ThemeIcon;
protected abstract loadData(): Promise<any[] | undefined>;
protected abstract createChildNodeList(): ExplorerNode[] | undefined;
}