Skip to content

Commit b32b3b0

Browse files
Make class search range larger (#1192)
After doing an admittedly quite small amount of benchmarking it seems that expanding the search range _does_ have a perf impact but it's not so much of one that we shouldn't increase it. Though this very likely depends on the regex in use. I've done a few major things in this PR: - Searching for class lists when hovering now starts at the beginning of the document. This important for users who are using things like `cva` (the same reason it was expanded for completions in #840) - The search range for completions inside a class attribute have been expanded to 15k characters. This is useful if you're using a really large cva definition in one class list. - Ditto for searching for custom regexes which is useful when using CVA with separate variables in a JSX/TSX document. Fixes #1032 Fixes #984 This is a mostly a stop-gap measure until I can land a better system for "parsing" documents that aims to make most class detection trivial and quite fast. Custom regexes won't necessarily be able to take advantage of that but if I can get some perf wins elsewhere I might be able to expand the search range even further. Ideally I'd figure out a way to run regexes in a worker thread and allow them to be "cancelled" when they take too long but that is a fairly major-ish undertaking.
1 parent c2660e4 commit b32b3b0

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ import { IS_SCRIPT_SOURCE, IS_TEMPLATE_SOURCE } from './metadata/extensions'
4242
import * as postcss from 'postcss'
4343
import { findFileDirective } from './completions/file-paths'
4444
import type { ThemeEntry } from './util/v4'
45-
import { posix } from 'node:path/win32'
4645
import { segment } from './util/segment'
4746
import { resolveKnownThemeKeys, resolveKnownThemeNamespaces } from './util/v4/theme-keys'
47+
import { SEARCH_RANGE } from './util/constants'
4848

4949
let isUtil = (className) =>
5050
Array.isArray(className.__info)
@@ -729,7 +729,7 @@ async function provideClassAttributeCompletions(
729729
context?: CompletionContext,
730730
): Promise<CompletionList> {
731731
let str = document.getText({
732-
start: document.positionAt(Math.max(0, document.offsetAt(position) - 2000)),
732+
start: document.positionAt(Math.max(0, document.offsetAt(position) - SEARCH_RANGE)),
733733
end: position,
734734
})
735735

@@ -796,7 +796,7 @@ async function provideCustomClassNameCompletions(
796796

797797
let text = document.getText({
798798
start: document.positionAt(0),
799-
end: document.positionAt(cursor + 2000),
799+
end: document.positionAt(cursor + SEARCH_RANGE),
800800
})
801801

802802
// Get completions from the first matching regex or regex pair
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* The maximum bounds around the cursor when searching for class names
3+
*/
4+
export const SEARCH_RANGE = 15_000

packages/tailwindcss-language-service/src/util/find.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { resolveRange } from './resolveRange'
1313
import { getTextWithoutComments } from './doc'
1414
import { isSemicolonlessCssLanguage } from './languages'
1515
import { customClassesIn } from './classes'
16+
import { SEARCH_RANGE } from './constants'
1617

1718
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
1819
let match: RegExpMatchArray
@@ -437,8 +438,8 @@ export async function findClassNameAtPosition(
437438
let classNames: DocumentClassName[] = []
438439
const positionOffset = doc.offsetAt(position)
439440
const searchRange: Range = {
440-
start: doc.positionAt(Math.max(0, positionOffset - 2000)),
441-
end: doc.positionAt(positionOffset + 2000),
441+
start: doc.positionAt(0),
442+
end: doc.positionAt(positionOffset + SEARCH_RANGE),
442443
}
443444

444445
if (isVueDoc(doc)) {

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Fix suggestion of utilities with slashes in them in v4 ([#1182](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1182))
1212
- Assume 16px font size for `1rem` in media queries ([#1190](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1190))
1313
- Show warning when loading a config in v3 fails ([#1191](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1191))
14+
- Better handle really long class lists in attributes and custom regexes ([#1192](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1192))
1415

1516
## 0.14.3
1617

0 commit comments

Comments
 (0)