-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathdependencyExplorer.ts
248 lines (226 loc) · 11 KB
/
dependencyExplorer.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import AwaitLock from "await-lock";
import * as fse from "fs-extra";
import * as _ from "lodash";
import * as path from "path";
import {
commands, Disposable, ExtensionContext, QuickPickItem, TextEditor, TreeView,
TreeViewExpansionEvent, TreeViewSelectionChangeEvent, TreeViewVisibilityChangeEvent, Uri, window,
} from "vscode";
import { instrumentOperationAsVsCodeCommand, sendInfo } from "vscode-extension-telemetry-wrapper";
import { Commands } from "../commands";
import { deleteFiles } from "../explorerCommands/delete";
import { JavaType, newFile, newFolder, newJavaFileWithSpecificType, newPackage, newResource } from "../explorerCommands/new";
import { renameFile } from "../explorerCommands/rename";
import { getCmdNode } from "../explorerCommands/utility";
import { Jdtls } from "../java/jdtls";
import { INodeData } from "../java/nodeData";
import { Settings } from "../settings";
import { EventCounter, Utility } from "../utility";
import { DataNode } from "./dataNode";
import { DependencyDataProvider } from "./dependencyDataProvider";
import { DragAndDropController } from "./DragAndDropController";
import { ExplorerNode } from "./explorerNode";
import { explorerNodeCache } from "./nodeCache/explorerNodeCache";
export class DependencyExplorer implements Disposable {
public static getInstance(context: ExtensionContext): DependencyExplorer {
if (!this._instance) {
this._instance = new DependencyExplorer(context);
}
return this._instance;
}
private static _instance: DependencyExplorer;
private _dependencyViewer: TreeView<ExplorerNode>;
private _dataProvider: DependencyDataProvider;
private _revealLock: AwaitLock;
constructor(public readonly context: ExtensionContext) {
this._dataProvider = new DependencyDataProvider(context);
const dndController = new DragAndDropController();
this._dependencyViewer = window.createTreeView(
"javaProjectExplorer",
{
treeDataProvider: this._dataProvider,
showCollapseAll: true,
dragAndDropController: dndController,
}
);
this._revealLock = new AwaitLock();
// register reveal events
context.subscriptions.push(
window.onDidChangeActiveTextEditor((textEditor: TextEditor | undefined) => {
if (this._dependencyViewer.visible && textEditor?.document) {
const uri: Uri = textEditor.document.uri;
this.reveal(uri);
}
}),
this._dependencyViewer.onDidChangeVisibility((e: TreeViewVisibilityChangeEvent) => {
if (e.visible) {
sendInfo("", { projectManagerVisible: 1 });
if (window.activeTextEditor) {
this.reveal(window.activeTextEditor.document.uri);
}
}
}),
this._dataProvider.onDidChangeTreeData(() => {
if (this._dependencyViewer.visible && window.activeTextEditor) {
this.reveal(window.activeTextEditor.document.uri);
}
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_IN_PROJECT_EXPLORER, async (uri: Uri) => {
await commands.executeCommand(Commands.JAVA_PROJECT_EXPLORER_FOCUS);
let fsPath: string = uri.fsPath;
const fileName: string = path.basename(fsPath);
if (/(.*\.gradle)|(.*\.gradle\.kts)|(pom\.xml)$/.test(fileName)) {
fsPath = path.dirname(fsPath);
}
uri = Uri.file(fsPath);
if ((await fse.stat(fsPath)).isFile()) {
await commands.executeCommand(Commands.VSCODE_OPEN, uri, { preserveFocus: true });
}
this.reveal(uri, false /*force to reveal even the sync setting is turned off*/);
}),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_EXPLORER_SHOW_NONJAVA_RESOURCES, async () => {
Settings.switchNonJavaResourceFilter(true);
}),
instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_EXPLORER_HIDE_NONJAVA_RESOURCES, async () => {
Settings.switchNonJavaResourceFilter(false);
}),
);
// register telemetry events
context.subscriptions.push(
this._dependencyViewer.onDidChangeSelection((_e: TreeViewSelectionChangeEvent<ExplorerNode>) => {
EventCounter.increase("didChangeSelection");
}),
this._dependencyViewer.onDidCollapseElement((_e: TreeViewExpansionEvent<ExplorerNode>) => {
EventCounter.increase("didCollapseElement");
}),
this._dependencyViewer.onDidExpandElement((_e: TreeViewExpansionEvent<ExplorerNode>) => {
EventCounter.increase("didExpandElement");
}),
);
// register keybinding commands
context.subscriptions.push(
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW, async (node: DataNode) => {
newResource(node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_CLASS, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.CLASS, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_INTERFACE, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.INTERFACE, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ENUM, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ENUM, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_RECORD, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.RECORD, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ANNOTATION, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ANNOTATION, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ABSTRACT_CLASS, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ABSTRACT_CLASS, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_FILE, async (node: DataNode) => {
newFile(node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_FOLDER, async (node: DataNode) => {
newFolder(node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_PACKAGE, async (node?: DataNode) => {
let cmdNode = getCmdNode(this._dependencyViewer.selection, node);
if (!cmdNode) {
cmdNode = await this.promptForProjectNode();
}
newPackage(cmdNode);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_EXPLORER_NEW_PACKAGE, (node: Uri) => {
newPackage(node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_REVEAL_FILE_OS, (node?: DataNode) => {
const cmdNode = getCmdNode(this._dependencyViewer.selection, node);
if (cmdNode?.uri) {
commands.executeCommand("revealFileInOS", Uri.parse(cmdNode.uri));
}
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_FILE_PATH, (node?: DataNode) => {
const cmdNode = getCmdNode(this._dependencyViewer.selection, node);
if (cmdNode?.uri) {
commands.executeCommand("copyFilePath", Uri.parse(cmdNode.uri));
}
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_COPY_RELATIVE_FILE_PATH, (node?: DataNode) => {
const cmdNode = getCmdNode(this._dependencyViewer.selection, node);
if (cmdNode?.uri) {
commands.executeCommand("copyRelativeFilePath", Uri.parse(cmdNode.uri));
}
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_RENAME_FILE, (node?: DataNode) => {
renameFile(getCmdNode(this._dependencyViewer.selection, node));
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_MOVE_FILE_TO_TRASH, (node?: DataNode) => {
deleteFiles(getCmdNode(this._dependencyViewer.selection, node), true);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_DELETE_FILE_PERMANENTLY, (node?: DataNode) => {
deleteFiles(getCmdNode(this._dependencyViewer.selection, node), false);
}),
);
}
public dispose(): void {
if (this._dependencyViewer) {
this._dependencyViewer.dispose();
}
}
public async reveal(uri: Uri, needCheckSyncSetting: boolean = true): Promise<void> {
try {
await this._revealLock.acquireAsync();
if (needCheckSyncSetting && !Settings.syncWithFolderExplorer()) {
return;
}
if (!await Utility.isRevealable(uri)) {
return;
}
let node: DataNode | undefined = explorerNodeCache.getDataNode(uri);
if (!node) {
const paths: INodeData[] = await Jdtls.resolvePath(uri.toString());
if (!_.isEmpty(paths)) {
node = await this._dataProvider.revealPaths(paths);
}
}
if (!node) {
return;
}
await this._dependencyViewer.reveal(node);
} finally {
this._revealLock.release();
}
}
public get dataProvider(): DependencyDataProvider {
return this._dataProvider;
}
private async promptForProjectNode(): Promise<DataNode | undefined> {
const projects = await this._dataProvider.getRootProjects();
if (projects.length === 0) {
window.showInformationMessage("There is no Java projects in current workspace.");
return undefined;
} else if (projects.length === 1) {
return projects[0] as DataNode;
} else {
const options: IProjectPickItem[] = projects.map((p: DataNode) => {
return {
label: p.name,
node: p,
};
});
const choice: IProjectPickItem | undefined = await window.showQuickPick(options, {
placeHolder: "Choose a project",
ignoreFocusOut: true,
});
return choice?.node as DataNode;
}
}
}
interface IProjectPickItem extends QuickPickItem {
node: ExplorerNode;
}