Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/markdown-language-features/src/util/resourceMap.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
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 * as vscode from 'vscode';
7
8
type ResourceToKey = (uri: vscode.Uri) => string;
9
10
const defaultResourceToKey = (resource: vscode.Uri): string => resource.toString();
11
12
export class ResourceMap<T> {
13
14
private readonly _map = new Map<string, { readonly uri: vscode.Uri; readonly value: T }>();
15
16
private readonly _toKey: ResourceToKey;
17
18
constructor(toKey: ResourceToKey = defaultResourceToKey) {
19
this._toKey = toKey;
20
}
21
22
public set(uri: vscode.Uri, value: T): this {
23
this._map.set(this._toKey(uri), { uri, value });
24
return this;
25
}
26
27
public get(resource: vscode.Uri): T | undefined {
28
return this._map.get(this._toKey(resource))?.value;
29
}
30
31
public has(resource: vscode.Uri): boolean {
32
return this._map.has(this._toKey(resource));
33
}
34
35
public get size(): number {
36
return this._map.size;
37
}
38
39
public clear(): void {
40
this._map.clear();
41
}
42
43
public delete(resource: vscode.Uri): boolean {
44
return this._map.delete(this._toKey(resource));
45
}
46
47
public *values(): IterableIterator<T> {
48
for (const entry of this._map.values()) {
49
yield entry.value;
50
}
51
}
52
53
public *keys(): IterableIterator<vscode.Uri> {
54
for (const entry of this._map.values()) {
55
yield entry.uri;
56
}
57
}
58
59
public *entries(): IterableIterator<[vscode.Uri, T]> {
60
for (const entry of this._map.values()) {
61
yield [entry.uri, entry.value];
62
}
63
}
64
65
public [Symbol.iterator](): IterableIterator<[vscode.Uri, T]> {
66
return this.entries();
67
}
68
}
69
70