Skip to content

Exclude classes in blocklist from IntelliSense features #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/tailwindcss-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,8 @@ async function createProjectService(
try {
state.config = resolveConfig.module(originalConfig)
state.separator = state.config.separator
state.blocklist = Array.isArray(state.config.blocklist) ? state.config.blocklist : []
delete state.config.blocklist

if (state.jit) {
state.jitContext = state.modules.jit.createContext.module(state)
Expand Down
16 changes: 12 additions & 4 deletions packages/tailwindcss-language-service/src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,29 @@ export function completionsFromClassList(
{
isIncomplete: false,
items: items.concat(
state.classList.map(([className, { color }], index) => {
state.classList.reduce<CompletionItem[]>((items, [className, { color }], index) => {
if (
state.blocklist?.includes([...existingVariants, className].join(state.separator))
) {
return items
}

let kind: CompletionItemKind = color ? 16 : 21
let documentation: string | undefined

if (color && typeof color !== 'string') {
documentation = culori.formatRgb(color)
}

return {
items.push({
label: className,
kind,
...(documentation ? { documentation } : {}),
sortText: naturalExpand(index, state.classList.length),
} as CompletionItem
})
})

return items
}, [] as CompletionItem[])
),
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function getCssConflictDiagnostics(
const classLists = await findClassListsInDocument(state, document)

classLists.forEach((classList) => {
const classNames = getClassNamesInClassList(classList)
const classNames = getClassNamesInClassList(classList, state.blocklist)

classNames.forEach((className, index) => {
if (state.jit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function getRecommendedVariantOrderDiagnostics(
const classLists = await findClassListsInDocument(state, document)

classLists.forEach((classList) => {
const classNames = getClassNamesInClassList(classList)
const classNames = getClassNamesInClassList(classList, state.blocklist)
classNames.forEach((className) => {
let { rules } = jit.generateRules(state, [className.className])
if (rules.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getDocumentColors(

let classLists = await findClassListsInDocument(state, document)
classLists.forEach((classList) => {
let classNames = getClassNamesInClassList(classList)
let classNames = getClassNamesInClassList(classList, state.blocklist)
classNames.forEach((className) => {
let color = getColor(state, className.className)
if (color === null || typeof color === 'string' || (color.alpha ?? 1) === 0) {
Expand Down
19 changes: 11 additions & 8 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ export function findLast(re: RegExp, str: string): RegExpMatchArray {
return matches[matches.length - 1]
}

export function getClassNamesInClassList({
classList,
range,
important,
}: DocumentClassList): DocumentClassName[] {
export function getClassNamesInClassList(
{ classList, range, important }: DocumentClassList,
blocklist: State['blocklist']
): DocumentClassName[] {
const parts = classList.split(/(\s+)/)
const names: DocumentClassName[] = []
let index = 0
for (let i = 0; i < parts.length; i++) {
if (i % 2 === 0) {
if (i % 2 === 0 && !blocklist.includes(parts[i])) {
const start = indexToPosition(classList, index)
const end = indexToPosition(classList, index + parts[i].length)
names.push({
Expand Down Expand Up @@ -77,15 +76,19 @@ export async function findClassNamesInRange(
includeCustom: boolean = true
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
return flatten(classLists.map(getClassNamesInClassList))
return flatten(
classLists.map((classList) => getClassNamesInClassList(classList, state.blocklist))
)
}

export async function findClassNamesInDocument(
state: State,
doc: TextDocument
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInDocument(state, doc)
return flatten(classLists.map(getClassNamesInClassList))
return flatten(
classLists.map((classList) => getClassNamesInClassList(classList, state.blocklist))
)
}

export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
Expand Down
1 change: 1 addition & 0 deletions packages/tailwindcss-language-service/src/util/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface State {
screens?: string[]
variants?: Variant[]
corePlugins?: string[]
blocklist?: unknown[]
modules?: {
tailwindcss?: { version: string; module: any }
postcss?: { version: string; module: Postcss }
Expand Down