forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectNode.ts
More file actions
186 lines (164 loc) · 7.09 KB
/
Copy pathprojectNode.ts
File metadata and controls
186 lines (164 loc) · 7.09 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { ThemeIcon, Uri, workspace } from "vscode";
import { Explorer } from "../constants";
import { ContainerEntryKind, IContainerNodeData } from "../java/containerNodeData";
import { HierarchicalPackageNodeData } from "../java/hierarchicalPackageNodeData";
import { Jdtls } from "../java/jdtls";
import { INodeData, NodeKind } from "../java/nodeData";
import { Settings } from "../settings";
import { ContainerNode } from "./containerNode";
import { DataNode } from "./dataNode";
import { ExplorerNode } from "./explorerNode";
import { HierarchicalPackageNode } from "./hierarchicalPackageNode";
import { NodeFactory } from "./nodeFactory";
import { PackageNode } from "./packageNode";
import { PrimaryTypeNode } from "./PrimaryTypeNode";
export class ProjectNode extends DataNode {
constructor(nodeData: INodeData, parent?: DataNode) {
super(nodeData, parent);
}
public async revealPaths(paths: INodeData[]): Promise<DataNode | undefined> {
if (!this.uri) {
return undefined;
}
if (workspace.getWorkspaceFolder(Uri.parse(this.uri))) {
return super.revealPaths(paths);
}
// invisible project uri is not contained in workspace
const childNodeData = paths[0];
const children: ExplorerNode[] = await this.getChildren();
if (!children) {
return undefined;
}
const childNode = <DataNode>children.find((child: DataNode) => {
if (child instanceof HierarchicalPackageNode) {
return childNodeData.name.startsWith(child.nodeData.name + ".") || childNodeData.name === child.nodeData.name;
}
return child.nodeData.name === childNodeData.name && child.path === childNodeData.path;
});
// don't shift when child node is an hierarchical node, or it may lose data of package node
if (!(childNode instanceof HierarchicalPackageNode)) {
paths.shift();
}
return (childNode && paths.length > 0) ? childNode.revealPaths(paths) : childNode;
}
public isUnmanagedFolder(): boolean {
const natureIds: string[] = this.nodeData.metaData?.[NATURE_ID] || [];
for (const natureId of natureIds) {
if (natureId === NatureId.UnmanagedFolder) {
return true;
}
}
return false;
}
protected async loadData(): Promise<INodeData[]> {
let result: INodeData[] = [];
return Jdtls.getPackageData({ kind: NodeKind.Project, projectUri: this.nodeData.uri }).then((res) => {
const sourceContainer: IContainerNodeData[] = [];
res.forEach((node) => {
const containerNode = <IContainerNodeData>node;
if (containerNode.entryKind === ContainerEntryKind.CPE_SOURCE) {
sourceContainer.push(containerNode);
} else {
result.push(node);
}
});
if (sourceContainer.length > 0) {
return Promise.all(sourceContainer.map((c) => Jdtls.getPackageData({ kind: NodeKind.Container, projectUri: this.uri, path: c.path })))
.then((rootPackages) => {
result = result.concat(...rootPackages);
return result;
});
} else {
return result;
}
});
}
protected createChildNodeList(): ExplorerNode[] {
const result: ExplorerNode[] = [];
const packageData: any[] = [];
if (this.nodeData.children && this.nodeData.children.length) {
this.nodeData.children.forEach((data) => {
if (data.kind === NodeKind.Container) {
result.push(new ContainerNode(data, this, this));
} else if (data.kind === NodeKind.PackageRoot) {
result.push(NodeFactory.createPackageRootNode(data, this, this));
} else if (data.kind === NodeKind.Package) {
// Invisible project may have an empty named package root, in that case,
// we will skip it.
packageData.push(data);
} else if (data.kind === NodeKind.PrimaryType) {
// For invisible project with empty named package root with a default package,
// types will be the project node's children
if (data.metaData && data.metaData[PrimaryTypeNode.K_TYPE_KIND]) {
result.push(new PrimaryTypeNode(data, this, undefined));
}
}
});
}
if (packageData.length > 0) {
if (Settings.isHierarchicalView()) {
const data: HierarchicalPackageNodeData = HierarchicalPackageNodeData.createHierarchicalNodeDataByPackageList(packageData);
const hierarchicalPackageNodes: HierarchicalPackageNode[] = data === undefined ? [] : data.children.map((hierarchicalChildrenNode) =>
new HierarchicalPackageNode(hierarchicalChildrenNode, this, this, this));
result.push(...hierarchicalPackageNodes);
} else {
result.push(...packageData.map((data) => new PackageNode(data, this, this, this)));
}
}
result.sort((a: DataNode, b: DataNode) => {
return b.nodeData.kind - a.nodeData.kind;
});
return result;
}
protected get iconPath(): ThemeIcon {
return new ThemeIcon("project");
}
protected get contextValue(): string {
let contextValue: string = Explorer.ContextValueType.Project;
const natureIds: string[] | undefined = this.nodeData.metaData?.[NATURE_ID];
if (natureIds) {
const attributeString: string = getProjectTypeAttributes(natureIds);
contextValue += attributeString;
}
return contextValue;
}
}
function getProjectTypeAttributes(natureIds: string []): string {
let attributeString: string = "";
for (const natureId of natureIds) {
const readableNature: string = getProjectType(natureId);
if (readableNature) {
attributeString += `+${readableNature}`;
}
}
return attributeString;
}
function getProjectType(natureId: string): string {
switch (natureId) {
case NatureId.Java:
return ReadableNature.Java;
case NatureId.Maven:
return ReadableNature.Maven;
case NatureId.Gradle:
return ReadableNature.Gradle;
case NatureId.UnmanagedFolder:
return ReadableNature.UnmanagedFolder;
default:
return "";
}
}
enum NatureId {
Maven = "org.eclipse.m2e.core.maven2Nature",
Gradle = "org.eclipse.buildship.core.gradleprojectnature",
UnmanagedFolder = "org.eclipse.jdt.ls.core.unmanagedFolder",
Java = "org.eclipse.jdt.core.javanature",
}
enum ReadableNature {
Maven = "maven",
Gradle = "gradle",
UnmanagedFolder = "unmanagedFolder",
Java = "java",
}
const NATURE_ID = "NatureId";