Fix IntelliSense when separator is `--` (#628)

* Fix IntelliSense when separator is `--`

* Tidy up
master
Brad Cornes 2022-10-13 09:23:19 +01:00 committed by GitHub
parent d073bb9de6
commit c9acd0d124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 56 deletions

View File

@ -1504,7 +1504,10 @@ class TW {
resolveProvider: true, resolveProvider: true,
triggerCharacters: [ triggerCharacters: [
...TRIGGER_CHARACTERS, ...TRIGGER_CHARACTERS,
...projects.map((project) => project.state.separator).filter(Boolean), ...projects
.map((project) => project.state.separator)
.filter((sep) => typeof sep === 'string')
.map((sep) => sep.slice(-1)),
].filter(Boolean), ].filter(Boolean),
}) })

View File

@ -6,7 +6,7 @@ export function getVariantsFromClassName(
className: string className: string
): { variants: string[]; offset: number } { ): { variants: string[]; offset: number } {
let allVariants = Object.keys(state.variants) let allVariants = Object.keys(state.variants)
let parts = Array.from(splitAtTopLevelOnly(className, state.separator)).filter(Boolean) let parts = splitAtTopLevelOnly(className, state.separator).filter(Boolean)
let variants = new Set<string>() let variants = new Set<string>()
let offset = 0 let offset = 0
@ -29,66 +29,34 @@ export function getVariantsFromClassName(
return { variants: Array.from(variants), offset } return { variants: Array.from(variants), offset }
} }
const REGEX_SPECIAL = /[\\^$.*+?()[\]{}|]/g // https://github.com/tailwindlabs/tailwindcss/blob/a8a2e2a7191fbd4bee044523aecbade5823a8664/src/util/splitAtTopLevelOnly.js
const REGEX_HAS_SPECIAL = RegExp(REGEX_SPECIAL.source) function splitAtTopLevelOnly(input: string, separator: string): string[] {
let stack: string[] = []
let parts: string[] = []
let lastPos = 0
function regexEscape(string: string): string { for (let idx = 0; idx < input.length; idx++) {
return string && REGEX_HAS_SPECIAL.test(string) let char = input[idx]
? string.replace(REGEX_SPECIAL, '\\$&')
: string || ''
}
function* splitAtTopLevelOnly(input: string, separator: string): Generator<string> { if (stack.length === 0 && char === separator[0]) {
let SPECIALS = new RegExp(`[(){}\\[\\]${regexEscape(separator)}]`, 'g') if (separator.length === 1 || input.slice(idx, idx + separator.length) === separator) {
parts.push(input.slice(lastPos, idx))
let depth = 0 lastPos = idx + separator.length
let lastIndex = 0
let found = false
let separatorIndex = 0
let separatorStart = 0
let separatorLength = separator.length
// Find all paren-like things & character
// And only split on commas if they're top-level
for (let match of input.matchAll(SPECIALS)) {
let matchesSeparator = match[0] === separator[separatorIndex]
let atEndOfSeparator = separatorIndex === separatorLength - 1
let matchesFullSeparator = matchesSeparator && atEndOfSeparator
if (match[0] === '(') depth++
if (match[0] === ')') depth--
if (match[0] === '[') depth++
if (match[0] === ']') depth--
if (match[0] === '{') depth++
if (match[0] === '}') depth--
if (matchesSeparator && depth === 0) {
if (separatorStart === 0) {
separatorStart = match.index
}
separatorIndex++
}
if (matchesFullSeparator && depth === 0) {
found = true
yield input.substring(lastIndex, separatorStart)
lastIndex = separatorStart + separatorLength
}
if (separatorIndex === separatorLength) {
separatorIndex = 0
separatorStart = 0
} }
} }
// Provide the last segment of the string if available if (char === '(' || char === '[' || char === '{') {
// Otherwise the whole string since no `char`s were found stack.push(char)
// This mirrors the behavior of string.split() } else if (
if (found) { (char === ')' && stack[stack.length - 1] === '(') ||
yield input.substring(lastIndex) (char === ']' && stack[stack.length - 1] === '[') ||
} else { (char === '}' && stack[stack.length - 1] === '{')
yield input ) {
stack.pop()
} }
} }
parts.push(input.slice(lastPos))
return parts
}