forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncHandler.ts
More file actions
103 lines (86 loc) · 3.83 KB
/
Copy pathsyncHandler.ts
File metadata and controls
103 lines (86 loc) · 3.83 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import { commands, Disposable, FileSystemWatcher, Uri, workspace } from "vscode";
import { instrumentOperation } from "vscode-extension-telemetry-wrapper";
import { Commands } from "./commands";
import { NodeKind } from "./java/nodeData";
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 {
if (autoRefresh) {
instrumentOperation(ENABLE_AUTO_REFRESH, () => this.enableAutoRefresh())();
} else {
instrumentOperation(DISABLE_AUTO_REFRESH, () => this.dispose())();
}
}
public dispose(): void {
for (const disposable of this.disposables) {
if (disposable) {
disposable.dispose();
}
}
this.disposables = [];
}
private async enableAutoRefresh() {
this.disposables.push(workspace.onDidChangeWorkspaceFolders(() => {
this.refresh();
}));
const fileSystemWatcher: FileSystemWatcher = workspace.createFileSystemWatcher("**/{*.java,src/**}");
this.setupWatchers(fileSystemWatcher);
this.disposables.push(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_REFRESH, /* debounce = */true, node);
}
}
export const syncHandler: SyncHandler = new SyncHandler();