From 1deca969f0f8d24505374945432e1dfddc109e3d Mon Sep 17 00:00:00 2001 From: William Killerud Date: Sun, 8 Dec 2024 14:16:09 +0100 Subject: [PATCH] fix: apply customData option --- packages/language-server/src/server.ts | 84 +++++++++++++++++++------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/packages/language-server/src/server.ts b/packages/language-server/src/server.ts index 0246dd5a..fb59c12b 100644 --- a/packages/language-server/src/server.ts +++ b/packages/language-server/src/server.ts @@ -21,7 +21,7 @@ import { TextDocumentChangeEvent, } from "vscode-languageserver/node"; import { TextDocument } from "vscode-languageserver-textdocument"; -import { URI } from "vscode-uri"; +import { URI, Utils } from "vscode-uri"; import type { FileSystemProvider } from "./file-system"; import { getFileSystemProvider } from "./file-system-provider"; import { RuntimeEnvironment } from "./runtime"; @@ -34,6 +34,10 @@ import { getSassRegionsDocument } from "./embedded"; import WorkspaceScanner from "./workspace-scanner"; import { createLogger, type Logger } from "./logger"; import merge from "lodash.merge"; +import { + ICSSDataProvider, + newCSSDataProvider, +} from "@somesass/vscode-css-languageservice"; export class SomeSassServer { private readonly connection: Connection; @@ -171,6 +175,47 @@ export class SomeSassServer { return settings; }; + const applyCustomData = async ( + configuration: LanguageServerConfiguration, + ) => { + const paths: string[] = []; + if (configuration.css.customData) { + paths.push(...configuration.css.customData); + } + if (configuration.sass.customData) { + paths.push(...configuration.sass.customData); + } + if (configuration.scss.customData) { + paths.push(...configuration.scss.customData); + } + + const customDataProviders = await Promise.all( + paths.map(async (path) => { + try { + let uri = path.startsWith("/") + ? URI.parse(path) + : Utils.joinPath(workspaceRoot!, path); + + const content = await fileSystemProvider!.readFile(uri, "utf-8"); + const rawData = JSON.parse(content); + + return newCSSDataProvider({ + version: rawData.version || 1, + properties: rawData.properties || [], + atDirectives: rawData.atDirectives || [], + pseudoClasses: rawData.pseudoClasses || [], + pseudoElements: rawData.pseudoElements || [], + }); + } catch (error) { + this.log.debug(String(error)); + return newCSSDataProvider({ version: 1 }); + } + }), + ); + + ls!.setDataProviders(customDataProviders); + }; + this.connection.onInitialized(async () => { try { // Let other methods await the result of the initial scan before proceeding @@ -180,7 +225,7 @@ export class SomeSassServer { this.connection.workspace.getConfiguration("editor"), ]; - Promise.all(configurationRequests).then((configs) => { + Promise.all(configurationRequests).then(async (configs) => { if ( !ls || !clientCapabilities || @@ -200,30 +245,23 @@ export class SomeSassServer { ]; const configuration = applyConfiguration(somesass, editor); + await applyCustomData(configuration); this.log.debug("Scanning workspace for files"); - return fileSystemProvider - .findFiles( - "**/*.{css,scss,sass,svelte,astro,vue}", - configuration.workspace.exclude, - ) - .then((files) => { - this.log.debug(`Found ${files.length} files, starting parse`); - - workspaceScanner = new WorkspaceScanner( - ls!, - fileSystemProvider!, - ); - - return workspaceScanner.scan(files); - }) - .then((promises) => { - this.log.debug( - `Initial scan finished, parsed ${promises.length} files`, - ); - resolve(); - }); + const files = await fileSystemProvider.findFiles( + "**/*.{css,scss,sass,svelte,astro,vue}", + configuration.workspace.exclude, + ); + + this.log.debug(`Found ${files.length} files, starting parse`); + + workspaceScanner = new WorkspaceScanner(ls!, fileSystemProvider!); + await workspaceScanner.scan(files); + + this.log.debug( + `Initial scan finished, parsed ${files.length} files`, + ); }); }); await initialScan;