Skip to content

Commit 07ad87e

Browse files
authored
Support class modifiers (#686)
1 parent 3289942 commit 07ad87e

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,9 @@ async function createProjectService(
991991
.getClassList()
992992
.filter((className) => className !== '*')
993993
.map((className) => {
994+
if (Array.isArray(className)) {
995+
return [className[0], { color: getColor(state, className[0]), ...className[1] }]
996+
}
994997
return [className, { color: getColor(state, className) }]
995998
})
996999
}

packages/tailwindcss-language-service/src/completionProvider.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,43 @@ export function completionsFromClassList(
6464
}
6565

6666
if (state.jit) {
67+
let { variants: existingVariants, offset } = getVariantsFromClassName(state, partialClassName)
68+
6769
if (
6870
context &&
6971
(context.triggerKind === 1 ||
7072
(context.triggerKind === 2 && context.triggerCharacter === '/')) &&
7173
partialClassName.includes('/')
7274
) {
73-
// opacity modifiers
75+
// modifiers
76+
let modifiers: string[]
7477
let beforeSlash = partialClassName.split('/').slice(0, -1).join('/')
75-
let testClass = beforeSlash + '/[0]'
76-
let { rules } = jit.generateRules(state, [testClass])
77-
if (rules.length > 0) {
78-
let opacities = dlv(state.config, 'theme.opacity', {})
79-
if (!isObject(opacities)) {
80-
opacities = {}
78+
let classListContainsModifiers = state.classList.some(
79+
(cls) => Array.isArray(cls) && cls[1].modifiers
80+
)
81+
82+
if (classListContainsModifiers) {
83+
let baseClassName = beforeSlash.slice(offset)
84+
modifiers = state.classList.find(
85+
(cls) => Array.isArray(cls) && cls[0] === baseClassName
86+
)?.[1].modifiers
87+
} else {
88+
let testClass = beforeSlash + '/[0]'
89+
let { rules } = jit.generateRules(state, [testClass])
90+
if (rules.length > 0) {
91+
let opacities = dlv(state.config, 'theme.opacity', {})
92+
if (!isObject(opacities)) {
93+
opacities = {}
94+
}
95+
modifiers = Object.keys(opacities)
8196
}
97+
}
98+
99+
if (modifiers) {
82100
return {
83101
isIncomplete: false,
84-
items: Object.keys(opacities).map((opacity, index) => {
85-
let className = `${beforeSlash}/${opacity}`
102+
items: modifiers.map((modifier, index) => {
103+
let className = `${beforeSlash}/${modifier}`
86104
let kind: CompletionItemKind = 21
87105
let documentation: string = null
88106

@@ -110,8 +128,6 @@ export function completionsFromClassList(
110128
}
111129
}
112130

113-
let { variants: existingVariants, offset } = getVariantsFromClassName(state, partialClassName)
114-
115131
replacementRange.start.character += offset
116132

117133
let important = partialClassName.substr(offset).startsWith('!')

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export interface State {
116116
editor?: EditorState
117117
jit?: boolean
118118
jitContext?: any
119-
classList?: Array<[string, { color: culori.Color | KeywordColor | null }]>
119+
classList?: Array<[string, { color: culori.Color | KeywordColor | null; modifiers?: string[] }]>
120120
pluginVersions?: string
121121
// postcssPlugins?: { before: any[]; after: any[] }
122122
}

0 commit comments

Comments
 (0)