classRegex: increase search range, make search global (#129)

master
Brad Cornes 2020-12-01 16:35:46 +00:00
parent f95c881b45
commit 85fb953d0a
1 changed files with 50 additions and 41 deletions

View File

@ -205,56 +205,65 @@ async function provideCustomClassNameCompletions(
const regexes = dlv(settings, 'experimental.classRegex', []) const regexes = dlv(settings, 'experimental.classRegex', [])
if (regexes.length === 0) return null if (regexes.length === 0) return null
const searchRange = { const positionOffset = document.offsetAt(position)
start: { line: Math.max(position.line - 10, 0), character: 0 },
end: { line: position.line + 10, character: 0 }, const searchRange: Range = {
start: document.positionAt(Math.max(0, positionOffset - 500)),
end: document.positionAt(positionOffset + 500),
} }
let str = document.getText(searchRange) let str = document.getText(searchRange)
for (let i = 0; i < regexes.length; i++) { for (let i = 0; i < regexes.length; i++) {
let [containerRegex, classRegex] = Array.isArray(regexes[i])
? regexes[i]
: [regexes[i]]
containerRegex = new MultiRegexp(new RegExp(containerRegex))
try { try {
const match = containerRegex.execForGroup(str, 1) let [containerRegex, classRegex] = Array.isArray(regexes[i])
if (match === null) { ? regexes[i]
throw Error() : [regexes[i]]
}
const searchStart = document.offsetAt(searchRange.start)
const matchStart = searchStart + match.start
const matchEnd = searchStart + match.end
const cursor = document.offsetAt(position)
if (cursor >= matchStart && cursor <= matchEnd) {
let classList
if (classRegex) { containerRegex = new MultiRegexp(new RegExp(containerRegex, 'g'))
classRegex = new MultiRegexp(new RegExp(classRegex, 'g')) let containerMatch
let classMatch
while ( while ((containerMatch = containerRegex.execForGroup(str, 1)) !== null) {
(classMatch = classRegex.execForGroup(match.match, 1)) !== null console.log(containerMatch)
) { const searchStart = document.offsetAt(searchRange.start)
const classMatchStart = matchStart + classMatch.start const matchStart = searchStart + containerMatch.start
const classMatchEnd = matchStart + classMatch.end const matchEnd = searchStart + containerMatch.end
if (cursor >= classMatchStart && cursor <= classMatchEnd) { const cursor = document.offsetAt(position)
classList = classMatch.match.substr(0, cursor - classMatchStart) if (cursor >= matchStart && cursor <= matchEnd) {
let classList
if (classRegex) {
classRegex = new MultiRegexp(new RegExp(classRegex, 'g'))
let classMatch
while (
(classMatch = classRegex.execForGroup(
containerMatch.match,
1
)) !== null
) {
const classMatchStart = matchStart + classMatch.start
const classMatchEnd = matchStart + classMatch.end
if (cursor >= classMatchStart && cursor <= classMatchEnd) {
classList = classMatch.match.substr(0, cursor - classMatchStart)
}
} }
}
if (typeof classList === 'undefined') {
throw Error()
}
} else {
classList = match.match.substr(0, cursor - matchStart)
}
return completionsFromClassList(state, classList, { if (typeof classList === 'undefined') {
start: { throw Error()
line: position.line, }
character: position.character - classList.length, } else {
}, classList = containerMatch.match.substr(0, cursor - matchStart)
end: position, }
})
return completionsFromClassList(state, classList, {
start: {
line: position.line,
character: position.character - classList.length,
},
end: position,
})
}
} }
} catch (_) {} } catch (_) {}
} }