Deduplicate classlist candidates (#572)
parent
05a8685c49
commit
d09a4b11f1
|
@ -11,6 +11,7 @@ import { getLanguageBoundaries } from './getLanguageBoundaries'
|
||||||
import { resolveRange } from './resolveRange'
|
import { resolveRange } from './resolveRange'
|
||||||
import dlv from 'dlv'
|
import dlv from 'dlv'
|
||||||
import { createMultiRegexp } from './createMultiRegexp'
|
import { createMultiRegexp } from './createMultiRegexp'
|
||||||
|
import { rangesEqual } from './rangesEqual'
|
||||||
|
|
||||||
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
|
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
|
||||||
let match: RegExpMatchArray
|
let match: RegExpMatchArray
|
||||||
|
@ -277,6 +278,13 @@ export async function findClassListsInHtmlRange(
|
||||||
return result
|
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(
|
export async function findClassListsInRange(
|
||||||
state: State,
|
state: State,
|
||||||
doc: TextDocument,
|
doc: TextDocument,
|
||||||
|
@ -290,7 +298,10 @@ export async function findClassListsInRange(
|
||||||
} else {
|
} else {
|
||||||
classLists = await findClassListsInHtmlRange(state, doc, range)
|
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(
|
export async function findClassListsInDocument(
|
||||||
|
@ -304,17 +315,19 @@ export async function findClassListsInDocument(
|
||||||
let boundaries = getLanguageBoundaries(state, doc)
|
let boundaries = getLanguageBoundaries(state, doc)
|
||||||
if (!boundaries) return []
|
if (!boundaries) return []
|
||||||
|
|
||||||
return flatten([
|
return dedupeClassLists(
|
||||||
...(await Promise.all(
|
flatten([
|
||||||
boundaries
|
...(await Promise.all(
|
||||||
.filter((b) => b.type === 'html' || b.type === 'jsx')
|
boundaries
|
||||||
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
|
.filter((b) => b.type === 'html' || b.type === 'jsx')
|
||||||
)),
|
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
|
||||||
...boundaries
|
)),
|
||||||
.filter((b) => b.type === 'css')
|
...boundaries
|
||||||
.map(({ range }) => findClassListsInCssRange(doc, range)),
|
.filter((b) => b.type === 'css')
|
||||||
await findCustomClassLists(state, doc),
|
.map(({ range }) => findClassListsInCssRange(doc, range)),
|
||||||
])
|
await findCustomClassLists(state, doc),
|
||||||
|
])
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function findHelperFunctionsInDocument(
|
export function findHelperFunctionsInDocument(
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Range } from 'vscode-languageserver'
|
import type { Range } from 'vscode-languageserver'
|
||||||
|
|
||||||
export function rangesEqual(a: Range, b: Range): boolean {
|
export function rangesEqual(a: Range, b: Range): boolean {
|
||||||
return (
|
return (
|
||||||
|
|
Loading…
Reference in New Issue