Skip to content

Commit ce761ba

Browse files
committed
boot language clients lazily
1 parent 99297ad commit ce761ba

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/extension.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Position,
1717
Range,
1818
TextEditorDecorationType,
19+
RelativePattern,
1920
} from 'vscode'
2021
import { LanguageClient, LanguageClientOptions, TransportKind } from 'vscode-languageclient/node'
2122
import { DEFAULT_LANGUAGES } from './lib/languages'
@@ -28,8 +29,11 @@ const colorNames = Object.keys(namedColors)
2829
const CLIENT_ID = 'tailwindcss-intellisense'
2930
const CLIENT_NAME = 'Tailwind CSS IntelliSense'
3031

32+
const CONFIG_FILE_GLOB = 'tailwind.config.{js,cjs}'
33+
3134
let clients: Map<string, LanguageClient> = new Map()
3235
let languages: Map<string, string[]> = new Map()
36+
let searchedFolders: Set<string> = new Set()
3337

3438
let _sortedWorkspaceFolders: string[] | undefined
3539
function sortedWorkspaceFolders(): string[] {
@@ -83,6 +87,19 @@ export function activate(context: ExtensionContext) {
8387
})
8488
)
8589

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+
86103
// TODO: check if the actual language MAPPING changed
87104
// not just the language IDs
88105
// e.g. "plaintext" already exists but you change it from "html" to "css"
@@ -117,6 +134,13 @@ export function activate(context: ExtensionContext) {
117134
// placeholder so we don't boot another server before this one is ready
118135
clients.set(folder.uri.toString(), null)
119136

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+
120144
let debugOptions = {
121145
execArgv: ['--nolazy', `--inspect=${6011 + clients.size}`],
122146
}
@@ -255,6 +279,7 @@ export function activate(context: ExtensionContext) {
255279
configurationSection: ['editor', 'tailwindCSS'],
256280
},
257281
}
282+
258283
let client = new LanguageClient(CLIENT_ID, CLIENT_NAME, serverOptions, clientOptions)
259284

260285
client.onReady().then(() => {
@@ -284,7 +309,7 @@ export function activate(context: ExtensionContext) {
284309
clients.set(folder.uri.toString(), client)
285310
}
286311

287-
function didOpenTextDocument(document: TextDocument): void {
312+
async function didOpenTextDocument(document: TextDocument): Promise<void> {
288313
// We are only interested in language mode text
289314
if (document.uri.scheme !== 'file') {
290315
return
@@ -300,11 +325,18 @@ export function activate(context: ExtensionContext) {
300325
// If we have nested workspace folders we only start a server on the outer most workspace folder.
301326
folder = getOuterMostWorkspaceFolder(folder)
302327

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
308340
}
309341

310342
bootWorkspaceClient(folder)
@@ -316,6 +348,7 @@ export function activate(context: ExtensionContext) {
316348
for (let folder of event.removed) {
317349
let client = clients.get(folder.uri.toString())
318350
if (client) {
351+
searchedFolders.delete(folder.uri.toString())
319352
clients.delete(folder.uri.toString())
320353
client.stop()
321354
}

0 commit comments

Comments
 (0)