Skip to content

Commit 74aad3d

Browse files
committed
wip
1 parent dc6216f commit 74aad3d

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

packages/tailwindcss-language-server/src/util/v4/design-system.ts

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,36 +62,58 @@ export async function loadDesignSystem(
6262

6363
// TODO: Formatting with prettier would be preferable, but it's too slow
6464
// Need to figure out why and if we can make it faster
65-
css = css.map((str) => {
66-
if (!str) return null
65+
let roots = css.map((str) => {
66+
if (str === null) return postcss.root()
67+
68+
let result = ''
69+
for (let i = 0; i < str.length; ++i) {
70+
if (str[i] === '\\') {
71+
result += str[i] + str[i + 1]
72+
i += 1
73+
} else if (str[i] === '"') {
74+
let end = str.indexOf('"', i + 1)
75+
result += str.slice(i, end + 1)
76+
i = end
77+
} else if (str[i] === "'") {
78+
let end = str.indexOf("'", i + 1)
79+
result += str.slice(i, end + 1)
80+
i = end
81+
} else if (str[i] === '{') {
82+
result += ' {\n'
83+
} else if (str[i] === '}') {
84+
result += '}\n'
85+
} else if (str[i] === ';') {
86+
result += ';\n'
87+
} else if (str[i] === ':') {
88+
result += ': '
89+
} else {
90+
result += str[i]
91+
}
92+
}
6793

68-
let lines = str
69-
//
70-
.replaceAll('{', ' {\n')
71-
.replaceAll(';', ' ;\n')
72-
.replaceAll('}', ' }\n')
73-
.split('\n')
94+
let lines = result.split('\n')
7495

7596
let depth = 0
7697

7798
for (let i = 0; i < lines.length; ++i) {
7899
let line = lines[i]
79100
if (line.includes('}')) depth--
80-
let indent = ' '.repeat(depth)
101+
let indent = ' '.repeat(Math.max(0, depth))
81102
line = indent + line
82103
if (line.includes('{')) depth++
83104
lines[i] = line
84105
}
85106

86-
return lines.join('\n')
87-
})
107+
let pretty = lines.join('\n').trim()
88108

89-
let root = css.map((str) => {
90-
if (str === null) return postcss.root()
91-
return postcss.parse(str)
109+
try {
110+
return postcss.parse(pretty)
111+
} catch {
112+
return postcss.parse(str)
113+
}
92114
})
93115

94-
return root
116+
return roots
95117
},
96118

97119
toCss(nodes: postcss.Root | postcss.Node[]): string {

0 commit comments

Comments
 (0)