Skip to content

Commit 85fb953

Browse files
committed
classRegex: increase search range, make search global (#129)
1 parent f95c881 commit 85fb953

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

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

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -205,56 +205,65 @@ async function provideCustomClassNameCompletions(
205205
const regexes = dlv(settings, 'experimental.classRegex', [])
206206
if (regexes.length === 0) return null
207207

208-
const searchRange = {
209-
start: { line: Math.max(position.line - 10, 0), character: 0 },
210-
end: { line: position.line + 10, character: 0 },
208+
const positionOffset = document.offsetAt(position)
209+
210+
const searchRange: Range = {
211+
start: document.positionAt(Math.max(0, positionOffset - 500)),
212+
end: document.positionAt(positionOffset + 500),
211213
}
212214

213215
let str = document.getText(searchRange)
214216

215217
for (let i = 0; i < regexes.length; i++) {
216-
let [containerRegex, classRegex] = Array.isArray(regexes[i])
217-
? regexes[i]
218-
: [regexes[i]]
219-
containerRegex = new MultiRegexp(new RegExp(containerRegex))
220218
try {
221-
const match = containerRegex.execForGroup(str, 1)
222-
if (match === null) {
223-
throw Error()
224-
}
225-
const searchStart = document.offsetAt(searchRange.start)
226-
const matchStart = searchStart + match.start
227-
const matchEnd = searchStart + match.end
228-
const cursor = document.offsetAt(position)
229-
if (cursor >= matchStart && cursor <= matchEnd) {
230-
let classList
231-
232-
if (classRegex) {
233-
classRegex = new MultiRegexp(new RegExp(classRegex, 'g'))
234-
let classMatch
235-
while (
236-
(classMatch = classRegex.execForGroup(match.match, 1)) !== null
237-
) {
238-
const classMatchStart = matchStart + classMatch.start
239-
const classMatchEnd = matchStart + classMatch.end
240-
if (cursor >= classMatchStart && cursor <= classMatchEnd) {
241-
classList = classMatch.match.substr(0, cursor - classMatchStart)
219+
let [containerRegex, classRegex] = Array.isArray(regexes[i])
220+
? regexes[i]
221+
: [regexes[i]]
222+
223+
containerRegex = new MultiRegexp(new RegExp(containerRegex, 'g'))
224+
let containerMatch
225+
226+
while ((containerMatch = containerRegex.execForGroup(str, 1)) !== null) {
227+
console.log(containerMatch)
228+
const searchStart = document.offsetAt(searchRange.start)
229+
const matchStart = searchStart + containerMatch.start
230+
const matchEnd = searchStart + containerMatch.end
231+
const cursor = document.offsetAt(position)
232+
if (cursor >= matchStart && cursor <= matchEnd) {
233+
let classList
234+
235+
if (classRegex) {
236+
classRegex = new MultiRegexp(new RegExp(classRegex, 'g'))
237+
let classMatch
238+
239+
while (
240+
(classMatch = classRegex.execForGroup(
241+
containerMatch.match,
242+
1
243+
)) !== null
244+
) {
245+
const classMatchStart = matchStart + classMatch.start
246+
const classMatchEnd = matchStart + classMatch.end
247+
if (cursor >= classMatchStart && cursor <= classMatchEnd) {
248+
classList = classMatch.match.substr(0, cursor - classMatchStart)
249+
}
242250
}
251+
252+
if (typeof classList === 'undefined') {
253+
throw Error()
254+
}
255+
} else {
256+
classList = containerMatch.match.substr(0, cursor - matchStart)
243257
}
244-
if (typeof classList === 'undefined') {
245-
throw Error()
246-
}
247-
} else {
248-
classList = match.match.substr(0, cursor - matchStart)
249-
}
250258

251-
return completionsFromClassList(state, classList, {
252-
start: {
253-
line: position.line,
254-
character: position.character - classList.length,
255-
},
256-
end: position,
257-
})
259+
return completionsFromClassList(state, classList, {
260+
start: {
261+
line: position.line,
262+
character: position.character - classList.length,
263+
},
264+
end: position,
265+
})
266+
}
258267
}
259268
} catch (_) {}
260269
}

0 commit comments

Comments
 (0)