Path: blob/main/extensions/css-language-features/client/src/customData.ts
3320 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';6import { Utils } from 'vscode-uri';78export function getCustomDataSource(toDispose: Disposable[]) {9let pathsInWorkspace = getCustomDataPathsInAllWorkspaces();10let pathsInExtensions = getCustomDataPathsFromAllExtensions();1112const onChange = new EventEmitter<void>();1314toDispose.push(extensions.onDidChange(_ => {15const newPathsInExtensions = getCustomDataPathsFromAllExtensions();16if (newPathsInExtensions.length !== pathsInExtensions.length || !newPathsInExtensions.every((val, idx) => val === pathsInExtensions[idx])) {17pathsInExtensions = newPathsInExtensions;18onChange.fire();19}20}));21toDispose.push(workspace.onDidChangeConfiguration(e => {22if (e.affectsConfiguration('css.customData')) {23pathsInWorkspace = getCustomDataPathsInAllWorkspaces();24onChange.fire();25}26}));2728return {29get uris() {30return pathsInWorkspace.concat(pathsInExtensions);31},32get onDidChange() {33return onChange.event;34}35};36}373839function getCustomDataPathsInAllWorkspaces(): string[] {40const workspaceFolders = workspace.workspaceFolders;4142const dataPaths: string[] = [];4344if (!workspaceFolders) {45return dataPaths;46}4748const collect = (paths: string[] | undefined, rootFolder: Uri) => {49if (Array.isArray(paths)) {50for (const path of paths) {51if (typeof path === 'string') {52dataPaths.push(Utils.resolvePath(rootFolder, path).toString());53}54}55}56};5758for (let i = 0; i < workspaceFolders.length; i++) {59const folderUri = workspaceFolders[i].uri;60const allCssConfig = workspace.getConfiguration('css', folderUri);61const customDataInspect = allCssConfig.inspect<string[]>('customData');62if (customDataInspect) {63collect(customDataInspect.workspaceFolderValue, folderUri);64if (i === 0) {65if (workspace.workspaceFile) {66collect(customDataInspect.workspaceValue, workspace.workspaceFile);67}68collect(customDataInspect.globalValue, folderUri);69}70}7172}73return dataPaths;74}7576function getCustomDataPathsFromAllExtensions(): string[] {77const dataPaths: string[] = [];78for (const extension of extensions.all) {79const customData = extension.packageJSON?.contributes?.css?.customData;80if (Array.isArray(customData)) {81for (const rp of customData) {82dataPaths.push(Utils.joinPath(extension.extensionUri, rp).toString());83}84}85}86return dataPaths;87}888990