forked from microsoft/vscode-java-dependency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorerNodeCache.ts
More file actions
65 lines (53 loc) · 1.92 KB
/
Copy pathexplorerNodeCache.ts
File metadata and controls
65 lines (53 loc) · 1.92 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import { Uri } from "vscode";
import { DataNode } from "../dataNode";
import { ExplorerNode } from "../explorerNode";
import { Trie, TrieNode } from "./Trie";
class ExplorerNodeCache {
private mutableNodeCache: Trie<DataNode> = new Trie();
public getDataNode(uri: Uri): DataNode | undefined {
return this.mutableNodeCache.find(uri.fsPath)?.value;
}
/**
* Find the node whose uri is best match to the input uri, which means
* the uri of the returned node is equal to or ancestor of the input one.
* @param uri
*/
public findBestMatchNodeByUri(uri: Uri): DataNode | undefined {
const parentDir = path.dirname(uri.fsPath);
const ancestor = this.mutableNodeCache.findFirstAncestorNodeWithData(parentDir);
return ancestor?.value;
}
public saveNode(node: ExplorerNode): void {
if (node instanceof DataNode && node.uri) {
this.mutableNodeCache.insert(node);
}
}
public saveNodes(nodes: ExplorerNode[]): void {
for (const node of nodes) {
this.saveNode(node);
}
}
public removeNodeChildren(node?: ExplorerNode): void {
if (!node) {
this.removeChildren(this.mutableNodeCache.root);
return;
}
if (!(node instanceof DataNode) || !node.uri) {
return;
}
const trieNode = this.mutableNodeCache.find(Uri.parse(node.uri).fsPath);
if (trieNode) {
this.removeChildren(trieNode);
}
}
private removeChildren(trieNode: TrieNode<DataNode | undefined>) {
trieNode.children = {};
if (trieNode.value?.nodeData?.children) {
trieNode.value.nodeData.children = undefined;
}
}
}
export const explorerNodeCache: ExplorerNodeCache = new ExplorerNodeCache();