Skip to content

Commit ba8a6ff

Browse files
author
Brad Cornes
committed
add directive completions
1 parent 7b8ee54 commit ba8a6ff

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

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

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,88 @@ function provideCssHelperCompletions(
286286
}
287287
}
288288

289+
function provideCssDirectiveCompletions(
290+
state: State,
291+
{ position, textDocument }: CompletionParams
292+
): CompletionList {
293+
let doc = state.editor.documents.get(textDocument.uri)
294+
295+
if (!isCssContext(doc, position)) {
296+
return null
297+
}
298+
299+
let text = doc.getText({
300+
start: { line: position.line, character: 0 },
301+
end: position,
302+
})
303+
304+
const match = text.match(/^\s*@(?<partial>[a-z]*)$/i)
305+
306+
if (match === null) return null
307+
308+
const items: CompletionItem[] = [
309+
{
310+
label: '@tailwind',
311+
documentation: {
312+
kind: MarkupKind.Markdown,
313+
value:
314+
'Use the `@tailwind` directive to insert Tailwind’s `base`, `components`, `utilities` and `screens` styles into your CSS.\n\n[Tailwind CSS Documentation](https://tailwindcss.com/docs/functions-and-directives#tailwind)',
315+
},
316+
},
317+
{
318+
label: '@variants',
319+
documentation: {
320+
kind: MarkupKind.Markdown,
321+
value:
322+
'You can generate `responsive`, `hover`, `focus`, `active`, and `group-hover` versions of your own utilities by wrapping their definitions in the `@variants` directive.\n\n[Tailwind CSS Documentation](https://tailwindcss.com/docs/functions-and-directives#variants)',
323+
},
324+
},
325+
{
326+
label: '@responsive',
327+
documentation: {
328+
kind: MarkupKind.Markdown,
329+
value:
330+
'You can generate responsive variants of your own classes by wrapping their definitions in the `@responsive` directive.\n\n[Tailwind CSS Documentation](https://tailwindcss.com/docs/functions-and-directives#responsive)',
331+
},
332+
},
333+
{
334+
label: '@screen',
335+
documentation: {
336+
kind: MarkupKind.Markdown,
337+
value:
338+
'The `@screen` directive allows you to create media queries that reference your breakpoints by name instead of duplicating their values in your own CSS.\n\n[Tailwind CSS Documentation](https://tailwindcss.com/docs/functions-and-directives#screen)',
339+
},
340+
},
341+
{
342+
label: '@apply',
343+
documentation: {
344+
kind: MarkupKind.Markdown,
345+
value:
346+
'Use `@apply` to inline any existing utility classes into your own custom CSS.\n\n[Tailwind CSS Documentation](https://tailwindcss.com/docs/functions-and-directives#apply)',
347+
},
348+
},
349+
]
350+
351+
return {
352+
isIncomplete: false,
353+
items: items.map((item) => ({
354+
...item,
355+
kind: CompletionItemKind.Keyword,
356+
data: 'directive',
357+
textEdit: {
358+
newText: item.label,
359+
range: {
360+
start: {
361+
line: position.line,
362+
character: position.character - match.groups.partial.length - 1,
363+
},
364+
end: position,
365+
},
366+
},
367+
})),
368+
}
369+
}
370+
289371
export function provideCompletions(
290372
state: State,
291373
params: CompletionParams
@@ -294,15 +376,16 @@ export function provideCompletions(
294376

295377
return (
296378
provideClassNameCompletions(state, params) ||
297-
provideCssHelperCompletions(state, params)
379+
provideCssHelperCompletions(state, params) ||
380+
provideCssDirectiveCompletions(state, params)
298381
)
299382
}
300383

301384
export function resolveCompletionItem(
302385
state: State,
303386
item: CompletionItem
304387
): CompletionItem {
305-
if (item.data === 'helper') {
388+
if (item.data === 'helper' || item.data === 'directive') {
306389
return item
307390
}
308391

0 commit comments

Comments
 (0)