Skip to content

Commit

Permalink
fix svelte.plugin.css.globals glob
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Oct 27, 2024
1 parent 85bc1b5 commit fc7e77b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
},
"devDependencies": {
"@types/estree": "^0.0.42",
"@types/globrex": "^0.1.4",
"@types/lodash": "^4.14.116",
"@types/mocha": "^9.1.0",
"@types/node": "^18.0.0",
"@types/sinon": "^7.5.2",
"cross-env": "^7.0.2",
"globrex": "^0.1.2",
"mocha": "^9.2.0",
"sinon": "^11.0.0",
"ts-node": "^10.0.0"
Expand Down
7 changes: 6 additions & 1 deletion packages/language-server/src/plugins/css/CSSPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { StyleAttributeDocument } from './StyleAttributeDocument';
import { getDocumentContext } from '../documentContext';
import { FoldingRange, FoldingRangeKind } from 'vscode-languageserver-types';
import { indentBasedFoldingRangeForTag } from '../../lib/foldingRange/indentFolding';
import { isNotNullOrUndefined, urlToPath } from '../../utils';

export class CSSPlugin
implements
Expand All @@ -68,7 +69,7 @@ export class CSSPlugin
private cssLanguageServices: CSSLanguageServices;
private workspaceFolders: WorkspaceFolder[];
private triggerCharacters = ['.', ':', '-', '/'];
private globalVars = new GlobalVars();
private globalVars: GlobalVars;

constructor(
docManager: DocumentManager,
Expand All @@ -80,6 +81,10 @@ export class CSSPlugin
this.workspaceFolders = workspaceFolders;
this.configManager = configManager;
this.updateConfigs();
const workspacePaths = workspaceFolders
.map((folder) => urlToPath(folder.uri))
.filter(isNotNullOrUndefined);
this.globalVars = new GlobalVars(workspacePaths);

this.globalVars.watchFiles(this.configManager.get('css.globals'));
this.configManager.onChange((config) => {
Expand Down
51 changes: 47 additions & 4 deletions packages/language-server/src/plugins/css/global-vars.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { watch, FSWatcher } from 'chokidar';
import { FSWatcher, watch } from 'chokidar';
import { readFile } from 'fs';
import { isNotNullOrUndefined, flatten } from '../../utils';
import globrex from 'globrex';
import { join } from 'path';
import { flatten, isNotNullOrUndefined, normalizePath } from '../../utils';

const varRegex = /^\s*(--\w+.*?):\s*?([^;]*)/;

Expand All @@ -12,26 +14,67 @@ export interface GlobalVar {

export class GlobalVars {
private fsWatcher?: FSWatcher;
private watchedFiles: string | undefined;
private globalVars = new Map<string, GlobalVar[]>();
private readonly workspaceRoot: string[];

constructor(workspaceRoot: string[]) {
this.workspaceRoot = workspaceRoot;
}

watchFiles(filesToWatch: string): void {
if (!filesToWatch) {
if (!filesToWatch || this.watchedFiles === filesToWatch) {
return;
}

this.watchedFiles = filesToWatch;
if (this.fsWatcher) {
this.fsWatcher.close();
this.globalVars.clear();
}

this.fsWatcher = watch(filesToWatch.split(','))
const paths = new Set<string>();
const includePatterns = new Set<string>();

for (const root of this.workspaceRoot) {
for (const filePath of filesToWatch.split(',')) {
if (!filePath.includes('*')) {
paths.add(filePath);
continue;
}

const normalizedPath = normalizePath(join(root, filePath));
includePatterns.add(normalizedPath);
const pathSegments = normalizedPath.split('**');
let directory = pathSegments[0] || '.';
paths.add(directory);
}
}

this.fsWatcher = watch(Array.from(paths), {
ignored: this.createIgnoreMatcher(includePatterns)
})
.addListener('add', (file) => this.updateForFile(file))
.addListener('change', (file) => {
this.updateForFile(file);
})
.addListener('unlink', (file) => this.globalVars.delete(file));
}

private createIgnoreMatcher(includePatterns: Set<string>) {
if (includePatterns.size === 0) {
return undefined;
}

const regexList = Array.from(includePatterns).map(
(pattern) => globrex(pattern, { globstar: true }).regex
);

return (path: string) => {
return !regexList.some((regex) => regex.test(path));
};
}

private updateForFile(filename: string) {
// Inside a small timeout because it seems chikidar is "too fast"
// and reading the file will then return empty content
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc7e77b

Please sign in to comment.