Skip to content

Deduplicate classlist candidates #572

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
Jul 6, 2022
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
37 changes: 25 additions & 12 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange'
import dlv from 'dlv'
import { createMultiRegexp } from './createMultiRegexp'
import { rangesEqual } from './rangesEqual'

export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
let match: RegExpMatchArray
Expand Down Expand Up @@ -277,6 +278,13 @@ export async function findClassListsInHtmlRange(
return result
}

function dedupeClassLists(classLists: DocumentClassList[]): DocumentClassList[] {
return classLists.filter(
(classList, classListIndex) =>
classListIndex === classLists.findIndex((c) => rangesEqual(c.range, classList.range))
)
}

export async function findClassListsInRange(
state: State,
doc: TextDocument,
Expand All @@ -290,7 +298,10 @@ export async function findClassListsInRange(
} else {
classLists = await findClassListsInHtmlRange(state, doc, range)
}
return [...classLists, ...(includeCustom ? await findCustomClassLists(state, doc, range) : [])]
return dedupeClassLists([
...classLists,
...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
])
}

export async function findClassListsInDocument(
Expand All @@ -304,17 +315,19 @@ export async function findClassListsInDocument(
let boundaries = getLanguageBoundaries(state, doc)
if (!boundaries) return []

return flatten([
...(await Promise.all(
boundaries
.filter((b) => b.type === 'html' || b.type === 'jsx')
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
)),
...boundaries
.filter((b) => b.type === 'css')
.map(({ range }) => findClassListsInCssRange(doc, range)),
await findCustomClassLists(state, doc),
])
return dedupeClassLists(
flatten([
...(await Promise.all(
boundaries
.filter((b) => b.type === 'html' || b.type === 'jsx')
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
)),
...boundaries
.filter((b) => b.type === 'css')
.map(({ range }) => findClassListsInCssRange(doc, range)),
await findCustomClassLists(state, doc),
])
)
}

export function findHelperFunctionsInDocument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Range } from 'vscode-languageserver'
import type { Range } from 'vscode-languageserver'

export function rangesEqual(a: Range, b: Range): boolean {
return (
Expand Down