Skip to content

Commit 155377f

Browse files
committed
Fix rewritten style rule offsets
1 parent 724db8c commit 155377f

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class CssServer {
225225
if (match) {
226226
symbol.name = `${match[1]} ${match[2]?.trim() ?? match[3]?.trim()}`
227227
}
228-
} else if (symbol.name === `.placeholder`) {
228+
} else if (/^\._+$/.test(symbol.name)) {
229229
let doc = documents.get(symbol.location.uri)
230230
let text = doc.getText(symbol.location.range)
231231
let match = text.trim().match(/^(@[^\s]+)(?:([^{]+)[{]|([^;{]+);)/)

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ test('@utility', () => {
5757

5858
let output = [
5959
//
60-
'.placeholder {', // wrong
60+
'._______ {',
6161
' color: red;',
6262
'}',
63-
'.placeholder {', // wrong
63+
'._______ {',
6464
' color: red;',
6565
'}',
66-
'.placeholder {', // wrong
66+
'._______ {',
6767
' color: --value(--font-_-line-height);',
6868
'}',
6969
]
@@ -92,14 +92,14 @@ test('@theme', () => {
9292

9393
let output = [
9494
//
95-
'.placeholder {', // wrong
95+
'._____ {',
9696
' --color: red;',
9797
' --_: initial;',
9898
' --text_: initial;',
9999
' --font-_: initial;',
100100
' --font-weight-_: initial;',
101101
'}',
102-
'.placeholder {', // wrong
102+
'._____ {',
103103
' --color: red;',
104104
' --_: initial;',
105105
' --text_: initial;',
@@ -124,8 +124,8 @@ test('@custom-variant', () => {
124124

125125
let output = [
126126
//
127-
'@media (℘) {}', // wrong
128-
'.placeholder {', // wrong
127+
'@media(℘) {}',
128+
'.______________ {',
129129
' &:hover {',
130130
' @slot;',
131131
' }',
@@ -147,7 +147,7 @@ test('@variant', () => {
147147

148148
let output = [
149149
//
150-
'.placeholder {', // wrong
150+
'._______ {',
151151
' &:hover {',
152152
' @slot;',
153153
' }',

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ function replaceWithAtRule(delta = 0) {
1414
}
1515

1616
function replaceWithStyleRule(delta = 0) {
17-
return (_match: string, p1: string) => {
17+
return (_match: string, name: string, p1: string) => {
18+
let className = '_'.repeat(name.length)
1819
let spaces = ' '.repeat(p1.length + delta)
19-
return `.placeholder${spaces}{`
20+
return `.${className}${spaces}{`
2021
}
2122
}
2223

@@ -36,16 +37,16 @@ export function rewriteCss(css: string) {
3637
css = css.replace(/@screen(\s+[^{]+){/g, replaceWithAtRule(-2))
3738
css = css.replace(/@variants(\s+[^{]+){/g, replaceWithAtRule())
3839
css = css.replace(/@responsive(\s*){/g, replaceWithAtRule())
39-
css = css.replace(/@utility(\s+[^{]+){/g, replaceWithStyleRule())
40-
css = css.replace(/@theme(\s+[^{]*){/g, replaceWithStyleRule())
40+
css = css.replace(/@(utility)(\s+[^{]+){/g, replaceWithStyleRule())
41+
css = css.replace(/@(theme)(\s+[^{]*){/g, replaceWithStyleRule())
4142

42-
css = css.replace(/@custom-variant(\s+[^;{]+);/g, (match: string) => {
43-
let spaces = ' '.repeat(match.length - 11)
44-
return `@media (${MEDIA_MARKER})${spaces}{}`
43+
css = css.replace(/@(custom-variant)(\s+[^;{]+);/g, (match: string, name: string) => {
44+
let spaces = ' '.repeat(match.length - name.length + 3)
45+
return `@media(${MEDIA_MARKER})${spaces}{}`
4546
})
4647

47-
css = css.replace(/@custom-variant(\s+[^{]+){/g, replaceWithStyleRule())
48-
css = css.replace(/@variant(\s+[^{]+){/g, replaceWithStyleRule())
48+
css = css.replace(/@(custom-variant)(\s+[^{]+){/g, replaceWithStyleRule())
49+
css = css.replace(/@(variant)(\s+[^{]+){/g, replaceWithStyleRule())
4950
css = css.replace(/@layer(\s+[^{]{2,}){/g, replaceWithAtRule(-3))
5051
css = css.replace(/@reference\s*([^;]{2,})/g, '@import $1')
5152

packages/tailwindcss-language-server/tests/css/css-server.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defineTest({
3838
uri: '{workspace:default}/file-1.css',
3939
range: {
4040
start: { line: 1, character: 0 },
41-
end: { line: 1, character: 31 },
41+
end: { line: 1, character: 30 },
4242
},
4343
},
4444
},

0 commit comments

Comments
 (0)