1
>
/*---------------------------------------------------------------------------------------------
resourceTree.ts
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);
51
}
53
>
set(path: string, child: Node<T, C>): void {
54
this._children.set(path, child);
55
}
57
>
delete(path: string): void {
58
this._children.delete(path);
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') {