Fix spinning unmatched delimiter (#343)

This commit is contained in:
Pokey Rule 2021-11-29 16:53:16 +00:00 committed by GitHub
parent 1de4725842
commit b07bee49a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -255,6 +255,12 @@ function getDelimiterPairOffsets(
(match, index) => {
const startOffset = match.index!;
const matchText = match[0];
// NB: It is important to cache here because otherwise the algorithm that
// disambiguates delimiters of unknown side goes badly super linear
let hasCachedDelimiterInfo = false;
let cachedDelimiterInfo: IndividualDelimiter | undefined = undefined;
return {
offsets: {
start: startOffset,
@ -262,20 +268,29 @@ function getDelimiterPairOffsets(
},
get delimiterInfo() {
const delimiterInfo = delimiterTextToDelimiterInfoMap[matchText];
if (hasCachedDelimiterInfo) {
return cachedDelimiterInfo;
}
const rawDelimiterInfo = delimiterTextToDelimiterInfoMap[matchText];
let side =
delimiterInfo.side === "unknown" && forceDirection == null
rawDelimiterInfo.side === "unknown" && forceDirection == null
? inferDelimiterSide(
text,
delimiterOccurrences,
index,
delimiterInfo?.delimiter,
rawDelimiterInfo?.delimiter,
startOffset
)
: delimiterInfo.side;
: rawDelimiterInfo.side;
return { ...delimiterInfo, side };
const delimiterInfo = { ...rawDelimiterInfo, side };
hasCachedDelimiterInfo = true;
cachedDelimiterInfo = delimiterInfo;
return delimiterInfo;
},
};
}