Skip to content

Commit 04b9e57

Browse files
committed
wip
1 parent 2920da8 commit 04b9e57

File tree

1 file changed

+47
-10
lines changed
  • packages/tailwindcss-language-server/src

1 file changed

+47
-10
lines changed

packages/tailwindcss-language-server/src/server.ts

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,38 @@ function withFallback<T>(getter: () => T, fallback: T): T {
320320
}
321321
}
322322

323+
function dirContains(dir: string, file: string): boolean {
324+
let relative = path.relative(dir, file)
325+
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative)
326+
}
327+
328+
function changeAffectsFile(change: string, files: string[]): boolean {
329+
for (let file of files) {
330+
console.log({ change, file, contains: dirContains(change, file) })
331+
if (change === file || dirContains(change, file)) {
332+
return true
333+
}
334+
}
335+
return false
336+
}
337+
338+
// We need to add parent directories to the watcher:
339+
// https://github.com/microsoft/vscode/issues/60813
340+
function getWatchPatternsForFile(file: string): string[] {
341+
let tmp: string
342+
let dir = path.dirname(file)
343+
let patterns: string[] = [file, dir]
344+
while (true) {
345+
dir = path.dirname((tmp = dir))
346+
if (tmp === dir) {
347+
break
348+
} else {
349+
patterns.push(dir)
350+
}
351+
}
352+
return patterns
353+
}
354+
323355
async function createProjectService(
324356
projectConfig: ProjectConfig,
325357
connection: Connection,
@@ -358,7 +390,14 @@ async function createProjectService(
358390
}
359391

360392
if (projectConfig.configPath) {
361-
watchPatterns([projectConfig.configPath, ...getModuleDependencies(projectConfig.configPath)])
393+
let deps = []
394+
try {
395+
deps = getModuleDependencies(projectConfig.configPath)
396+
} catch {}
397+
watchPatterns([
398+
...getWatchPatternsForFile(projectConfig.configPath),
399+
...deps.flatMap((dep) => getWatchPatternsForFile(dep)),
400+
])
362401
}
363402

364403
function log(...args: string[]): void {
@@ -374,10 +413,8 @@ async function createProjectService(
374413
for (let change of changes) {
375414
let file = normalizePath(change.file)
376415

377-
let isConfigFile = projectConfig.configPath
378-
? file === projectConfig.configPath
379-
: minimatch(file, `**/${CONFIG_GLOB}`, { dot: true })
380-
let isDependency = state.dependencies && state.dependencies.includes(change.file)
416+
let isConfigFile = changeAffectsFile(file, [projectConfig.configPath])
417+
let isDependency = changeAffectsFile(change.file, state.dependencies ?? [])
381418
let isPackageFile = minimatch(file, `**/${PACKAGE_LOCK_GLOB}`, { dot: true })
382419

383420
if (!isConfigFile && !isDependency && !isPackageFile) continue
@@ -432,14 +469,14 @@ async function createProjectService(
432469

433470
function resetState(): void {
434471
// clearAllDiagnostics(state)
435-
refreshDiagnostics()
436472
Object.keys(state).forEach((key) => {
437473
// Keep `dependencies` to ensure that they are still watched
438474
if (key !== 'editor' && key !== 'dependencies') {
439475
delete state[key]
440476
}
441477
})
442478
state.enabled = false
479+
refreshDiagnostics()
443480
updateCapabilities()
444481
}
445482

@@ -478,7 +515,7 @@ async function createProjectService(
478515
throw new SilentError('No config file found.')
479516
}
480517

481-
watchPatterns([configPath])
518+
watchPatterns(getWatchPatternsForFile(configPath))
482519

483520
const pnpPath = findUp.sync(
484521
(dir) => {
@@ -967,7 +1004,7 @@ async function createProjectService(
9671004
// }
9681005
state.dependencies = getModuleDependencies(state.configPath)
9691006
// chokidarWatcher?.add(state.dependencies)
970-
watchPatterns(state.dependencies ?? [])
1007+
watchPatterns((state.dependencies ?? []).flatMap((dep) => getWatchPatternsForFile(dep)))
9711008

9721009
state.configId = getConfigId(state.configPath, state.dependencies)
9731010

@@ -1686,8 +1723,8 @@ class TW {
16861723
for (let [key] of this.projects) {
16871724
let projectConfig = JSON.parse(key) as ProjectConfig
16881725
if (
1689-
normalizedFilename === projectConfig.configPath &&
1690-
change.type === FileChangeType.Deleted
1726+
change.type === FileChangeType.Deleted &&
1727+
changeAffectsFile(normalizedFilename, [projectConfig.configPath])
16911728
) {
16921729
needsRestart = true
16931730
break changeLoop

0 commit comments

Comments
 (0)