-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathcustomData.ts
143 lines (124 loc) · 4.22 KB
/
customData.ts
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
import { Runtime } from './htmlClient';
import { Utils } from 'vscode-uri';
export function getCustomDataSource(runtime: Runtime, toDispose: Disposable[]) {
let localExtensionUris = new Set<string>();
let externalExtensionUris = new Set<string>();
const workspaceUris = new Set<string>();
collectInWorkspaces(workspaceUris);
collectInExtensions(localExtensionUris, externalExtensionUris);
const onChange = new EventEmitter<void>();
toDispose.push(extensions.onDidChange(_ => {
const newLocalExtensionUris = new Set<string>();
const newExternalExtensionUris = new Set<string>();
collectInExtensions(newLocalExtensionUris, newExternalExtensionUris);
if (hasChanges(newLocalExtensionUris, localExtensionUris) || hasChanges(newExternalExtensionUris, externalExtensionUris)) {
localExtensionUris = newLocalExtensionUris;
externalExtensionUris = newExternalExtensionUris;
onChange.fire();
}
}));
toDispose.push(workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('html.customData')) {
workspaceUris.clear();
collectInWorkspaces(workspaceUris);
onChange.fire();
}
}));
toDispose.push(workspace.onDidChangeTextDocument(e => {
const path = e.document.uri.toString();
if (externalExtensionUris.has(path) || workspaceUris.has(path)) {
onChange.fire();
}
}));
return {
get uris() {
return [...localExtensionUris].concat([...externalExtensionUris], [...workspaceUris]);
},
get onDidChange() {
return onChange.event;
},
getContent(uriString: string): Thenable<string> {
const uri = Uri.parse(uriString);
if (localExtensionUris.has(uriString)) {
return workspace.fs.readFile(uri).then(buffer => {
return new runtime.TextDecoder().decode(buffer);
});
}
return workspace.openTextDocument(uri).then(doc => {
return doc.getText();
});
}
};
}
function hasChanges(s1: Set<string>, s2: Set<string>) {
if (s1.size !== s2.size) {
return true;
}
for (const uri of s1) {
if (!s2.has(uri)) {
return true;
}
}
return false;
}
function isURI(uriOrPath: string) {
return /^(?<scheme>\w[\w\d+.-]*):/.test(uriOrPath);
}
function collectInWorkspaces(workspaceUris: Set<string>): Set<string> {
const workspaceFolders = workspace.workspaceFolders;
const dataPaths = new Set<string>();
if (!workspaceFolders) {
return dataPaths;
}
const collect = (uriOrPaths: string[] | undefined, rootFolder: Uri) => {
if (Array.isArray(uriOrPaths)) {
for (const uriOrPath of uriOrPaths) {
if (typeof uriOrPath === 'string') {
if (!isURI(uriOrPath)) {
// path in the workspace
workspaceUris.add(Utils.resolvePath(rootFolder, uriOrPath).toString());
} else {
// external uri
workspaceUris.add(uriOrPath);
}
}
}
}
};
for (let i = 0; i < workspaceFolders.length; i++) {
const folderUri = workspaceFolders[i].uri;
const allHtmlConfig = workspace.getConfiguration('html', folderUri);
const customDataInspect = allHtmlConfig.inspect<string[]>('customData');
if (customDataInspect) {
collect(customDataInspect.workspaceFolderValue, folderUri);
if (i === 0) {
if (workspace.workspaceFile) {
collect(customDataInspect.workspaceValue, workspace.workspaceFile);
}
collect(customDataInspect.globalValue, folderUri);
}
}
}
return dataPaths;
}
function collectInExtensions(localExtensionUris: Set<string>, externalUris: Set<string>): void {
for (const extension of extensions.allAcrossExtensionHosts) {
const customData = extension.packageJSON?.contributes?.html?.customData;
if (Array.isArray(customData)) {
for (const uriOrPath of customData) {
if (!isURI(uriOrPath)) {
// relative path in an extension
localExtensionUris.add(Uri.joinPath(extension.extensionUri, uriOrPath).toString());
} else {
// external uri
externalUris.add(uriOrPath);
}
}
}
}
}