Skip to content

Commit 52c149e

Browse files
committed
fix: clear cache when file update, and goto def
1 parent 7a7fcd0 commit 52c149e

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

server/src/server.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ColorInformation,
1414
FileChangeType,
1515
Color,
16+
Location
1617
} from 'vscode-languageserver/node';
1718

1819
import * as fs from 'fs';
@@ -26,14 +27,14 @@ import {
2627
getCSSLanguageService,
2728
getLESSLanguageService,
2829
getSCSSLanguageService,
29-
Location,
3030
} from 'vscode-css-languageservice';
3131

3232
import { Position, TextDocument } from 'vscode-languageserver-textdocument';
3333

3434
import { Symbols } from 'vscode-css-languageservice/lib/umd/parser/cssSymbolScope.js';
3535
import isColor from './utils/isColor';
3636
import { uriToPath } from './utils/protocol';
37+
import { pathToFileURL } from 'url';
3738

3839
// Create a connection for the server, using Node's IPC as a transport.
3940
// Also include all preview / proposed LSP features.
@@ -106,6 +107,13 @@ export function culoriColorToVscodeColor(color: culori.Color): Color {
106107
return { red: rgb.r, green: rgb.g, blue: rgb.b, alpha: rgb.alpha ?? 1 };
107108
}
108109

110+
const clearFileCache = (filePath: string) => {
111+
cachedVariables[filePath]?.forEach((_, key) => {
112+
cachedVariables['all']?.delete(key);
113+
});
114+
cachedVariables[filePath]?.clear();
115+
};
116+
109117
const parseCSSVariablesFromText = ({
110118
content,
111119
filePath,
@@ -114,12 +122,17 @@ const parseCSSVariablesFromText = ({
114122
filePath: string
115123
}) => {
116124
try {
125+
// reset cache for this file
126+
clearFileCache(filePath);
127+
117128
const fileExtension = path.extname(filePath);
118129
const languageService = getLanguageService(fileExtension);
119130
const service = languageService();
120131

132+
const fileURI = pathToFileURL(filePath).toString();
133+
121134
const document = TextDocument.create(
122-
`file:///${filePath}`,
135+
fileURI,
123136
'css',
124137
0,
125138
content
@@ -129,14 +142,6 @@ const parseCSSVariablesFromText = ({
129142

130143
const symbolContext = new Symbols(stylesheet);
131144

132-
// const documentColors = service.findDocumentColors(document, stylesheet);
133-
134-
// const result: ColorInformation[] = []
135-
// ;(stylesheet as any).accept((node: any) => {
136-
// console.log('node', node);
137-
// return true;
138-
// });
139-
140145
symbolContext.global.symbols.forEach((symbol: CSSSymbol) => {
141146
if (symbol.name.startsWith('--')) {
142147
if (!cachedVariables[filePath]) {
@@ -149,7 +154,7 @@ const parseCSSVariablesFromText = ({
149154
const variable: CSSVariable = {
150155
symbol,
151156
definition: {
152-
uri: `file:///${filePath}`,
157+
uri: fileURI,
153158
range: Range.create(
154159
document.positionAt(symbol.node.offset),
155160
document.positionAt(symbol.node.end)
@@ -169,6 +174,8 @@ const parseCSSVariablesFromText = ({
169174
cachedVariables[filePath].set(symbol.name, variable);
170175
}
171176
});
177+
178+
console.log(cachedVariables);
172179
} catch (error) {
173180
console.error(error);
174181
}
@@ -336,10 +343,7 @@ connection.onDidChangeWatchedFiles((_change) => {
336343
if (filePath) {
337344
// remove variables from cache
338345
if (change.type === FileChangeType.Deleted && cachedVariables[filePath]) {
339-
cachedVariables[filePath]?.forEach((_, key) => {
340-
cachedVariables['all']?.delete(key);
341-
});
342-
cachedVariables[filePath].clear();
346+
clearFileCache(filePath);
343347
} else {
344348
const content = fs.readFileSync(filePath, 'utf8');
345349
parseCSSVariablesFromText({
@@ -355,7 +359,6 @@ connection.onDidChangeWatchedFiles((_change) => {
355359
connection.onCompletion(
356360
(_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
357361
const doc = documents.get(_textDocumentPosition.textDocument.uri);
358-
359362
if (!doc) {
360363
return [];
361364
}
@@ -389,6 +392,8 @@ connection.onCompletion(
389392
items.push(completion);
390393
});
391394

395+
console.log("this is complete", items);
396+
392397
return items;
393398
}
394399
);

0 commit comments

Comments
 (0)