Skip to content

Commit d1af532

Browse files
committed
Update v3 support, color handling
1 parent 9e91134 commit d1af532

File tree

15 files changed

+551
-740
lines changed

15 files changed

+551
-740
lines changed

package-lock.json

+294-259
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tailwindcss-language-server/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
"access": "public"
2222
},
2323
"devDependencies": {
24-
"@ctrl/tinycolor": "3.1.4",
2524
"@parcel/watcher": "2.0.0-alpha.10",
2625
"@types/debounce": "1.2.0",
2726
"@types/node": "14.14.34",
2827
"@types/vscode": "1.52.0",
2928
"@vercel/ncc": "0.28.4",
3029
"builtin-modules": "3.2.0",
3130
"chokidar": "3.5.1",
31+
"color-name": "1.1.4",
32+
"culori": "0.20.1",
3233
"debounce": "1.2.0",
3334
"detective": "5.2.0",
3435
"dlv": "1.1.3",

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

+68-34
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ import {
6969
} from './lsp/diagnosticsProvider'
7070
import { doCodeActions } from 'tailwindcss-language-service/src/codeActions/codeActionProvider'
7171
import { getDocumentColors } from 'tailwindcss-language-service/src/documentColorProvider'
72-
import { fromRatio, names as namedColors } from '@ctrl/tinycolor'
7372
import { debounce } from 'debounce'
7473
import { getModuleDependencies } from './util/getModuleDependencies'
7574
import assert from 'assert'
7675
// import postcssLoadConfig from 'postcss-load-config'
7776
import * as parcel from './watcher/index.js'
77+
import { generateRules } from 'tailwindcss-language-service/src/util/jit'
78+
import { getColor } from 'tailwindcss-language-service/src/util/color'
79+
import * as culori from 'culori'
80+
import namedColors from 'color-name'
7881

7982
const CONFIG_FILE_GLOB = '{tailwind,tailwind.config}.{js,cjs}'
8083
const TRIGGER_CHARACTERS = [
@@ -101,7 +104,7 @@ declare var __non_webpack_require__: typeof require
101104
const connection =
102105
process.argv.length <= 2 ? createConnection(process.stdin, process.stdout) : createConnection()
103106

104-
console.log = connection.console.log.bind(connection.console)
107+
// console.log = connection.console.log.bind(connection.console)
105108
console.error = connection.console.error.bind(connection.console)
106109

107110
process.on('unhandledRejection', (e: any) => {
@@ -757,6 +760,9 @@ async function createProjectService(
757760
let presetVariants: any[] = []
758761
let originalConfig: any
759762

763+
// TODO
764+
let isV3 = semver.gte(tailwindcss.version, '2.99.0') || tailwindcss.version.includes('insiders')
765+
760766
let hook = new Hook(fs.realpathSync(state.configPath), (exports) => {
761767
originalConfig = klona(exports)
762768

@@ -783,7 +789,9 @@ async function createProjectService(
783789
delete exports.mode
784790

785791
// TODO
786-
if (false || (state.modules.jit && mode === 'jit')) {
792+
let isJit = isV3 || (state.modules.jit && mode === 'jit')
793+
794+
if (isJit) {
787795
state.jit = true
788796
exports.variants = []
789797

@@ -858,32 +866,43 @@ async function createProjectService(
858866
if (state.jit) {
859867
state.jitContext = state.modules.jit.createContext.module(state)
860868
state.jitContext.tailwindConfig.separator = state.config.separator
869+
if (state.jitContext.completions) {
870+
state.coreUtilities = state.jitContext.completions().map((item) => {
871+
let className = Array.isArray(item) ? item[0] : item
872+
return [className, { color: getColor(state, className) }]
873+
})
874+
}
861875
}
862876

863877
let postcssResult: Result
864-
try {
865-
postcssResult = await postcss
866-
.module([
867-
// ...state.postcssPlugins.before.map((x) => x()),
868-
tailwindcss.module(state.configPath),
869-
// ...state.postcssPlugins.after.map((x) => x()),
870-
])
871-
.process(
872-
[
873-
semver.gte(tailwindcss.version, '0.99.0') ? 'base' : 'preflight',
874-
'components',
875-
'utilities',
876-
]
877-
.map((x) => `/*__tw_intellisense_layer_${x}__*/\n@tailwind ${x};`)
878-
.join('\n'),
879-
{
880-
from: undefined,
881-
}
882-
)
883-
} catch (error) {
884-
throw error
885-
} finally {
878+
879+
if (state.coreUtilities) {
886880
hook.unhook()
881+
} else {
882+
try {
883+
postcssResult = await postcss
884+
.module([
885+
// ...state.postcssPlugins.before.map((x) => x()),
886+
tailwindcss.module(state.configPath),
887+
// ...state.postcssPlugins.after.map((x) => x()),
888+
])
889+
.process(
890+
[
891+
semver.gte(tailwindcss.version, '0.99.0') ? 'base' : 'preflight',
892+
'components',
893+
'utilities',
894+
]
895+
.map((x) => `/*__tw_intellisense_layer_${x}__*/\n@tailwind ${x};`)
896+
.join('\n'),
897+
{
898+
from: undefined,
899+
}
900+
)
901+
} catch (error) {
902+
throw error
903+
} finally {
904+
hook.unhook()
905+
}
887906
}
888907

889908
if (state.dependencies) {
@@ -895,7 +914,9 @@ async function createProjectService(
895914
state.configId = getConfigId(state.configPath, state.dependencies)
896915

897916
state.plugins = await getPlugins(originalConfig)
898-
state.classNames = (await extractClassNames(postcssResult.root)) as ClassNames
917+
if (postcssResult) {
918+
state.classNames = (await extractClassNames(postcssResult.root)) as ClassNames
919+
}
899920
state.variants = getVariants(state)
900921

901922
let screens = dlv(state.config, 'theme.screens', dlv(state.config, 'screens', {}))
@@ -969,16 +990,25 @@ async function createProjectService(
969990
let currentColor = match[1]
970991

971992
let isNamedColor = colorNames.includes(currentColor)
972-
let color = fromRatio({
993+
994+
let color: culori.RgbColor = {
995+
mode: 'rgb',
973996
r: params.color.red,
974997
g: params.color.green,
975998
b: params.color.blue,
976-
a: params.color.alpha,
977-
})
999+
alpha: params.color.alpha,
1000+
}
1001+
1002+
let hexValue = culori.formatHex8(color)
1003+
1004+
if (!isNamedColor && (currentColor.length === 4 || currentColor.length === 5)) {
1005+
let [, ...chars] =
1006+
hexValue.match(/^#([a-f\d])\1([a-f\d])\2([a-f\d])\3(?:([a-f\d])\4)?$/i) ?? []
1007+
if (chars.length) {
1008+
hexValue = `#${chars.filter(Boolean).join('')}`
1009+
}
1010+
}
9781011

979-
let hexValue = color.toHex8String(
980-
!isNamedColor && (currentColor.length === 4 || currentColor.length === 5)
981-
)
9821012
if (hexValue.length === 5) {
9831013
hexValue = hexValue.replace(/f$/, '')
9841014
} else if (hexValue.length === 9) {
@@ -989,8 +1019,12 @@ async function createProjectService(
9891019

9901020
return [
9911021
hexValue,
992-
color.toRgbString().replace(/ /g, ''),
993-
color.toHslString().replace(/ /g, ''),
1022+
culori.formatRgb(color).replace(/ /g, ''),
1023+
culori
1024+
.formatHsl(color)
1025+
.replace(/ /g, '')
1026+
// round numbers
1027+
.replace(/\d+\.\d+(%?)/g, (value, suffix) => `${Math.round(parseFloat(value))}${suffix}`),
9941028
].map((value) => ({ label: `${prefix}-[${value}]` }))
9951029
},
9961030
}

packages/tailwindcss-language-server/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
"tailwindcss-language-service/*": ["../packages/tailwindcss-language-service/*"]
1515
}
1616
},
17-
"include": ["src", "../packages/tailwindcss-language-service"]
17+
"include": ["src", "../packages/tailwindcss-language-service", "../../types"]
1818
}

0 commit comments

Comments
 (0)