fix whitespace

This commit is contained in:
Philipp Kühn
2018-11-08 16:40:45 +01:00
parent a7d3af30bb
commit c5d7ef4e79

View File

@@ -22,39 +22,42 @@ function triggerCharacter({
const textTo = $position.end() const textTo = $position.end()
const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0') const text = $position.doc.textBetween(textFrom, textTo, '\0', '\0')
let match = regexp.exec(text) let match = regexp.exec(text)
let position let position
while (match !== null) { while (match !== null) {
// JavaScript doesn't have lookbehinds; this hacks a check that first character is " " // JavaScript doesn't have lookbehinds; this hacks a check that first character is " "
// or the line beginning // or the line beginning
const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index) const matchPrefix = match.input.slice(Math.max(0, match.index - 1), match.index)
if (/^[\s\0]?$/.test(matchPrefix)) { if (/^[\s\0]?$/.test(matchPrefix)) {
// The absolute position of the match in the document // The absolute position of the match in the document
const from = match.index + $position.start() const from = match.index + $position.start()
let to = from + match[0].length let to = from + match[0].length
// Edge case handling; if spaces are allowed and we're directly in between // Edge case handling; if spaces are allowed and we're directly in between
// two triggers // two triggers
if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) { if (allowSpaces && suffix.test(text.slice(to - 1, to + 1))) {
match[0] += ' ' match[0] += ' '
to += 1 to += 1
} }
// If the $position is located within the matched substring, return that range // If the $position is located within the matched substring, return that range
if (from < $position.pos && to >= $position.pos) { if (from < $position.pos && to >= $position.pos) {
position = { position = {
range: { range: {
from, from,
to, to,
}, },
query: match[0].slice(char.length), query: match[0].slice(char.length),
text: match[0], text: match[0],
} }
} }
} }
match = regexp.exec(text)
} match = regexp.exec(text)
return position }
return position
} }
} }