@@ -21,15 +21,99 @@ import { registerReferences } from './capabilities/references'
2121import { clearAllCache , clearDocumentCache , getMatchedPositionsFromDoc } from './core/cache'
2222import { ContextManager } from './core/context'
2323import { defaultSettings } from './types'
24+ import { resolveWorkspaceRoots } from './utils/roots'
2425
2526const connection = createConnection ( ProposedFeatures . all )
2627const documents = new TextDocuments ( TextDocument )
2728
29+ interface UnocssSettingsInput extends Partial < Omit < ServerSettings , 'autocompleteMatchType' | 'autocompleteStrict' | 'autocompleteMaxItems' > > {
30+ autocomplete ?: {
31+ matchType ?: ServerSettings [ 'autocompleteMatchType' ]
32+ strict ?: boolean
33+ maxItems ?: number
34+ }
35+ }
36+
37+ interface WorkspaceInitializationOptions {
38+ workspaceFileDir ?: string
39+ workspaceFolderPaths ?: string [ ]
40+ }
41+
42+ interface WorkspaceContextState {
43+ rootPath ?: string
44+ fileDir ?: string
45+ folderPaths : string [ ]
46+ }
47+
2848let settings : ServerSettings = { ...defaultSettings }
2949let contextManager : ContextManager
3050let hasWatchedFilesCapability = false
3151let serverInitialized = false
3252let watcherDisposable : Disposable | undefined
53+ const workspaceContext : WorkspaceContextState = {
54+ folderPaths : [ ] ,
55+ }
56+
57+ function mergeSettings ( unocssSettings : UnocssSettingsInput = { } ) {
58+ settings = {
59+ ...defaultSettings ,
60+ ...unocssSettings ,
61+ autocompleteMatchType : unocssSettings ?. autocomplete ?. matchType ?? defaultSettings . autocompleteMatchType ,
62+ autocompleteStrict : unocssSettings ?. autocomplete ?. strict ?? defaultSettings . autocompleteStrict ,
63+ autocompleteMaxItems : unocssSettings ?. autocomplete ?. maxItems ?? defaultSettings . autocompleteMaxItems ,
64+ }
65+ }
66+
67+ function resolveConfiguredRoots ( root : string | string [ ] | undefined ) : string [ ] {
68+ return resolveWorkspaceRoots ( root , {
69+ workspaceRootPath : workspaceContext . rootPath ,
70+ workspaceFileDir : workspaceContext . fileDir ,
71+ workspaceFolderPaths : workspaceContext . folderPaths ,
72+ } )
73+ }
74+
75+ async function applyConfiguredRoots ( ) {
76+ if ( ! contextManager )
77+ return
78+
79+ await contextManager . setRoots ( resolveConfiguredRoots ( settings . root ) )
80+ }
81+
82+ function updateWorkspaceContext (
83+ rootUri : string ,
84+ workspaceFolders : { uri : string } [ ] | null | undefined ,
85+ initializationOptions : WorkspaceInitializationOptions | undefined ,
86+ ) {
87+ workspaceContext . rootPath = uriToFsPath ( rootUri )
88+ workspaceContext . fileDir = initializationOptions ?. workspaceFileDir
89+ workspaceContext . folderPaths = initializationOptions ?. workspaceFolderPaths
90+ || workspaceFolders ?. map ( folder => uriToFsPath ( folder . uri ) )
91+ || [ ]
92+ }
93+
94+ function createContextManager ( ) {
95+ if ( ! workspaceContext . rootPath )
96+ return
97+
98+ contextManager = new ContextManager (
99+ workspaceContext . rootPath ,
100+ connection ,
101+ workspaceContext . folderPaths . length ? workspaceContext . folderPaths : [ workspaceContext . rootPath ] ,
102+ )
103+
104+ contextManager . events . on ( 'contextReload' , ( ctx ) => {
105+ resetAutoCompleteCache ( ctx )
106+ } )
107+ contextManager . events . on ( 'contextUnload' , ( ctx ) => {
108+ resetAutoCompleteCache ( ctx )
109+ } )
110+ contextManager . events . on ( 'unload' , ( ctx ) => {
111+ resetAutoCompleteCache ( ctx )
112+ } )
113+ contextManager . events . on ( 'contextLoaded' , ( ) => {
114+ void updateConfigWatchers ( )
115+ } )
116+ }
33117
34118async function updateConfigWatchers ( ) : Promise < void > {
35119 if ( ! hasWatchedFilesCapability || ! serverInitialized || ! contextManager )
@@ -78,26 +162,14 @@ function getContextManager() {
78162
79163connection . onInitialize ( ( params ) => {
80164 hasWatchedFilesCapability = ! ! params . capabilities . workspace ?. didChangeWatchedFiles ?. dynamicRegistration
165+ const initializationOptions = params . initializationOptions as WorkspaceInitializationOptions | undefined
81166
82167 const workspaceFolders = params . workspaceFolders
83168 const rootUri = workspaceFolders ?. [ 0 ] ?. uri || params . rootUri
84169
85170 if ( rootUri ) {
86- const rootPath = uriToFsPath ( rootUri )
87- contextManager = new ContextManager ( rootPath , connection )
88-
89- contextManager . events . on ( 'contextReload' , ( ctx ) => {
90- resetAutoCompleteCache ( ctx )
91- } )
92- contextManager . events . on ( 'contextUnload' , ( ctx ) => {
93- resetAutoCompleteCache ( ctx )
94- } )
95- contextManager . events . on ( 'unload' , ( ctx ) => {
96- resetAutoCompleteCache ( ctx )
97- } )
98- contextManager . events . on ( 'contextLoaded' , ( ) => {
99- void updateConfigWatchers ( )
100- } )
171+ updateWorkspaceContext ( rootUri , workspaceFolders , initializationOptions )
172+ createContextManager ( )
101173 }
102174
103175 // Register all LSP capabilities
@@ -121,9 +193,16 @@ connection.onInitialize((params) => {
121193} )
122194
123195connection . onInitialized ( async ( ) => {
124- serverInitialized = true
125- if ( contextManager )
196+ try {
197+ mergeSettings ( await connection . workspace . getConfiguration ( 'unocss' ) )
198+ }
199+ catch { }
200+
201+ if ( contextManager ) {
126202 await contextManager . ready
203+ await applyConfiguredRoots ( )
204+ }
205+ serverInitialized = true
127206 connection . console . log ( '✅ UnoCSS Language Server initialized' )
128207 await updateConfigWatchers ( )
129208} )
@@ -143,16 +222,12 @@ connection.onDidChangeWatchedFiles((_change) => {
143222 } , 500 )
144223} )
145224
146- connection . onDidChangeConfiguration ( ( change ) => {
225+ connection . onDidChangeConfiguration ( async ( change ) => {
147226 const unocssSettings = change . settings ?. unocss
148227 if ( unocssSettings ) {
149- settings = {
150- ...defaultSettings ,
151- ...unocssSettings ,
152- autocompleteMatchType : unocssSettings ?. autocomplete ?. matchType ?? defaultSettings . autocompleteMatchType ,
153- autocompleteStrict : unocssSettings ?. autocomplete ?. strict ?? defaultSettings . autocompleteStrict ,
154- autocompleteMaxItems : unocssSettings ?. autocomplete ?. maxItems ?? defaultSettings . autocompleteMaxItems ,
155- }
228+ mergeSettings ( unocssSettings )
229+ await applyConfiguredRoots ( )
230+ await updateConfigWatchers ( )
156231 resetAutoCompleteCache ( )
157232 }
158233} )
0 commit comments