@@ -16,6 +16,7 @@ import {
16
16
Position ,
17
17
Range ,
18
18
TextEditorDecorationType ,
19
+ RelativePattern ,
19
20
} from 'vscode'
20
21
import { LanguageClient , LanguageClientOptions , TransportKind } from 'vscode-languageclient/node'
21
22
import { DEFAULT_LANGUAGES } from './lib/languages'
@@ -28,8 +29,11 @@ const colorNames = Object.keys(namedColors)
28
29
const CLIENT_ID = 'tailwindcss-intellisense'
29
30
const CLIENT_NAME = 'Tailwind CSS IntelliSense'
30
31
32
+ const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}'
33
+
31
34
let clients : Map < string , LanguageClient > = new Map ( )
32
35
let languages : Map < string , string [ ] > = new Map ( )
36
+ let searchedFolders : Set < string > = new Set ( )
33
37
34
38
let _sortedWorkspaceFolders : string [ ] | undefined
35
39
function sortedWorkspaceFolders ( ) : string [ ] {
@@ -83,6 +87,19 @@ export function activate(context: ExtensionContext) {
83
87
} )
84
88
)
85
89
90
+ let watcher = Workspace . createFileSystemWatcher ( `**/${ CONFIG_FILE_GLOB } ` , false , true , true )
91
+
92
+ watcher . onDidCreate ( ( uri ) => {
93
+ let folder = Workspace . getWorkspaceFolder ( uri )
94
+ if ( ! folder ) {
95
+ return
96
+ }
97
+ folder = getOuterMostWorkspaceFolder ( folder )
98
+ bootWorkspaceClient ( folder )
99
+ } )
100
+
101
+ context . subscriptions . push ( watcher )
102
+
86
103
// TODO: check if the actual language MAPPING changed
87
104
// not just the language IDs
88
105
// e.g. "plaintext" already exists but you change it from "html" to "css"
@@ -117,6 +134,13 @@ export function activate(context: ExtensionContext) {
117
134
// placeholder so we don't boot another server before this one is ready
118
135
clients . set ( folder . uri . toString ( ) , null )
119
136
137
+ if ( ! languages . has ( folder . uri . toString ( ) ) ) {
138
+ languages . set (
139
+ folder . uri . toString ( ) ,
140
+ dedupe ( [ ...DEFAULT_LANGUAGES , ...Object . keys ( getUserLanguages ( folder ) ) ] )
141
+ )
142
+ }
143
+
120
144
let debugOptions = {
121
145
execArgv : [ '--nolazy' , `--inspect=${ 6011 + clients . size } ` ] ,
122
146
}
@@ -255,6 +279,7 @@ export function activate(context: ExtensionContext) {
255
279
configurationSection : [ 'editor' , 'tailwindCSS' ] ,
256
280
} ,
257
281
}
282
+
258
283
let client = new LanguageClient ( CLIENT_ID , CLIENT_NAME , serverOptions , clientOptions )
259
284
260
285
client . onReady ( ) . then ( ( ) => {
@@ -284,7 +309,7 @@ export function activate(context: ExtensionContext) {
284
309
clients . set ( folder . uri . toString ( ) , client )
285
310
}
286
311
287
- function didOpenTextDocument ( document : TextDocument ) : void {
312
+ async function didOpenTextDocument ( document : TextDocument ) : Promise < void > {
288
313
// We are only interested in language mode text
289
314
if ( document . uri . scheme !== 'file' ) {
290
315
return
@@ -300,11 +325,18 @@ export function activate(context: ExtensionContext) {
300
325
// If we have nested workspace folders we only start a server on the outer most workspace folder.
301
326
folder = getOuterMostWorkspaceFolder ( folder )
302
327
303
- if ( ! languages . has ( folder . uri . toString ( ) ) ) {
304
- languages . set (
305
- folder . uri . toString ( ) ,
306
- dedupe ( [ ...DEFAULT_LANGUAGES , ...Object . keys ( getUserLanguages ( ) ) ] )
307
- )
328
+ if ( searchedFolders . has ( folder . uri . toString ( ) ) ) return
329
+
330
+ searchedFolders . add ( folder . uri . toString ( ) )
331
+
332
+ let [ configFile ] = await Workspace . findFiles (
333
+ new RelativePattern ( folder , `**/${ CONFIG_FILE_GLOB } ` ) ,
334
+ '**/node_modules/**' ,
335
+ 1
336
+ )
337
+
338
+ if ( ! configFile ) {
339
+ return
308
340
}
309
341
310
342
bootWorkspaceClient ( folder )
@@ -316,6 +348,7 @@ export function activate(context: ExtensionContext) {
316
348
for ( let folder of event . removed ) {
317
349
let client = clients . get ( folder . uri . toString ( ) )
318
350
if ( client ) {
351
+ searchedFolders . delete ( folder . uri . toString ( ) )
319
352
clients . delete ( folder . uri . toString ( ) )
320
353
client . stop ( )
321
354
}
0 commit comments