@@ -13,7 +13,8 @@ import {
1313 ColorInformation ,
1414 FileChangeType ,
1515 Color ,
16- Location
16+ Location ,
17+ Hover ,
1718} from 'vscode-languageserver/node' ;
1819
1920import * as fs from 'fs' ;
@@ -59,7 +60,7 @@ type CSSVariable = {
5960 color ?: Color
6061}
6162
62- const cachedVariables : Record < string , Map < string , CSSVariable > > = { } ;
63+ let cachedVariables : Record < string , Map < string , CSSVariable > > = { } ;
6364
6465export const getLanguageService = ( fileExtension : string ) => {
6566 switch ( fileExtension ) {
@@ -114,6 +115,11 @@ const clearFileCache = (filePath: string) => {
114115 cachedVariables [ filePath ] ?. clear ( ) ;
115116} ;
116117
118+ const clearAllCache = ( ) => {
119+ cachedVariables [ 'all' ] ?. clear ( ) ;
120+ cachedVariables = { } ;
121+ } ;
122+
117123const parseCSSVariablesFromText = ( {
118124 content,
119125 filePath,
@@ -131,12 +137,7 @@ const parseCSSVariablesFromText = ({
131137
132138 const fileURI = pathToFileURL ( filePath ) . toString ( ) ;
133139
134- const document = TextDocument . create (
135- fileURI ,
136- 'css' ,
137- 0 ,
138- content
139- ) ;
140+ const document = TextDocument . create ( fileURI , 'css' , 0 , content ) ;
140141
141142 const stylesheet = service . parseStylesheet ( document ) ;
142143
@@ -174,8 +175,6 @@ const parseCSSVariablesFromText = ({
174175 cachedVariables [ filePath ] . set ( symbol . name , variable ) ;
175176 }
176177 } ) ;
177-
178- console . log ( cachedVariables ) ;
179178 } catch ( error ) {
180179 console . error ( error ) ;
181180 }
@@ -205,14 +204,6 @@ const parseAndSyncVariables = (
205204
206205connection . onInitialize ( async ( params : InitializeParams ) => {
207206 const capabilities = params . capabilities ;
208- const settings = await getDocumentSettings ( ) ;
209-
210- const validFolders = params . workspaceFolders
211- ?. map ( ( folder ) => uriToPath ( folder . uri ) || '' )
212- . filter ( ( path ) => ! ! path ) ;
213-
214- // parse and sync variables
215- parseAndSyncVariables ( validFolders || [ ] , settings ) ;
216207
217208 // Does the client support the `workspace/configuration` request?
218209 // If not, we fall back using global settings.
@@ -236,9 +227,11 @@ connection.onInitialize(async (params: InitializeParams) => {
236227 resolveProvider : true ,
237228 } ,
238229 definitionProvider : true ,
230+ hoverProvider : true ,
239231 colorProvider : true ,
240232 } ,
241233 } ;
234+
242235 if ( hasWorkspaceFolderCapability ) {
243236 result . capabilities . workspace = {
244237 workspaceFolders : {
@@ -249,7 +242,7 @@ connection.onInitialize(async (params: InitializeParams) => {
249242 return result ;
250243} ) ;
251244
252- connection . onInitialized ( ( ) => {
245+ connection . onInitialized ( async ( ) => {
253246 if ( hasConfigurationCapability ) {
254247 // Register for all configuration changes.
255248 connection . client . register (
@@ -262,6 +255,16 @@ connection.onInitialized(() => {
262255 connection . console . log ( 'Workspace folder change event received.' ) ;
263256 } ) ;
264257 }
258+
259+ const workspaceFolders = await connection . workspace . getWorkspaceFolders ( ) ;
260+ const validFolders = workspaceFolders
261+ ?. map ( ( folder ) => uriToPath ( folder . uri ) || '' )
262+ . filter ( ( path ) => ! ! path ) ;
263+
264+ const settings = await getDocumentSettings ( ) ;
265+
266+ // parse and sync variables
267+ parseAndSyncVariables ( validFolders || [ ] , settings ) ;
265268} ) ;
266269
267270// The example settings
@@ -297,6 +300,7 @@ connection.onDidChangeConfiguration(async (change) => {
297300 if ( hasConfigurationCapability ) {
298301 // Reset all cached document settings
299302 documentSettings . clear ( ) ;
303+ clearAllCache ( ) ;
300304
301305 const validFolders = await connection . workspace
302306 . getWorkspaceFolders ( )
@@ -392,8 +396,6 @@ connection.onCompletion(
392396 items . push ( completion ) ;
393397 } ) ;
394398
395- console . log ( "this is complete" , items ) ;
396-
397399 return items ;
398400 }
399401) ;
@@ -417,7 +419,6 @@ export function indexToPosition(str: string, index: number): Position {
417419 const data = lineColumn ( str + '\n' , index ) ;
418420
419421 if ( ! data ) {
420- console . log ( str , index ) ;
421422 return { line : 0 , character : 0 } ;
422423 }
423424 const { line, col } = data ;
@@ -437,13 +438,9 @@ connection.onDocumentColor((params): ColorInformation[] => {
437438
438439 const globalStart : Position = { line : 0 , character : 0 } ;
439440
440-
441441 matches . map ( ( match ) => {
442442 const start = indexToPosition ( text , match . index + 4 ) ;
443- const end = indexToPosition (
444- text ,
445- match . index + match [ 0 ] . length
446- ) ;
443+ const end = indexToPosition ( text , match . index + match [ 0 ] . length ) ;
447444
448445 const cssVariable = cachedVariables [ 'all' ] ?. get ( match . groups . varName ) ;
449446
@@ -471,12 +468,36 @@ connection.onDocumentColor((params): ColorInformation[] => {
471468 return colors ;
472469} ) ;
473470
471+ connection . onHover ( ( params ) => {
472+ const doc = documents . get ( params . textDocument . uri ) ;
473+
474+ if ( ! doc ) {
475+ return null ;
476+ }
477+
478+ const offset = doc . offsetAt ( params . position ) ;
479+ const currentWord = getCurrentWord ( doc , offset ) ;
480+
481+ if ( ! currentWord ) return null ;
482+
483+ const nornalizedWord = currentWord . slice ( 1 ) ;
484+
485+ const cssVariable = cachedVariables [ 'all' ] ?. get ( nornalizedWord ) ;
486+
487+ if ( cssVariable ) {
488+ return {
489+ contents : cssVariable . symbol . value ,
490+ range : cssVariable . definition . range ,
491+ } as Hover ;
492+ }
493+
494+ return null ;
495+ } ) ;
496+
474497connection . onColorPresentation ( ( params ) => {
475498 const document = documents . get ( params . textDocument . uri ) ;
476-
477- const className = document . getText ( params . range ) ;
478499
479- connection . console . log ( 'on color presentation ' + className ) ;
500+ const className = document . getText ( params . range ) ;
480501 if ( ! className ) {
481502 return [ ] ;
482503 }
@@ -496,15 +517,13 @@ connection.onDefinition((params) => {
496517 if ( ! currentWord ) return null ;
497518
498519 const nornalizedWord = currentWord . slice ( 1 ) ;
499-
500- const definition = null ;
501520 const cssVariable = cachedVariables [ 'all' ] ?. get ( nornalizedWord ) ;
502521
503522 if ( cssVariable ) {
504523 return cssVariable . definition ;
505524 }
506525
507- return definition ;
526+ return null ;
508527} ) ;
509528
510529// Make the text document manager listen on the connection
0 commit comments