Skip to content

Commit 3e923db

Browse files
committed
add package root to document selectors
1 parent dbe5864 commit 3e923db

File tree

1 file changed

+27
-14
lines changed
  • packages/tailwindcss-language-server/src

1 file changed

+27
-14
lines changed

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,15 @@ type ProjectConfig = {
195195
documentSelector?: Array<DocumentSelector>
196196
}
197197

198-
type DocumentSelector = { pattern: string; priority: number }
199198
enum DocumentSelectorPriority {
200199
CONFIG_FILE = 0,
201200
CSS_FILE = 0,
202201
CONTENT_FILE = 1,
203202
CSS_DIRECTORY = 2,
204203
CONFIG_DIRECTORY = 3,
204+
ROOT_DIRECTORY = 4,
205205
}
206+
type DocumentSelector = { pattern: string; priority: DocumentSelectorPriority }
206207

207208
function getMode(config: any): unknown {
208209
if (typeof config.mode !== 'undefined') {
@@ -1403,6 +1404,26 @@ async function getConfigFileFromCssFile(cssFile: string): Promise<string | null>
14031404
return path.resolve(path.dirname(cssFile), match.groups.config.slice(1, -1))
14041405
}
14051406

1407+
function getPackageRoot(cwd: string, rootDir: string) {
1408+
try {
1409+
let pkgJsonPath = findUp.sync(
1410+
(dir) => {
1411+
let pkgJson = path.join(dir, 'package.json')
1412+
if (findUp.sync.exists(pkgJson)) {
1413+
return pkgJson
1414+
}
1415+
if (dir === rootDir) {
1416+
return findUp.stop
1417+
}
1418+
},
1419+
{ cwd }
1420+
)
1421+
return pkgJsonPath ? path.dirname(pkgJsonPath) : rootDir
1422+
} catch {
1423+
return rootDir
1424+
}
1425+
}
1426+
14061427
function getContentDocumentSelectorFromConfigFile(
14071428
configPath: string,
14081429
tailwindVersion: string,
@@ -1419,19 +1440,7 @@ function getContentDocumentSelectorFromConfigFile(
14191440
if (relativeEnabled) {
14201441
contentBase = path.dirname(configPath)
14211442
} else {
1422-
let pkgJsonPath = findUp.sync(
1423-
(dir) => {
1424-
let pkgJson = path.join(dir, 'package.json')
1425-
if (findUp.sync.exists(pkgJson)) {
1426-
return pkgJson
1427-
}
1428-
if (dir === rootDir) {
1429-
return findUp.stop
1430-
}
1431-
},
1432-
{ cwd: path.dirname(configPath) }
1433-
)
1434-
contentBase = pkgJsonPath ? path.dirname(pkgJsonPath) : rootDir
1443+
contentBase = getPackageRoot(path.dirname(configPath), rootDir)
14351444
}
14361445
return content
14371446
.filter((item): item is string => typeof item === 'string')
@@ -1587,6 +1596,10 @@ class TW {
15871596
}
15881597
: []
15891598
)
1599+
.concat({
1600+
pattern: normalizePath(path.join(getPackageRoot(path.dirname(configPath), base), '**')),
1601+
priority: DocumentSelectorPriority.ROOT_DIRECTORY,
1602+
})
15901603
projects[configPath] = [...(projects[configPath] ?? []), ...documentSelector]
15911604
if (isCssFile) {
15921605
cssFileConfigMap.set(normalizedFilename, configPath)

0 commit comments

Comments
 (0)