Skip to content

Commit 9c60e0b

Browse files
committed
add @​layer completions
1 parent 4f0e415 commit 9c60e0b

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,45 @@ function provideVariantsDirectiveCompletions(
562562
}
563563
}
564564

565+
function provideLayerDirectiveCompletions(
566+
state: State,
567+
document: TextDocument,
568+
position: Position
569+
): CompletionList {
570+
if (!isCssContext(state, document, position)) {
571+
return null
572+
}
573+
574+
let text = document.getText({
575+
start: { line: position.line, character: 0 },
576+
end: position,
577+
})
578+
579+
const match = text.match(/^\s*@layer\s+(?<partial>[^\s]*)$/i)
580+
581+
if (match === null) return null
582+
583+
return {
584+
isIncomplete: false,
585+
items: ['base', 'components', 'utilities'].map((layer, index) => ({
586+
label: layer,
587+
kind: 21,
588+
data: 'layer',
589+
sortText: naturalExpand(index),
590+
textEdit: {
591+
newText: layer,
592+
range: {
593+
start: {
594+
line: position.line,
595+
character: position.character - match.groups.partial.length,
596+
},
597+
end: position,
598+
},
599+
},
600+
})),
601+
}
602+
}
603+
565604
function provideScreenDirectiveCompletions(
566605
state: State,
567606
document: TextDocument,
@@ -678,6 +717,20 @@ function provideCssDirectiveCompletions(
678717
)})`,
679718
},
680719
},
720+
...(semver.gte(state.version, '1.8.0')
721+
? [
722+
{
723+
label: '@layer',
724+
documentation: {
725+
kind: 'markdown' as typeof MarkupKind.Markdown,
726+
value: `Use the \`@layer\` directive to tell Tailwind which "bucket" a set of custom styles belong to. Valid layers are \`base\`, \`components\`, and \`utilities\`.\n\n[Tailwind CSS Documentation](${docsUrl(
727+
state.version,
728+
'functions-and-directives/#layer'
729+
)})`,
730+
},
731+
},
732+
]
733+
: []),
681734
]
682735

683736
return {
@@ -779,6 +832,7 @@ export async function doComplete(
779832
provideScreenDirectiveCompletions(state, document, position) ||
780833
provideVariantsDirectiveCompletions(state, document, position) ||
781834
provideTailwindDirectiveCompletions(state, document, position) ||
835+
provideLayerDirectiveCompletions(state, document, position) ||
782836
(await provideCustomClassNameCompletions(state, document, position))
783837

784838
if (result) return result
@@ -790,7 +844,9 @@ export async function resolveCompletionItem(
790844
state: State,
791845
item: CompletionItem
792846
): Promise<CompletionItem> {
793-
if (['helper', 'directive', 'variant', '@tailwind'].includes(item.data)) {
847+
if (
848+
['helper', 'directive', 'variant', 'layer', '@tailwind'].includes(item.data)
849+
) {
794850
return item
795851
}
796852

0 commit comments

Comments
 (0)