Skip to content

Commit ec2a903

Browse files
committed
Add TWC support
1 parent 9d7006d commit ec2a903

File tree

6 files changed

+43
-7
lines changed

6 files changed

+43
-7
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tailwindcss-language-server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@tailwindcss/language-server",
33
"description": "Tailwind CSS Language Server",
44
"license": "MIT",
5-
"version": "0.0.16",
5+
"version": "0.0.16-andre",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/tailwindlabs/tailwindcss-intellisense.git",

packages/tailwindcss-language-service/src/util/colorEquivalents.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import type { State } from './state'
12
import type { Plugin } from 'postcss'
23
import parseValue from 'postcss-value-parser'
34
import { inGamut } from 'culori'
45
import { formatColor, getColorFromValue } from './color'
56
import type { Comment } from './comments'
67

8+
const TW_REGEX = /--twc-(\w+)-(\d+)/
79
let allowedFunctions = ['rgb', 'rgba', 'hsl', 'hsla', 'lch', 'lab', 'oklch', 'oklab']
810

9-
export function equivalentColorValues({ comments }: { comments: Comment[] }): Plugin {
11+
export function equivalentColorValues(state: State, { comments }: { comments: Comment[] }): Plugin {
1012
return {
1113
postcssPlugin: 'plugin',
1214
Declaration(decl) {
@@ -23,12 +25,35 @@ export function equivalentColorValues({ comments }: { comments: Comment[] }): Pl
2325
return false
2426
}
2527

28+
const twcNode = node.nodes.filter(
29+
(n) => n.type === 'function' && n.nodes.length && n.nodes[0].value.startsWith('--twc-'),
30+
)
31+
if (twcNode) {
32+
const res = decl.value.match(TW_REGEX)
33+
if (res) {
34+
const [, color, value] = res
35+
const light = state.config.theme.colors[color][value]
36+
const dark = state.config.theme.darkColors[color][value]
37+
38+
comments.push({
39+
index:
40+
decl.source.start.offset +
41+
`${decl.prop}${decl.raws.between}`.length +
42+
node.sourceEndIndex,
43+
value: `${light} ${dark}`,
44+
})
45+
46+
return false
47+
}
48+
}
49+
2650
const values = node.nodes.filter((n) => n.type === 'word').map((n) => n.value)
2751
if (values.length < 3) {
2852
return false
2953
}
3054

3155
const color = getColorFromValue(`${node.value}(${values.join(' ')})`)
56+
3257
if (!inGamut('rgb')(color)) {
3358
return false
3459
}

packages/tailwindcss-language-service/src/util/equivalents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { TailwindCssSettings } from './state'
1+
import type { TailwindCssSettings, State } from './state'
22
import { equivalentPixelValues } from './pixelEquivalents'
33
import { equivalentColorValues } from './colorEquivalents'
44
import postcss, { type AcceptedPlugin } from 'postcss'
55
import { applyComments, type Comment } from './comments'
66

7-
export function addEquivalents(css: string, settings: TailwindCssSettings): string {
7+
export function addEquivalents(css: string, settings: TailwindCssSettings, state: State): string {
88
let comments: Comment[] = []
99

1010
let plugins: AcceptedPlugin[] = []
@@ -18,7 +18,7 @@ export function addEquivalents(css: string, settings: TailwindCssSettings): stri
1818
)
1919
}
2020

21-
plugins.push(equivalentColorValues({ comments }))
21+
plugins.push(equivalentColorValues(state, { comments }))
2222

2323
try {
2424
postcss(plugins).process(css, { from: undefined }).css

packages/tailwindcss-language-service/src/util/jit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function stringifyRoot(state: State, root: Root, uri?: string): Pro
4444

4545
let css = clone.toString()
4646

47-
css = addEquivalents(css, settings.tailwindCSS)
47+
css = addEquivalents(css, settings.tailwindCSS, state)
4848

4949
let identSize = state.v4 ? 2 : 4
5050
let identPattern = state.v4 ? /^(?: )+/gm : /^(?: )+/gm

packages/vscode-tailwindcss/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## Andre note to self
2+
3+
```
4+
git clone https://github.com/apazzolini/tailwindcss-intellisense.git
5+
cd tailwindcss-intellisense
6+
npm i
7+
cd packages/tailwindcss-language-server
8+
npm run build
9+
npm i -g $(pwd)
10+
```
11+
112
<img src="https://raw.githubusercontent.com/bradlc/vscode-tailwindcss/master/packages/vscode-tailwindcss/.github/banner.png" alt="" />
213

314
Tailwind CSS IntelliSense enhances the Tailwind development experience by providing Visual Studio Code users with advanced features such as autocomplete, syntax highlighting, and linting.

0 commit comments

Comments
 (0)