Skip to content

Commit

Permalink
Remove custom AST pretty printer
Browse files Browse the repository at this point in the history
This won’t be necessary as the alpha now always pretty prints an AST
  • Loading branch information
thecrypticace committed Mar 5, 2024
1 parent e871015 commit c3c1755
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 55 deletions.
55 changes: 2 additions & 53 deletions packages/tailwindcss-language-server/src/util/v4/design-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,64 +60,13 @@ export async function loadDesignSystem(
// return str
// })

// TODO: Formatting with prettier would be preferable, but it's too slow
// Need to figure out why and if we can make it faster
let roots = css.map((str) => {
if (str === null) return postcss.root()

let result = ''
for (let i = 0; i < str.length; ++i) {
if (str[i] === '\\') {
result += str[i] + str[i + 1]
i += 1
} else if (str[i] === '"') {
let end = str.indexOf('"', i + 1)
result += str.slice(i, end + 1)
i = end
} else if (str[i] === "'") {
let end = str.indexOf("'", i + 1)
result += str.slice(i, end + 1)
i = end
} else if (str[i] === '{') {
result += ' {\n'
} else if (str[i] === '}') {
result += '}\n'
} else if (str[i] === ';') {
result += ';\n'
} else if (str[i] === ':') {
let prev = str.charCodeAt(i - 1)
if (
(prev >= 65 && prev <= 90) ||
(prev >= 97 && prev <= 122) ||
(prev >= 48 && prev <= 57)
) {
result += ': '
} else {
result += ':'
}
} else {
result += str[i]
}
}

let lines = result.split('\n')

let depth = 0

for (let i = 0; i < lines.length; ++i) {
let line = lines[i]
if (line.includes('}')) depth--
let indent = ' '.repeat(Math.max(0, depth))
lines[i] = indent + line
if (line.includes('{')) depth++
}

let pretty = lines.join('\n').trim()

try {
return postcss.parse(pretty)
} catch {
return postcss.parse(str)
} catch {
return null
}
})

Expand Down
7 changes: 5 additions & 2 deletions packages/tailwindcss-language-service/src/util/jit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ export async function stringifyRoot(state: State, root: Root, uri?: string): Pro
css = addPixelEquivalentsToCss(css, settings.tailwindCSS.rootFontSize)
}

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

return css
.replace(/([^;{}\s])(\n\s*})/g, (_match, before, after) => `${before};${after}`)
.replace(/^(?: )+/gm, (indent: string) =>
' '.repeat((indent.length / 4) * settings.editor.tabSize)
.replace(identPattern, (indent: string) =>
' '.repeat((indent.length / identSize) * settings.editor.tabSize)
)
}

Expand Down

0 comments on commit c3c1755

Please sign in to comment.