Skip to content

Commit 1353031

Browse files
committed
Refactor
1 parent cbbca79 commit 1353031

File tree

2 files changed

+54
-71
lines changed

2 files changed

+54
-71
lines changed

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

+51-66
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 (let selector of await calculateDocumnetSelectors(
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,
@@ -795,26 +740,66 @@ function requiresPreprocessor(filepath: string) {
795740
return ext === '.scss' || ext === '.sass' || ext === '.less' || ext === '.styl' || ext === '.pcss'
796741
}
797742

798-
export async function calculateDocumnetSelectors(
799-
entry: ConfigEntry,
743+
export async function calculateDocumentSelectors(
744+
config: ConfigEntry,
800745
features: Feature[],
801746
resolver: Resolver,
802-
existing?: DocumentSelector[],
803747
actualConfig?: any,
804748
) {
805-
let selectors = []
749+
let selectors: DocumentSelector[] = []
806750

807-
for (let selector of existing ?? []) {
808-
if (selector.priority === DocumentSelectorPriority.CONTENT_FILE) {
809-
continue
810-
}
751+
// selectors:
752+
// - CSS files
753+
for (let entry of config.entries) {
754+
if (entry.type !== 'css') continue
811755

812-
selectors.push(selector)
756+
selectors.push({
757+
pattern: entry.path,
758+
priority: DocumentSelectorPriority.CSS_FILE,
759+
})
813760
}
814761

815-
for await (let selector of contentSelectorsFromConfig(entry, features, resolver, actualConfig)) {
762+
// - Config File
763+
selectors.push({
764+
pattern: config.path,
765+
priority: DocumentSelectorPriority.CONFIG_FILE,
766+
})
767+
768+
// - Content patterns from config
769+
for await (let selector of contentSelectorsFromConfig(config, features, resolver, actualConfig)) {
816770
selectors.push(selector)
817771
}
818772

773+
// - Directories containing the CSS files
774+
for (let entry of config.entries) {
775+
if (entry.type !== 'css') continue
776+
777+
selectors.push({
778+
pattern: normalizePath(path.join(path.dirname(entry.path), '**')),
779+
priority: DocumentSelectorPriority.CSS_DIRECTORY,
780+
})
781+
}
782+
783+
// - Directory containing the config
784+
selectors.push({
785+
pattern: normalizePath(path.join(path.dirname(config.path), '**')),
786+
priority: DocumentSelectorPriority.CONFIG_DIRECTORY,
787+
})
788+
789+
// - Root of package that contains the config
790+
selectors.push({
791+
pattern: normalizePath(path.join(config.packageRoot, '**')),
792+
priority: DocumentSelectorPriority.PACKAGE_DIRECTORY,
793+
})
794+
795+
// Reorder selectors from most specific to least specific
796+
selectors.sort((a, z) => a.priority - z.priority)
797+
798+
// Eliminate duplicate selector patterns
799+
selectors = selectors.filter(
800+
({ pattern }, index, documentSelectors) =>
801+
documentSelectors.findIndex(({ pattern: p }) => p === pattern) === index,
802+
)
803+
819804
return selectors
820805
}

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import {
8080
normalizeDriveLetter,
8181
} from './utils'
8282
import type { DocumentService } from './documents'
83-
import { calculateDocumnetSelectors, type ProjectConfig } from './project-locator'
83+
import { calculateDocumentSelectors, type ProjectConfig } from './project-locator'
8484
import { supportedFeatures } from '@tailwindcss/language-service/src/features'
8585
import { loadDesignSystem } from './util/v4'
8686
import { readCssFile } from './util/css'
@@ -309,11 +309,10 @@ export async function createProjectService(
309309
projectConfig.configPath &&
310310
(isConfigFile || isDependency)
311311
) {
312-
documentSelector = await calculateDocumnetSelectors(
312+
documentSelector = await calculateDocumentSelectors(
313313
projectConfig.config,
314314
state.features,
315315
resolver,
316-
documentSelector,
317316
)
318317

319318
checkOpenDocuments()
@@ -961,11 +960,10 @@ export async function createProjectService(
961960

962961
/////////////////////
963962
if (!projectConfig.isUserConfigured) {
964-
documentSelector = await calculateDocumnetSelectors(
963+
documentSelector = await calculateDocumentSelectors(
965964
projectConfig.config,
966965
state.features,
967966
resolver,
968-
documentSelector,
969967
originalConfig,
970968
)
971969
}

0 commit comments

Comments
 (0)