src/vs/base/common/resourceTree.ts

187 LOC · 144 covered · 43 uncovered · 25 ranges · 3 concepts · 3 introducers · 3 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- resourceTree.ts ×15
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { memoize } from './decorators.js';
7 > import { PathIterator } from './ternarySearchTree.js';
8 > import * as paths from './path.js';
9 > import { extUri as defaultExtUri, IExtUri } from './resources.js';
10 > import { URI } from './uri.js';
11 >
12 > export interface IResourceNode<T, C = void> {
13 > readonly uri: URI;
14 > readonly relativePath: string;
15 > readonly name: string;
16 > readonly element: T | undefined;
17 > readonly children: Iterable<IResourceNode<T, C>>;
18 > readonly childrenCount: number;
19 > readonly parent: IResourceNode<T, C> | undefined;
20 > readonly context: C;
21 > get(childName: string): IResourceNode<T, C> | undefined;
22 > }
23 >
24 > class Node<T, C> implements IResourceNode<T, C> {
25 >
26 > private _children = new Map<string, Node<T, C>>();
27 >
28 > get childrenCount(): number {
29 > return this._children.size;
30 > }
31 >
32 > get children(): Iterable<Node<T, C>> {
33 return this._children.values();
34 }
36 > @memoize
37 > get name(): string {
38 return paths.posix.basename(this.relativePath);
39 }
41 > constructor(
42 > readonly uri: URI,
43 > readonly relativePath: string,
44 > readonly context: C,
45 > public element: T | undefined = undefined,
46 > readonly parent: IResourceNode<T, C> | undefined = undefined
47 > ) { }
48 >
49 > get(path: string): Node<T, C> | undefined {
50 > return this._children.get(path); resourceTree.ts ×9
51 > }
53 > set(path: string, child: Node<T, C>): void {
54 > this._children.set(path, child); resourceTree.ts ×9
55 > }
57 > delete(path: string): void {
58 > this._children.delete(path); resourceTree.ts ×9
59 > }
61 > clear(): void {
62 this._children.clear();
63 }
65 >
66 function collect<T, C>(node: IResourceNode<T, C>, result: T[]): T[] {
67 if (typeof node.element !== 'undefined') {
68 result.push(node.element);
69 }
70
71 for (const child of node.children) {
72 collect(child, result);
73 }
74
75 return result;
76 }
78 > export class ResourceTree<T extends NonNullable<unknown>, C> {
79 >
80 > readonly root: Node<T, C>;
81 >
82 > static getRoot<T, C>(node: IResourceNode<T, C>): IResourceNode<T, C> {
83 > while (node.parent) {
84 > node = node.parent;
85 > }
86 >
87 > return node;
88 > }
89 >
90 > static collect<T, C>(node: IResourceNode<T, C>): T[] {
91 return collect(node, []);
92 }
94 > static isResourceNode<T, C>(obj: unknown): obj is IResourceNode<T, C> {
95 return obj instanceof Node;
96 }
98 > constructor(context: C, rootURI: URI = URI.file('/'), private extUri: IExtUri = defaultExtUri) {
99 > this.root = new Node(rootURI, '', context);
100 > }
101 >
102 > add(uri: URI, element: T): void {
103 > const key = this.extUri.relativePath(this.root.uri, uri) || uri.path; resourceTree.ts ×9
104 > const iterator = new PathIterator(false).reset(key);
105 > let node = this.root;
106 > let path = '';
107 >
108 > while (true) {
109 > const name = iterator.value();
110 > path = path + '/' + name;
111 >
112 > let child = node.get(name);
113 >
114 > if (!child) {
115 > child = new Node(
116 > this.extUri.joinPath(this.root.uri, path),
117 > path,
118 > this.root.context,
119 > iterator.hasNext() ? undefined : element,
120 > node
121 > );
122 >
123 > node.set(name, child);
124 > } else if (!iterator.hasNext()) {
125 child.element = element;
126 }
128 > node = child;
129 >
130 > if (!iterator.hasNext()) {
131 > return;
132 > }
133 >
134 > iterator.next();
135 > }
136 > }
138 > delete(uri: URI): T | undefined {
139 > const key = this.extUri.relativePath(this.root.uri, uri) || uri.path; resourceTree.ts ×9
140 > const iterator = new PathIterator(false).reset(key);
141 > return this._delete(this.root, iterator);
142 > }
144 > private _delete(node: Node<T, C>, iterator: PathIterator): T | undefined {
145 > const name = iterator.value(); resourceTree.ts ×9
146 > const child = node.get(name);
147 >
148 > if (!child) {
149 return undefined;
150 }
152 > if (iterator.hasNext()) {
153 > const result = this._delete(child, iterator.next()); resourceTree.ts ×1
154 >
155 > if (typeof result !== 'undefined' && child.childrenCount === 0) {
156 > node.delete(name);
157 > }
158 >
159 > return result;
160 > }
162 > node.delete(name);
163 > return child.element;
164 > }
166 > clear(): void {
167 this.root.clear();
168 }
170 > getNode(uri: URI): IResourceNode<T, C> | undefined {
171 const key = this.extUri.relativePath(this.root.uri, uri) || uri.path;
172 const iterator = new PathIterator(false).reset(key);
173 let node = this.root;
174
175 while (true) {
176 const name = iterator.value();
177 const child = node.get(name);
178
179 if (!child || !iterator.hasNext()) {
180 return child;
181 }
182
183 node = child;
184 iterator.next();
185 }
186 }