diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 6aa6d24f3d50ad353acf2173cb140d53484f1cf9..825a2e334412bea16ebd4e5e37b1b5a087005d55 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -197,62 +197,6 @@ return null } -function createMultiRegexp(regexString: string) { - let insideCharClass = false - let captureGroupIndex = -1 - - for (let i = 0; i < regexString.length; i++) { - if ( - !insideCharClass && - regexString[i] === '[' && - regexString[i - 1] !== '\\' - ) { - insideCharClass = true - } else if ( - insideCharClass && - regexString[i] === ']' && - regexString[i - 1] !== '\\' - ) { - insideCharClass = false - } else if ( - !insideCharClass && - regexString[i] === '(' && - regexString.substr(i + 1, 2) !== '?:' - ) { - captureGroupIndex = i - break - } - } - - const re = /(?:[^\\]|^)\(\?:/g - let match: RegExpExecArray - let nonCaptureGroupIndexes: number[] = [] - - while ((match = re.exec(regexString)) !== null) { - if (match[0].startsWith('(')) { - nonCaptureGroupIndexes.push(match.index) - } else { - nonCaptureGroupIndexes.push(match.index + 1) - } - } - - const regex = new MultiRegexp( - new RegExp( - regexString.replace(re, (m) => m.substr(0, m.length - 2)), - 'g' - ) - ) - - let groupIndex = - 1 + nonCaptureGroupIndexes.filter((i) => i < captureGroupIndex).length - - return { - exec: (str: string) => { - return regex.execForGroup(str, groupIndex) - }, - } -} - async function provideCustomClassNameCompletions( state: State, document: TextDocument, @@ -277,10 +221,10 @@ let [containerRegex, classRegex] = Array.isArray(regexes[i]) ? regexes[i] : [regexes[i]] - containerRegex = createMultiRegexp(containerRegex) + containerRegex = new MultiRegexp(new RegExp(containerRegex, 'g')) let containerMatch - while ((containerMatch = containerRegex.exec(str)) !== null) { + while ((containerMatch = containerRegex.execForGroup(str, 1)) !== null) { const searchStart = document.offsetAt(searchRange.start) const matchStart = searchStart + containerMatch.start const matchEnd = searchStart + containerMatch.end @@ -289,11 +233,14 @@ if (cursor >= matchStart && cursor <= matchEnd) { let classList if (classRegex) { - classRegex = createMultiRegexp(classRegex) + classRegex = new MultiRegexp(new RegExp(classRegex, 'g')) let classMatch while ( - (classMatch = classRegex.exec(containerMatch.match)) !== null + (classMatch = classRegex.execForGroup( + containerMatch.match, + 1 + )) !== null ) { const classMatchStart = matchStart + classMatch.start const classMatchEnd = matchStart + classMatch.end