Skip to content

Fix(frontend): auto completion and render of tailwind classes in app editor #5817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions frontend/src/lib/components/SimpleEditor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -467,33 +467,46 @@
}

function addTailwindClassCompletions() {
// Define a custom word definition for Tailwind classes
languages.setMonarchTokensProvider('tailwindcss', {
tokenizer: {
root: [[/[a-zA-Z0-9-]+/, 'tailwind-class']]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider updating the Monarch token regex to include additional characters (like :) for Tailwind variant syntax (e.g. md:text-center).

Suggested change
root: [[/[a-zA-Z0-9-]+/, 'tailwind-class']]
root: [[/[a-zA-Z0-9:-]+/, 'tailwind-class']]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern [a-zA-Z0-9-]+ doesn't account for all valid Tailwind class characters. Tailwind classes commonly include colons for variants (e.g., hover:, focus:) and slashes for fractional values (e.g., w-1/2).

Consider updating the pattern to:

/[a-zA-Z0-9-:\/]+/

This would ensure proper recognition of the full range of Tailwind class syntax during autocompletion.

Suggested change
root: [[/[a-zA-Z0-9-]+/, 'tailwind-class']]
root: [[/[a-zA-Z0-9-:\/]+/, 'tailwind-class']]

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

}
})

languages.registerCompletionItemProvider('tailwindcss', {
triggerCharacters: ['-'],
provideCompletionItems: function (model, position, context, token) {
const word = model.getWordUntilPosition(position)
const wordUntilPosition = model.getWordUntilPosition(position)
const lineContent = model.getLineContent(position.lineNumber)

// Get the text from the start of the line to the cursor
const textUntilPosition = lineContent.substring(0, position.column - 1)
// Find the last space before the cursor
const lastSpaceIndex = textUntilPosition.lastIndexOf(' ')
const startColumn = lastSpaceIndex === -1 ? 1 : lastSpaceIndex + 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calculation lastSpaceIndex + 2 appears to position the cursor two characters after a space, which may skip a character. Consider using lastSpaceIndex + 1 instead to position the cursor immediately after the space. This would ensure the completion range starts at the correct position for the current word being typed.

Suggested change
const startColumn = lastSpaceIndex === -1 ? 1 : lastSpaceIndex + 2
const startColumn = lastSpaceIndex === -1 ? 1 : lastSpaceIndex + 1

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.


const range = {
startLineNumber: position.lineNumber,
startColumn: word.startColumn,
startColumn: startColumn,
endLineNumber: position.lineNumber,
endColumn: word.endColumn
endColumn: position.column
}

if (word && word.word) {
const currentWord = word.word

const suggestions = tailwindClasses
.filter((className) => className.includes(currentWord))
.map((className) => ({
label: className,
kind: languages.CompletionItemKind.Class,
insertText: className,
documentation: 'Custom CSS class',
range: range
}))
const currentWord = wordUntilPosition.word

return { suggestions }
}
const suggestions = tailwindClasses
.filter((className) => className.includes(currentWord))
.map((className) => ({
label: className,
kind: languages.CompletionItemKind.Class,
insertText: className,
documentation: 'Tailwind CSS class',
range: range,
preselect: true
}))

return { suggestions: [] }
return { suggestions }
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/tailwind.config.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const plugin = require('tailwindcss/plugin')
const { tailwindClasses } = require('./src/lib/components/apps/editor/componentsPanel/tailwindUtils')

const lightTheme = {
surface: '#ffffff',
Expand Down Expand Up @@ -80,7 +81,8 @@ const config = {
'autocomplete-list-item',
'autocomplete-list-item-create',
'selected',
'wm-tab-selected'
'wm-tab-selected',
...tailwindClasses
],
theme: {
colors: {
Expand Down