Skip to content

Commit 8b16c05

Browse files
committed
wip
1 parent e33bb3e commit 8b16c05

File tree

8 files changed

+159
-116
lines changed

8 files changed

+159
-116
lines changed

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

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import { getModuleDependencies } from './util/getModuleDependencies'
5656
import assert from 'assert'
5757
// import postcssLoadConfig from 'postcss-load-config'
5858
import { bigSign } from '@tailwindcss/language-service/src/util/jit'
59-
import { getColor } from '@tailwindcss/language-service/src/util/color'
59+
import { getColor, getColors } from '@tailwindcss/language-service/src/util/color'
6060
import * as culori from 'culori'
6161
import namedColors from 'color-name'
6262
import tailwindPlugins from './lib/plugins'
@@ -84,7 +84,7 @@ const colorNames = Object.keys(namedColors)
8484

8585
function getConfigId(configPath: string, configDependencies: string[]): string {
8686
return JSON.stringify(
87-
[configPath, ...configDependencies].map((file) => [file, fs.statSync(file).mtimeMs])
87+
[configPath, ...configDependencies].map((file) => [file, fs.statSync(file).mtimeMs]),
8888
)
8989
}
9090

@@ -180,7 +180,7 @@ export async function createProjectService(
180180
refreshDiagnostics: () => void,
181181
watchPatterns: (patterns: string[]) => void,
182182
initialTailwindVersion: string,
183-
getConfiguration: (uri?: string) => Promise<Settings>
183+
getConfiguration: (uri?: string) => Promise<Settings>,
184184
): Promise<ProjectService> {
185185
let enabled = false
186186
const folder = projectConfig.folder
@@ -228,11 +228,11 @@ export async function createProjectService(
228228
return (await isExcluded(
229229
state,
230230
document,
231-
path.join(directory, dirent.name, isDirectory ? '/' : '')
231+
path.join(directory, dirent.name, isDirectory ? '/' : ''),
232232
))
233233
? null
234234
: [dirent.name, { isDirectory }]
235-
})
235+
}),
236236
)
237237
return result.filter((item) => item !== null)
238238
} catch {
@@ -255,7 +255,7 @@ export async function createProjectService(
255255

256256
function log(...args: string[]): void {
257257
console.log(
258-
`[${path.relative(projectConfig.folder, projectConfig.configPath)}] ${args.join(' ')}`
258+
`[${path.relative(projectConfig.folder, projectConfig.configPath)}] ${args.join(' ')}`,
259259
)
260260
}
261261

@@ -280,12 +280,12 @@ export async function createProjectService(
280280
) {
281281
documentSelector = [
282282
...documentSelector.filter(
283-
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE
283+
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE,
284284
),
285285
...getContentDocumentSelectorFromConfigFile(
286286
projectConfig.configPath,
287287
initialTailwindVersion,
288-
projectConfig.folder
288+
projectConfig.folder,
289289
),
290290
]
291291

@@ -390,7 +390,7 @@ export async function createProjectService(
390390
return findUp.stop
391391
}
392392
},
393-
{ cwd: folder }
393+
{ cwd: folder },
394394
)
395395

396396
if (pnpPath) {
@@ -498,10 +498,9 @@ export async function createProjectService(
498498
resolveConfigFn = (config) => resolveConfig([config, defaultConfig])
499499
} catch (_) {
500500
try {
501-
const resolveConfig = require(resolveFrom(
502-
tailwindDir,
503-
'./lib/util/mergeConfigWithDefaults.js'
504-
))
501+
const resolveConfig = require(
502+
resolveFrom(tailwindDir, './lib/util/mergeConfigWithDefaults.js'),
503+
)
505504
const defaultConfig = require(resolveFrom(tailwindDir, './defaultConfig.js'))
506505
resolveConfigFn = (config) => resolveConfig.default(config, defaultConfig())
507506
} catch (_) {
@@ -522,7 +521,7 @@ export async function createProjectService(
522521

523522
if (semver.gte(tailwindcssVersion, '1.99.0')) {
524523
applyComplexClasses = firstOptional(() =>
525-
require(resolveFrom(tailwindDir, './lib/lib/substituteClassApplyAtRules'))
524+
require(resolveFrom(tailwindDir, './lib/lib/substituteClassApplyAtRules')),
526525
)
527526
} else if (semver.gte(tailwindcssVersion, '1.7.0')) {
528527
applyComplexClasses = require(resolveFrom(tailwindDir, './lib/flagged/applyComplexClasses'))
@@ -541,43 +540,39 @@ export async function createProjectService(
541540
try {
542541
let createContext = first(
543542
() => {
544-
let createContextFn = require(resolveFrom(
545-
configDir,
546-
'tailwindcss/lib/lib/setupContextUtils'
547-
)).createContext
543+
let createContextFn = require(
544+
resolveFrom(configDir, 'tailwindcss/lib/lib/setupContextUtils'),
545+
).createContext
548546
assert.strictEqual(typeof createContextFn, 'function')
549547
return (state) => createContextFn(state.config)
550548
},
551549
() => {
552-
let createContextFn = require(resolveFrom(
553-
configDir,
554-
'tailwindcss/lib/jit/lib/setupContextUtils'
555-
)).createContext
550+
let createContextFn = require(
551+
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContextUtils'),
552+
).createContext
556553
assert.strictEqual(typeof createContextFn, 'function')
557554
return (state) => createContextFn(state.config)
558555
},
559556
// TODO: the next two are canary releases only so can probably be removed
560557
() => {
561-
let setupTrackingContext = require(resolveFrom(
562-
configDir,
563-
'tailwindcss/lib/jit/lib/setupTrackingContext'
564-
)).default
558+
let setupTrackingContext = require(
559+
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupTrackingContext'),
560+
).default
565561
assert.strictEqual(typeof setupTrackingContext, 'function')
566562
return (state) =>
567563
setupTrackingContext(
568564
state.configPath,
569565
tailwindDirectives,
570-
registerDependency
566+
registerDependency,
571567
)(result, root)
572568
},
573569
() => {
574-
let setupContext = require(resolveFrom(
575-
configDir,
576-
'tailwindcss/lib/jit/lib/setupContext'
577-
)).default
570+
let setupContext = require(
571+
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContext'),
572+
).default
578573
assert.strictEqual(typeof setupContext, 'function')
579574
return (state) => setupContext(state.configPath, tailwindDirectives)(result, root)
580-
}
575+
},
581576
)
582577

583578
jitModules = {
@@ -587,7 +582,7 @@ export async function createProjectService(
587582
require(resolveFrom(configDir, 'tailwindcss/lib/lib/generateRules')).generateRules,
588583
() =>
589584
require(resolveFrom(configDir, 'tailwindcss/lib/jit/lib/generateRules'))
590-
.generateRules
585+
.generateRules,
591586
),
592587
},
593588
createContext: {
@@ -599,14 +594,14 @@ export async function createProjectService(
599594
require(resolveFrom(configDir, 'tailwindcss/lib/lib/expandApplyAtRules')).default,
600595
() =>
601596
require(resolveFrom(configDir, 'tailwindcss/lib/jit/lib/expandApplyAtRules'))
602-
.default
597+
.default,
603598
),
604599
},
605600
evaluateTailwindFunctions: {
606601
module: firstOptional(
607602
() =>
608603
require(resolveFrom(configDir, 'tailwindcss/lib/lib/evaluateTailwindFunctions'))
609-
.default
604+
.default,
610605
),
611606
},
612607
}
@@ -670,7 +665,7 @@ export async function createProjectService(
670665

671666
try {
672667
state.corePlugins = Object.keys(
673-
require(resolveFrom(path.dirname(state.configPath), 'tailwindcss/lib/plugins/index.js'))
668+
require(resolveFrom(path.dirname(state.configPath), 'tailwindcss/lib/plugins/index.js')),
674669
)
675670
} catch (_) {}
676671

@@ -693,7 +688,7 @@ export async function createProjectService(
693688
rule.before(
694689
postcss.comment({
695690
text: '__ORIGINAL_SELECTOR__:' + rule.selector,
696-
})
691+
}),
697692
)
698693
rule.selector = newSelector
699694
}
@@ -738,7 +733,7 @@ export async function createProjectService(
738733
let designSystem = await loadDesignSystem(
739734
state.modules.tailwindcss.module,
740735
state.configPath,
741-
css
736+
css,
742737
)
743738

744739
state.designSystem = designSystem
@@ -851,13 +846,13 @@ export async function createProjectService(
851846
if (!projectConfig.isUserConfigured) {
852847
documentSelector = [
853848
...documentSelector.filter(
854-
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE
849+
({ priority }) => priority !== DocumentSelectorPriority.CONTENT_FILE,
855850
),
856851
...getContentDocumentSelectorFromConfigFile(
857852
state.configPath,
858853
tailwindcss.version,
859854
projectConfig.folder,
860-
originalConfig
855+
originalConfig,
861856
),
862857
]
863858
}
@@ -873,15 +868,23 @@ export async function createProjectService(
873868
delete state.config.blocklist
874869

875870
if (state.v4) {
876-
state.classList = state.designSystem.getClassList().map((className) => {
877-
return [
878-
className[0],
879-
{
880-
...className[1],
881-
color: getColor(state, className[0]),
882-
},
883-
]
884-
})
871+
let classList = state.designSystem.getClassList()
872+
let colors = await getColors(
873+
state,
874+
classList.map((entry) => entry[0]),
875+
)
876+
877+
state.classList = await Promise.all(
878+
classList.map((className, idx) => {
879+
return [
880+
className[0],
881+
{
882+
...className[1],
883+
color: colors[idx],
884+
},
885+
]
886+
}),
887+
)
885888
} else if (state.jit) {
886889
state.jitContext = state.modules.jit.createContext.module(state)
887890
state.jitContext.tailwindConfig.separator = state.config.separator
@@ -931,7 +934,7 @@ export async function createProjectService(
931934
.join('\n'),
932935
{
933936
from: undefined,
934-
}
937+
},
935938
)
936939
} catch (error) {
937940
throw error
@@ -947,8 +950,8 @@ export async function createProjectService(
947950
// chokidarWatcher?.add(state.dependencies)
948951
watchPatterns(
949952
(state.dependencies ?? []).flatMap((dep) =>
950-
getWatchPatternsForFile(dep, projectConfig.folder)
951-
)
953+
getWatchPatternsForFile(dep, projectConfig.folder),
954+
),
952955
)
953956

954957
state.configId = getConfigId(state.configPath, state.dependencies)
@@ -1043,15 +1046,15 @@ export async function createProjectService(
10431046
let document = documentService.getDocument(params.textDocument.uri)
10441047
if (!document) return null
10451048
return getDocumentLinks(state, document, (linkPath) =>
1046-
URI.file(path.resolve(path.dirname(URI.parse(document.uri).fsPath), linkPath)).toString()
1049+
URI.file(path.resolve(path.dirname(URI.parse(document.uri).fsPath), linkPath)).toString(),
10471050
)
10481051
},
10491052
provideDiagnostics: debounce(
10501053
(document: TextDocument) => {
10511054
if (!state.enabled) return
10521055
provideDiagnostics(state, document)
10531056
},
1054-
params.initializationOptions?.testMode ? 0 : 500
1057+
params.initializationOptions?.testMode ? 0 : 500,
10551058
),
10561059
provideDiagnosticsForce: (document: TextDocument) => {
10571060
if (!state.enabled) return
@@ -1071,7 +1074,7 @@ export async function createProjectService(
10711074
if (!document) return []
10721075
let className = document.getText(params.range)
10731076
let match = className.match(
1074-
new RegExp(`-\\[(${colorNames.join('|')}|(?:(?:#|rgba?\\(|hsla?\\())[^\\]]+)\\]$`, 'i')
1077+
new RegExp(`-\\[(${colorNames.join('|')}|(?:(?:#|rgba?\\(|hsla?\\())[^\\]]+)\\]$`, 'i'),
10751078
)
10761079
// let match = className.match(/-\[((?:#|rgba?\(|hsla?\()[^\]]+)\]$/i)
10771080
if (match === null) return []
@@ -1193,7 +1196,7 @@ type Plugin = SimplePlugin | WrappedPlugin
11931196
function runPlugin(
11941197
plugin: Plugin,
11951198
state: State,
1196-
apiOverrides: Record<string, Function> = {}
1199+
apiOverrides: Record<string, Function> = {},
11971200
): void {
11981201
let config = state.config
11991202
let postcss = state.modules.postcss.module
@@ -1266,7 +1269,7 @@ function getVariants(state: State): Array<Variant> {
12661269
}
12671270

12681271
let fns = (Array.isArray(variantFnOrFns[0]) ? variantFnOrFns : [variantFnOrFns]).map(
1269-
([_sort, fn]) => fn
1272+
([_sort, fn]) => fn,
12701273
)
12711274

12721275
let placeholder = '__variant_placeholder__'
@@ -1324,7 +1327,7 @@ function getVariants(state: State): Array<Variant> {
13241327
definition = `@${rule.name} ${rule.params}`
13251328
}
13261329
},
1327-
})
1330+
}),
13281331
)
13291332

13301333
if (!definition) {
@@ -1355,7 +1358,7 @@ function getVariants(state: State): Array<Variant> {
13551358
return definitions
13561359
},
13571360
})
1358-
}
1361+
},
13591362
)
13601363

13611364
return result
@@ -1454,7 +1457,7 @@ async function getPlugins(config: any) {
14541457
return {
14551458
name: fnName,
14561459
}
1457-
})
1460+
}),
14581461
)
14591462
}
14601463

@@ -1470,7 +1473,7 @@ function getPackageRoot(cwd: string, rootDir: string) {
14701473
return findUp.stop
14711474
}
14721475
},
1473-
{ cwd }
1476+
{ cwd },
14741477
)
14751478
return pkgJsonPath ? path.dirname(pkgJsonPath) : rootDir
14761479
} catch {
@@ -1482,7 +1485,7 @@ function getContentDocumentSelectorFromConfigFile(
14821485
configPath: string,
14831486
tailwindVersion: string,
14841487
rootDir: string,
1485-
actualConfig?: any
1488+
actualConfig?: any,
14861489
): DocumentSelector[] {
14871490
let config = actualConfig ?? require(configPath)
14881491
let contentConfig: unknown = config.content?.files ?? config.content
@@ -1501,7 +1504,7 @@ function getContentDocumentSelectorFromConfigFile(
15011504
.map((item) =>
15021505
item.startsWith('!')
15031506
? `!${path.resolve(contentBase, item.slice(1))}`
1504-
: path.resolve(contentBase, item)
1507+
: path.resolve(contentBase, item),
15051508
)
15061509
.map((item) => ({
15071510
pattern: normalizePath(item),

0 commit comments

Comments
 (0)