Skip to content

Commit d1e4806

Browse files
committed
Cache compiled path matchers
1 parent 54c35ad commit d1e4806

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import picomatch from 'picomatch'
2+
import { DefaultMap } from './util/default-map'
3+
4+
export interface PathMatcher {
5+
anyMatches(pattern: string, paths: string[]): boolean
6+
clear(): void
7+
}
8+
9+
export function createPathMatcher(): PathMatcher {
10+
let matchers = new DefaultMap<string, picomatch.Matcher>((pattern) => {
11+
// Escape picomatch special characters so they're matched literally
12+
pattern = pattern.replace(/[\[\]{}()]/g, (m) => `\\${m}`)
13+
14+
return picomatch(pattern, { dot: true })
15+
})
16+
17+
return {
18+
anyMatches: (pattern, paths) => {
19+
let check = matchers.get(pattern)
20+
return paths.some((path) => check(path))
21+
},
22+
clear: () => matchers.clear(),
23+
}
24+
}

packages/tailwindcss-language-server/src/tw.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import { ProjectLocator, type ProjectConfig } from './project-locator'
5656
import type { TailwindCssSettings } from '@tailwindcss/language-service/src/util/state'
5757
import { createResolver, Resolver } from './resolver'
5858
import { analyzeStylesheet } from './version-guesser.js'
59+
import { createPathMatcher, PathMatcher } from './matching.js'
5960

6061
const TRIGGER_CHARACTERS = [
6162
// class attributes
@@ -104,12 +105,14 @@ export class TW {
104105
private watched: string[] = []
105106

106107
private settingsCache: SettingsCache
108+
private pathMatcher: PathMatcher
107109

108110
constructor(private connection: Connection) {
109111
this.documentService = new DocumentService(this.connection)
110112
this.projects = new Map()
111113
this.projectCounter = 0
112114
this.settingsCache = createSettingsCache(connection)
115+
this.pathMatcher = createPathMatcher()
113116
}
114117

115118
async init(): Promise<void> {
@@ -151,6 +154,7 @@ export class TW {
151154
private async _init(): Promise<void> {
152155
clearRequireCache()
153156

157+
this.pathMatcher.clear()
154158
let folders = this.getWorkspaceFolders().map((folder) => normalizePath(folder.uri))
155159

156160
if (folders.length === 0) {
@@ -961,32 +965,20 @@ export class TW {
961965
}
962966

963967
for (let selector of project.documentSelector()) {
964-
let pattern = selector.pattern.replace(/[\[\]{}()]/g, (m) => `\\${m}`)
965-
966-
if (pattern.startsWith('!')) {
967-
if (picomatch(pattern.slice(1), { dot: true })(fsPath)) {
968-
break
969-
}
970-
971-
if (picomatch(pattern.slice(1), { dot: true })(normalPath)) {
972-
break
973-
}
968+
if (
969+
selector.pattern.startsWith('!') &&
970+
this.pathMatcher.anyMatches(selector.pattern.slice(1), [fsPath, normalPath])
971+
) {
972+
break
974973
}
975974

976-
if (selector.priority < matchedPriority) {
977-
if (picomatch(pattern, { dot: true })(fsPath)) {
978-
matchedProject = project
979-
matchedPriority = selector.priority
980-
981-
continue
982-
}
983-
984-
if (picomatch(pattern, { dot: true })(normalPath)) {
985-
matchedProject = project
986-
matchedPriority = selector.priority
987-
988-
continue
989-
}
975+
if (
976+
selector.priority < matchedPriority &&
977+
this.pathMatcher.anyMatches(selector.pattern, [fsPath, normalPath])
978+
) {
979+
matchedProject = project
980+
matchedPriority = selector.priority
981+
continue
990982
}
991983
}
992984
}

0 commit comments

Comments
 (0)