-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathsyncHandler.ts
More file actions
142 lines (121 loc) · 5.25 KB
/
syncHandler.ts
File metadata and controls
142 lines (121 loc) · 5.25 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import * as fse from "fs-extra";
import { commands, Disposable, FileSystemWatcher, RelativePattern, Uri, workspace } from "vscode";
import { instrumentOperation } from "vscode-extension-telemetry-wrapper";
import { Commands } from "./commands";
import { NodeKind } from "./java/nodeData";
import { languageServerApiManager } from "./languageServerApi/languageServerApiManager";
import { Settings } from "./settings";
import { DataNode } from "./views/dataNode";
import { ExplorerNode } from "./views/explorerNode";
import { explorerNodeCache } from "./views/nodeCache/explorerNodeCache";
const ENABLE_AUTO_REFRESH: string = "java.view.package.enableAutoRefresh";
const DISABLE_AUTO_REFRESH: string = "java.view.package.disableAutoRefresh";
class SyncHandler implements Disposable {
private disposables: Disposable[] = [];
public updateFileWatcher(autoRefresh?: boolean): void {
this.dispose();
if (autoRefresh) {
instrumentOperation(ENABLE_AUTO_REFRESH, () => this.enableAutoRefresh())();
} else {
instrumentOperation(DISABLE_AUTO_REFRESH, () => {})();
}
}
public dispose(): void {
for (const disposable of this.disposables) {
if (disposable) {
disposable.dispose();
}
}
this.disposables = [];
}
private async enableAutoRefresh() {
if (!await languageServerApiManager.ready()) {
return;
}
this.disposables.push(workspace.onDidChangeWorkspaceFolders(() => {
this.refresh();
}));
try {
const result: IListCommandResult | undefined = await commands.executeCommand<IListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND,
Commands.JAVA_PROJECT_LIST_SOURCE_PATHS);
if (!result || !result.status || !result.data || result.data.length === 0) {
throw new Error("Failed to list the source paths");
}
for (const sourcePathData of result.data) {
const normalizedPath: string = Uri.file(sourcePathData.path).fsPath;
if (!(await fse.pathExists(normalizedPath))) {
continue;
}
const pattern: RelativePattern = new RelativePattern(normalizedPath, "**/*");
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher(pattern);
this.disposables.push(watcher);
this.setupWatchers(watcher);
}
} catch (e) {
const fileSystemWatcher: FileSystemWatcher = workspace.createFileSystemWatcher("**/{*.java,src/**}");
this.disposables.push(fileSystemWatcher);
this.setupWatchers(fileSystemWatcher);
}
}
private setupWatchers(watcher: FileSystemWatcher): void {
this.disposables.push(watcher.onDidChange((uri: Uri) => {
if (path.extname(uri.fsPath) !== ".java" || !Settings.showMembers()) {
return;
}
const node: DataNode | undefined = explorerNodeCache.getDataNode(uri);
this.refresh(node);
}));
this.disposables.push(watcher.onDidCreate((uri: Uri) => {
this.refresh(this.getParentNodeInExplorer(uri));
}));
this.disposables.push(watcher.onDidDelete((uri: Uri) => {
this.refresh(this.getParentNodeInExplorer(uri));
}));
}
private getParentNodeInExplorer(uri: Uri): ExplorerNode | undefined {
let node: DataNode | undefined = explorerNodeCache.findBestMatchNodeByUri(uri);
if (!node) {
return undefined;
}
if (Settings.isHierarchicalView()) {
// TODO: has to get the hierarchical package root node due to the java side implementation
// because currently it will only get the types for a package node but no child packages.
while (node && node.nodeData.kind !== NodeKind.PackageRoot) {
node = <DataNode>node.getParent();
}
return node;
} else {
// in flat view
if (path.extname(uri.fsPath) === ".java" && node.uri &&
Uri.parse(node.uri).fsPath === path.dirname(uri.fsPath)) {
// if the returned node is direct parent of the input uri, refresh it.
return node;
} else {
// the direct parent is not rendered in the explorer, the returned node
// is other package fragment, we need to refresh the package fragment root.
while (node && node.nodeData.kind > NodeKind.PackageRoot) {
node = <DataNode>node.getParent();
}
return node;
}
}
}
private refresh(node?: ExplorerNode): void {
commands.executeCommand(Commands.VIEW_PACKAGE_INTERNAL_REFRESH, /* debounce = */true, node);
}
}
interface ISourcePath {
path: string;
displayPath: string;
projectName: string;
projectType: string;
}
interface IListCommandResult {
status: boolean;
message: string;
data?: ISourcePath[];
}
export const syncHandler: SyncHandler = new SyncHandler();