Skip to content

Commit 926885a

Browse files
committed
Use enum for document selector priorities
1 parent 8032797 commit 926885a

File tree

1 file changed

+36
-13
lines changed
  • packages/tailwindcss-language-server/src

1 file changed

+36
-13
lines changed

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ type ProjectConfig = {
198198
}
199199

200200
type DocumentSelector = { pattern: string; priority: number }
201+
enum DocumentSelectorPriority {
202+
CONFIG_FILE = 0,
203+
CSS_FILE = 0,
204+
CONTENT_FILE = 1,
205+
CSS_DIRECTORY = 2,
206+
CONFIG_DIRECTORY = 3,
207+
}
201208

202209
function getMode(config: any): unknown {
203210
if (typeof config.mode !== 'undefined') {
@@ -371,7 +378,9 @@ async function createProjectService(
371378
if (!enabled) {
372379
if (projectConfig.configPath && (isConfigFile || isDependency)) {
373380
documentSelector = [
374-
...documentSelector.filter(({ priority }) => priority !== 1),
381+
...documentSelector.filter(
382+
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE
383+
),
375384
// TODO `state.version` isn't going to exist here
376385
...getContentDocumentSelectorFromConfigFile(
377386
projectConfig.configPath,
@@ -880,7 +889,9 @@ async function createProjectService(
880889

881890
/////////////////////
882891
documentSelector = [
883-
...documentSelector.filter(({ priority }) => priority !== 1),
892+
...documentSelector.filter(
893+
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE
894+
),
884895
...getContentDocumentSelectorFromConfigFile(
885896
state.configPath,
886897
tailwindcss.version,
@@ -1420,7 +1431,10 @@ function getContentDocumentSelectorFromConfigFile(
14201431
return content
14211432
.filter((item): item is string => typeof item === 'string')
14221433
.map((item) => path.resolve(contentBase, item))
1423-
.map((item) => ({ pattern: normalizePath(item), priority: 1 }))
1434+
.map((item) => ({
1435+
pattern: normalizePath(item),
1436+
priority: DocumentSelectorPriority.CONTENT_FILE,
1437+
}))
14241438
}
14251439

14261440
class TW {
@@ -1486,9 +1500,10 @@ class TW {
14861500
return {
14871501
folder: base,
14881502
configPath: path.resolve(base, relativeConfigPath),
1489-
documentSelector: []
1490-
.concat(relativeDocumentSelectorOrSelectors)
1491-
.map((selector) => ({ priority: 1, pattern: path.resolve(base, selector) })),
1503+
documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({
1504+
priority: DocumentSelectorPriority.CONTENT_FILE,
1505+
pattern: path.resolve(base, selector),
1506+
})),
14921507
}
14931508
}
14941509
)
@@ -1537,24 +1552,32 @@ class TW {
15371552
let documentSelector = contentSelector
15381553
.concat({
15391554
pattern: normalizePath(filename),
1540-
priority: 0,
1555+
priority: isCssFile
1556+
? DocumentSelectorPriority.CSS_FILE
1557+
: DocumentSelectorPriority.CONFIG_FILE,
15411558
})
15421559
.concat(
15431560
isCssFile
15441561
? {
15451562
pattern: normalizePath(configPath),
1546-
priority: 0,
1563+
priority: DocumentSelectorPriority.CONFIG_FILE,
15471564
}
15481565
: []
15491566
)
15501567
.concat({
15511568
pattern: normalizePath(path.join(path.dirname(filename), '**')),
1552-
priority: 2,
1553-
})
1554-
.concat({
1555-
pattern: normalizePath(path.join(path.dirname(configPath), '**')),
1556-
priority: 3,
1569+
priority: isCssFile
1570+
? DocumentSelectorPriority.CSS_DIRECTORY
1571+
: DocumentSelectorPriority.CONFIG_DIRECTORY,
15571572
})
1573+
.concat(
1574+
isCssFile
1575+
? {
1576+
pattern: normalizePath(path.join(path.dirname(configPath), '**')),
1577+
priority: DocumentSelectorPriority.CONFIG_DIRECTORY,
1578+
}
1579+
: []
1580+
)
15581581
projects[configPath] = [...(projects[configPath] ?? []), ...documentSelector]
15591582
if (isCssFile) {
15601583
cssFileConfigMap.set(normalizedFilename, configPath)

0 commit comments

Comments
 (0)