@@ -13,6 +13,7 @@ import {
13
13
ColorInformation ,
14
14
FileChangeType ,
15
15
Color ,
16
+ Location
16
17
} from 'vscode-languageserver/node' ;
17
18
18
19
import * as fs from 'fs' ;
@@ -26,14 +27,14 @@ import {
26
27
getCSSLanguageService ,
27
28
getLESSLanguageService ,
28
29
getSCSSLanguageService ,
29
- Location ,
30
30
} from 'vscode-css-languageservice' ;
31
31
32
32
import { Position , TextDocument } from 'vscode-languageserver-textdocument' ;
33
33
34
34
import { Symbols } from 'vscode-css-languageservice/lib/umd/parser/cssSymbolScope.js' ;
35
35
import isColor from './utils/isColor' ;
36
36
import { uriToPath } from './utils/protocol' ;
37
+ import { pathToFileURL } from 'url' ;
37
38
38
39
// Create a connection for the server, using Node's IPC as a transport.
39
40
// Also include all preview / proposed LSP features.
@@ -106,6 +107,13 @@ export function culoriColorToVscodeColor(color: culori.Color): Color {
106
107
return { red : rgb . r , green : rgb . g , blue : rgb . b , alpha : rgb . alpha ?? 1 } ;
107
108
}
108
109
110
+ const clearFileCache = ( filePath : string ) => {
111
+ cachedVariables [ filePath ] ?. forEach ( ( _ , key ) => {
112
+ cachedVariables [ 'all' ] ?. delete ( key ) ;
113
+ } ) ;
114
+ cachedVariables [ filePath ] ?. clear ( ) ;
115
+ } ;
116
+
109
117
const parseCSSVariablesFromText = ( {
110
118
content,
111
119
filePath,
@@ -114,12 +122,17 @@ const parseCSSVariablesFromText = ({
114
122
filePath : string
115
123
} ) => {
116
124
try {
125
+ // reset cache for this file
126
+ clearFileCache ( filePath ) ;
127
+
117
128
const fileExtension = path . extname ( filePath ) ;
118
129
const languageService = getLanguageService ( fileExtension ) ;
119
130
const service = languageService ( ) ;
120
131
132
+ const fileURI = pathToFileURL ( filePath ) . toString ( ) ;
133
+
121
134
const document = TextDocument . create (
122
- `file:/// ${ filePath } ` ,
135
+ fileURI ,
123
136
'css' ,
124
137
0 ,
125
138
content
@@ -129,14 +142,6 @@ const parseCSSVariablesFromText = ({
129
142
130
143
const symbolContext = new Symbols ( stylesheet ) ;
131
144
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
-
140
145
symbolContext . global . symbols . forEach ( ( symbol : CSSSymbol ) => {
141
146
if ( symbol . name . startsWith ( '--' ) ) {
142
147
if ( ! cachedVariables [ filePath ] ) {
@@ -149,7 +154,7 @@ const parseCSSVariablesFromText = ({
149
154
const variable : CSSVariable = {
150
155
symbol,
151
156
definition : {
152
- uri : `file:/// ${ filePath } ` ,
157
+ uri : fileURI ,
153
158
range : Range . create (
154
159
document . positionAt ( symbol . node . offset ) ,
155
160
document . positionAt ( symbol . node . end )
@@ -169,6 +174,8 @@ const parseCSSVariablesFromText = ({
169
174
cachedVariables [ filePath ] . set ( symbol . name , variable ) ;
170
175
}
171
176
} ) ;
177
+
178
+ console . log ( cachedVariables ) ;
172
179
} catch ( error ) {
173
180
console . error ( error ) ;
174
181
}
@@ -336,10 +343,7 @@ connection.onDidChangeWatchedFiles((_change) => {
336
343
if ( filePath ) {
337
344
// remove variables from cache
338
345
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 ) ;
343
347
} else {
344
348
const content = fs . readFileSync ( filePath , 'utf8' ) ;
345
349
parseCSSVariablesFromText ( {
@@ -355,7 +359,6 @@ connection.onDidChangeWatchedFiles((_change) => {
355
359
connection . onCompletion (
356
360
( _textDocumentPosition : TextDocumentPositionParams ) : CompletionItem [ ] => {
357
361
const doc = documents . get ( _textDocumentPosition . textDocument . uri ) ;
358
-
359
362
if ( ! doc ) {
360
363
return [ ] ;
361
364
}
@@ -389,6 +392,8 @@ connection.onCompletion(
389
392
items . push ( completion ) ;
390
393
} ) ;
391
394
395
+ console . log ( "this is complete" , items ) ;
396
+
392
397
return items ;
393
398
}
394
399
) ;
0 commit comments