Skip to content

Commit 7c7f74b

Browse files
author
Benjamin Pasero
committed
quick access - better normalize matches
1 parent b644d80 commit 7c7f74b

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

src/vs/base/common/fuzzyScorer.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,45 @@ function createMatches(offsets: number[] | undefined): IMatch[] {
526526
}
527527

528528
function normalizeMatches(matches: IMatch[]): IMatch[] {
529-
const positions = new Set<number>();
530529

531-
for (const match of matches) {
532-
for (let i = match.start; i < match.end; i++) {
533-
positions.add(i);
530+
// sort matches by start to be able to normalize
531+
const sortedMatches = matches.sort((matchA, matchB) => {
532+
return matchA.start - matchB.start;
533+
});
534+
535+
// merge matches that overlap
536+
const normalizedMatches: IMatch[] = [];
537+
let currentMatch: IMatch | undefined = undefined;
538+
for (const match of sortedMatches) {
539+
540+
// if we have no current match or the matches
541+
// do not overlap, we take it as is and remember
542+
// it for future merging
543+
if (!currentMatch || !matchOverlaps(currentMatch, match)) {
544+
currentMatch = match;
545+
normalizedMatches.push(match);
534546
}
547+
548+
// otherwise we merge the matches
549+
else {
550+
currentMatch.start = Math.min(currentMatch.start, match.start);
551+
currentMatch.end = Math.max(currentMatch.end, match.end);
552+
}
553+
}
554+
555+
return normalizedMatches;
556+
}
557+
558+
function matchOverlaps(matchA: IMatch, matchB: IMatch): boolean {
559+
if (matchA.end < matchB.start) {
560+
return false; // A ends before B starts
561+
}
562+
563+
if (matchB.end < matchA.start) {
564+
return false; // B ends before A starts
535565
}
536566

537-
return createMatches(Array.from(positions.values()).sort((a, b) => a - b));
567+
return true;
538568
}
539569

540570
//#endregion

0 commit comments

Comments
 (0)