Add modifier completions for `@apply` and `classRegex` configs (#732)

master
Brad Cornes 2023-03-13 10:29:11 +00:00 committed by GitHub
parent 05f8df0f60
commit cf2a5535b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 13 deletions

View File

@ -44,7 +44,6 @@ export function completionsFromClassList(
classList: string, classList: string,
classListRange: Range, classListRange: Range,
filter?: (item: CompletionItem) => boolean, filter?: (item: CompletionItem) => boolean,
document?: TextDocument,
context?: CompletionContext context?: CompletionContext
): CompletionList { ): CompletionList {
let classNames = classList.split(/[\s+]/) let classNames = classList.split(/[\s+]/)
@ -464,7 +463,6 @@ async function provideClassAttributeCompletions(
end: position, end: position,
}, },
undefined, undefined,
document,
context context
) )
} }
@ -476,7 +474,8 @@ async function provideClassAttributeCompletions(
async function provideCustomClassNameCompletions( async function provideCustomClassNameCompletions(
state: State, state: State,
document: TextDocument, document: TextDocument,
position: Position position: Position,
context?: CompletionContext
): Promise<CompletionList> { ): Promise<CompletionList> {
const settings = await state.editor.getConfiguration(document.uri) const settings = await state.editor.getConfiguration(document.uri)
const regexes = settings.tailwindCSS.experimental.classRegex const regexes = settings.tailwindCSS.experimental.classRegex
@ -527,13 +526,19 @@ async function provideCustomClassNameCompletions(
classList = containerMatch[1].substr(0, cursor - matchStart) classList = containerMatch[1].substr(0, cursor - matchStart)
} }
return completionsFromClassList(state, classList, { return completionsFromClassList(
start: { state,
line: position.line, classList,
character: position.character - classList.length, {
start: {
line: position.line,
character: position.character - classList.length,
},
end: position,
}, },
end: position, undefined,
}) context
)
} }
} }
} catch (_) {} } catch (_) {}
@ -545,7 +550,8 @@ async function provideCustomClassNameCompletions(
function provideAtApplyCompletions( function provideAtApplyCompletions(
state: State, state: State,
document: TextDocument, document: TextDocument,
position: Position position: Position,
context?: CompletionContext
): CompletionList { ): CompletionList {
let str = document.getText({ let str = document.getText({
start: { line: Math.max(position.line - 30, 0), character: 0 }, start: { line: Math.max(position.line - 30, 0), character: 0 },
@ -580,7 +586,8 @@ function provideAtApplyCompletions(
let className = item.data?.className ?? item.label let className = item.data?.className ?? item.label
let validated = validateApply(state, [...variants, className]) let validated = validateApply(state, [...variants, className])
return validated !== null && validated.isApplyable === true return validated !== null && validated.isApplyable === true
} },
context
) )
} }
@ -596,7 +603,7 @@ async function provideClassNameCompletions(
context?: CompletionContext context?: CompletionContext
): Promise<CompletionList> { ): Promise<CompletionList> {
if (isCssContext(state, document, position)) { if (isCssContext(state, document, position)) {
return provideAtApplyCompletions(state, document, position) return provideAtApplyCompletions(state, document, position, context)
} }
if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) { if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {
@ -1329,7 +1336,7 @@ export async function doComplete(
provideTailwindDirectiveCompletions(state, document, position) || provideTailwindDirectiveCompletions(state, document, position) ||
provideLayerDirectiveCompletions(state, document, position) || provideLayerDirectiveCompletions(state, document, position) ||
(await provideConfigDirectiveCompletions(state, document, position)) || (await provideConfigDirectiveCompletions(state, document, position)) ||
(await provideCustomClassNameCompletions(state, document, position)) (await provideCustomClassNameCompletions(state, document, position, context))
if (result) return result if (result) return result