Skip to content

Commit 9313d47

Browse files
committed
Refactor
Compute correct document selectors when a project is initialized Refactor
1 parent 38d7a12 commit 9313d47

File tree

2 files changed

+67
-57
lines changed

2 files changed

+67
-57
lines changed

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

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -206,62 +206,7 @@ export class ProjectLocator {
206206
// Look for the package root for the config
207207
config.packageRoot = await getPackageRoot(path.dirname(config.path), this.base)
208208

209-
let selectors: DocumentSelector[] = []
210-
211-
// selectors:
212-
// - CSS files
213-
for (let entry of config.entries) {
214-
if (entry.type !== 'css') continue
215-
selectors.push({
216-
pattern: entry.path,
217-
priority: DocumentSelectorPriority.CSS_FILE,
218-
})
219-
}
220-
221-
// - Config File
222-
selectors.push({
223-
pattern: config.path,
224-
priority: DocumentSelectorPriority.CONFIG_FILE,
225-
})
226-
227-
// - Content patterns from config
228-
for await (let selector of contentSelectorsFromConfig(
229-
config,
230-
tailwind.features,
231-
this.resolver,
232-
)) {
233-
selectors.push(selector)
234-
}
235-
236-
// - Directories containing the CSS files
237-
for (let entry of config.entries) {
238-
if (entry.type !== 'css') continue
239-
selectors.push({
240-
pattern: normalizePath(path.join(path.dirname(entry.path), '**')),
241-
priority: DocumentSelectorPriority.CSS_DIRECTORY,
242-
})
243-
}
244-
245-
// - Directory containing the config
246-
selectors.push({
247-
pattern: normalizePath(path.join(path.dirname(config.path), '**')),
248-
priority: DocumentSelectorPriority.CONFIG_DIRECTORY,
249-
})
250-
251-
// - Root of package that contains the config
252-
selectors.push({
253-
pattern: normalizePath(path.join(config.packageRoot, '**')),
254-
priority: DocumentSelectorPriority.PACKAGE_DIRECTORY,
255-
})
256-
257-
// Reorder selectors from most specific to least specific
258-
selectors.sort((a, z) => a.priority - z.priority)
259-
260-
// Eliminate duplicate selector patterns
261-
selectors = selectors.filter(
262-
({ pattern }, index, documentSelectors) =>
263-
documentSelectors.findIndex(({ pattern: p }) => p === pattern) === index,
264-
)
209+
let selectors = await calculateDocumentSelectors(config, tailwind.features, this.resolver)
265210

266211
return {
267212
config,
@@ -793,3 +738,66 @@ function requiresPreprocessor(filepath: string) {
793738

794739
return ext === '.scss' || ext === '.sass' || ext === '.less' || ext === '.styl' || ext === '.pcss'
795740
}
741+
742+
export async function calculateDocumentSelectors(
743+
config: ConfigEntry,
744+
features: Feature[],
745+
resolver: Resolver,
746+
) {
747+
let selectors: DocumentSelector[] = []
748+
749+
// selectors:
750+
// - CSS files
751+
for (let entry of config.entries) {
752+
if (entry.type !== 'css') continue
753+
754+
selectors.push({
755+
pattern: entry.path,
756+
priority: DocumentSelectorPriority.CSS_FILE,
757+
})
758+
}
759+
760+
// - Config File
761+
selectors.push({
762+
pattern: config.path,
763+
priority: DocumentSelectorPriority.CONFIG_FILE,
764+
})
765+
766+
// - Content patterns from config
767+
for await (let selector of contentSelectorsFromConfig(config, features, resolver)) {
768+
selectors.push(selector)
769+
}
770+
771+
// - Directories containing the CSS files
772+
for (let entry of config.entries) {
773+
if (entry.type !== 'css') continue
774+
775+
selectors.push({
776+
pattern: normalizePath(path.join(path.dirname(entry.path), '**')),
777+
priority: DocumentSelectorPriority.CSS_DIRECTORY,
778+
})
779+
}
780+
781+
// - Directory containing the config
782+
selectors.push({
783+
pattern: normalizePath(path.join(path.dirname(config.path), '**')),
784+
priority: DocumentSelectorPriority.CONFIG_DIRECTORY,
785+
})
786+
787+
// - Root of package that contains the config
788+
selectors.push({
789+
pattern: normalizePath(path.join(config.packageRoot, '**')),
790+
priority: DocumentSelectorPriority.PACKAGE_DIRECTORY,
791+
})
792+
793+
// Reorder selectors from most specific to least specific
794+
selectors.sort((a, z) => a.priority - z.priority)
795+
796+
// Eliminate duplicate selector patterns
797+
selectors = selectors.filter(
798+
({ pattern }, index, documentSelectors) =>
799+
documentSelectors.findIndex(({ pattern: p }) => p === pattern) === index,
800+
)
801+
802+
return selectors
803+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ export async function createProjectService(
286286
)
287287
}
288288

289-
function onFileEvents(changes: Array<{ file: string; type: FileChangeType }>): void {
289+
async function onFileEvents(
290+
changes: Array<{ file: string; type: FileChangeType }>,
291+
): Promise<void> {
290292
let needsInit = false
291293
let needsRebuild = false
292294

0 commit comments

Comments
 (0)