forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdependencyExplorer.ts
More file actions
200 lines (178 loc) · 8.27 KB
/
Copy pathdependencyExplorer.ts
File metadata and controls
200 lines (178 loc) · 8.27 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
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 { Build } from "../constants";
import { deleteFiles } from "../explorerCommands/delete";
import { newJavaClass, newPackage } 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 { 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;
constructor(public readonly context: ExtensionContext) {
this._dataProvider = new DependencyDataProvider(context);
this._dependencyViewer = window.createTreeView("javaProjectExplorer", { treeDataProvider: this._dataProvider, showCollapseAll: true });
// 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 (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 (Build.FILE_NAMES.includes(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*/);
}),
);
// 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_JAVA_CLASS, async (node?: DataNode) => {
let cmdNode = getCmdNode(this._dependencyViewer.selection, node);
if (!cmdNode) {
cmdNode = await this.promptForProjectNode();
}
newJavaClass(cmdNode);
}),
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_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));
}),
);
}
public dispose(): void {
if (this._dependencyViewer) {
this._dependencyViewer.dispose();
}
}
public async reveal(uri: Uri, needCheckSyncSetting: boolean = true): Promise<void> {
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);
}
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;
}