Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/css-language-features/client/src/customData.ts
3320 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 { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
7
import { Utils } from 'vscode-uri';
8
9
export function getCustomDataSource(toDispose: Disposable[]) {
10
let pathsInWorkspace = getCustomDataPathsInAllWorkspaces();
11
let pathsInExtensions = getCustomDataPathsFromAllExtensions();
12
13
const onChange = new EventEmitter<void>();
14
15
toDispose.push(extensions.onDidChange(_ => {
16
const newPathsInExtensions = getCustomDataPathsFromAllExtensions();
17
if (newPathsInExtensions.length !== pathsInExtensions.length || !newPathsInExtensions.every((val, idx) => val === pathsInExtensions[idx])) {
18
pathsInExtensions = newPathsInExtensions;
19
onChange.fire();
20
}
21
}));
22
toDispose.push(workspace.onDidChangeConfiguration(e => {
23
if (e.affectsConfiguration('css.customData')) {
24
pathsInWorkspace = getCustomDataPathsInAllWorkspaces();
25
onChange.fire();
26
}
27
}));
28
29
return {
30
get uris() {
31
return pathsInWorkspace.concat(pathsInExtensions);
32
},
33
get onDidChange() {
34
return onChange.event;
35
}
36
};
37
}
38
39
40
function getCustomDataPathsInAllWorkspaces(): string[] {
41
const workspaceFolders = workspace.workspaceFolders;
42
43
const dataPaths: string[] = [];
44
45
if (!workspaceFolders) {
46
return dataPaths;
47
}
48
49
const collect = (paths: string[] | undefined, rootFolder: Uri) => {
50
if (Array.isArray(paths)) {
51
for (const path of paths) {
52
if (typeof path === 'string') {
53
dataPaths.push(Utils.resolvePath(rootFolder, path).toString());
54
}
55
}
56
}
57
};
58
59
for (let i = 0; i < workspaceFolders.length; i++) {
60
const folderUri = workspaceFolders[i].uri;
61
const allCssConfig = workspace.getConfiguration('css', folderUri);
62
const customDataInspect = allCssConfig.inspect<string[]>('customData');
63
if (customDataInspect) {
64
collect(customDataInspect.workspaceFolderValue, folderUri);
65
if (i === 0) {
66
if (workspace.workspaceFile) {
67
collect(customDataInspect.workspaceValue, workspace.workspaceFile);
68
}
69
collect(customDataInspect.globalValue, folderUri);
70
}
71
}
72
73
}
74
return dataPaths;
75
}
76
77
function getCustomDataPathsFromAllExtensions(): string[] {
78
const dataPaths: string[] = [];
79
for (const extension of extensions.all) {
80
const customData = extension.packageJSON?.contributes?.css?.customData;
81
if (Array.isArray(customData)) {
82
for (const rp of customData) {
83
dataPaths.push(Utils.joinPath(extension.extensionUri, rp).toString());
84
}
85
}
86
}
87
return dataPaths;
88
}
89
90