Skip to content

Commit 0e89aca

Browse files
committed
Suggest @slot when inside a long form @variant rule
1 parent 47b26ea commit 0e89aca

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,23 @@ function provideCssDirectiveCompletions(
15401540
)})`,
15411541
},
15421542
})
1543+
1544+
// If we're inside an @variant directive, also add `@slot`
1545+
if (isInsideAtRule('variant', document, position)) {
1546+
items.push({
1547+
label: '@slot',
1548+
documentation: {
1549+
kind: 'markdown' as typeof MarkupKind.Markdown,
1550+
value: `Use the \`@slot\` directive to define where rules go in a custom variant.\n\n[Tailwind CSS Documentation](${docsUrl(
1551+
state.version,
1552+
'functions-and-directives/#slot',
1553+
)})`,
1554+
},
1555+
1556+
// Make sure this appears as the first at-rule
1557+
sortText: '-0000000',
1558+
})
1559+
}
15431560
}
15441561

15451562
return withDefaults(
@@ -1567,6 +1584,21 @@ function provideCssDirectiveCompletions(
15671584
)
15681585
}
15691586

1587+
function isInsideAtRule(name: string, document: TextDocument, position: Position) {
1588+
// 1. Get all text up to the current position
1589+
let text = document.getText({
1590+
start: { line: 0, character: 0 },
1591+
end: position,
1592+
})
1593+
1594+
// 2. Find the last instance of the at-rule
1595+
let block = text.lastIndexOf(`@${name}`)
1596+
if (block === -1) return false
1597+
1598+
// 4. Count the number of open and close braces following the rule to determine if we're inside it
1599+
return braceLevel(text.slice(block)) > 0
1600+
}
1601+
15701602
// Provide completions for directives that take file paths
15711603
async function provideFileDirectiveCompletions(
15721604
state: State,

0 commit comments

Comments
 (0)