Deduplicate classlist candidates (#572)

master
Brad Cornes 2022-07-06 16:40:29 +01:00 committed by GitHub
parent 05a8685c49
commit d09a4b11f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -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(

View File

@ -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 (