Skip to content

Commit eb1d972

Browse files
committed
add option to allow for weak match as first match, use that option for outline (experiment) microsoft#50724
1 parent 424ac2e commit eb1d972

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/vs/base/common/filters.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ const enum Arrow { Top = 0b1, Diag = 0b10, Left = 0b100 }
454454

455455
export type FuzzyScore = [number, number[]];
456456

457-
export function fuzzyScore(pattern: string, word: string, patternMaxWhitespaceIgnore?: number): FuzzyScore {
457+
export function fuzzyScore(pattern: string, word: string, patternMaxWhitespaceIgnore?: number, firstMatchCanBeWeak?: boolean): FuzzyScore {
458458

459459
const patternLen = pattern.length > 100 ? 100 : pattern.length;
460460
const wordLen = word.length > 100 ? 100 : word.length;
@@ -578,6 +578,7 @@ export function fuzzyScore(pattern: string, word: string, patternMaxWhitespaceIg
578578
_matchesCount = 0;
579579
_topScore = -100;
580580
_patternStartPos = patternStartPos;
581+
_firstMatchCanBeWeak = firstMatchCanBeWeak;
581582
_findAllMatches(patternLen, wordLen, patternLen === wordLen ? 1 : 0, new LazyArray(), false);
582583

583584
if (_matchesCount === 0) {
@@ -591,6 +592,7 @@ let _matchesCount: number = 0;
591592
let _topMatch: LazyArray;
592593
let _topScore: number = 0;
593594
let _patternStartPos: number = 0;
595+
let _firstMatchCanBeWeak: boolean = false;
594596

595597
function _findAllMatches(patternPos: number, wordPos: number, total: number, matches: LazyArray, lastMatched: boolean): void {
596598

@@ -644,7 +646,7 @@ function _findAllMatches(patternPos: number, wordPos: number, total: number, mat
644646
if (score === 1) {
645647
simpleMatchCount += 1;
646648

647-
if (patternPos === _patternStartPos) {
649+
if (patternPos === _patternStartPos && !_firstMatchCanBeWeak) {
648650
// when the first match is a weak
649651
// match we discard it
650652
return undefined;

src/vs/base/test/common/filters.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ suite('Filters', () => {
309309
assertMatches('fo', 'bar\\foo', 'bar\\^f^oo', fuzzyScore);
310310
});
311311

312+
test('fuzzyScore (first match can be weak)', function () {
313+
let fuzzyScoreWeak = (pattern, word) => fuzzyScore(pattern, word, undefined, true);
314+
assertMatches('Three', 'HTMLHRElement', 'H^TML^H^R^El^ement', fuzzyScoreWeak);
315+
assertMatches('tor', 'constructor', 'construc^t^o^r', fuzzyScoreWeak);
316+
assertMatches('ur', 'constructor', 'constr^ucto^r', fuzzyScoreWeak);
317+
assertTopScore(fuzzyScoreWeak, 'tor', 2, 'constructor', 'Thor', 'cTor');
318+
});
319+
312320
test('fuzzyScore, many matches', function () {
313321

314322
assertMatches(

src/vs/workbench/parts/outline/electron-browser/outlineModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class OutlineGroup extends TreeElement {
105105
}
106106

107107
private _updateMatches(pattern: string, item: OutlineElement, topMatch: OutlineElement): OutlineElement {
108-
item.score = fuzzyScore(pattern, item.symbol.name);
108+
item.score = fuzzyScore(pattern, item.symbol.name, undefined, true);
109109
if (item.score && (!topMatch || item.score[0] > topMatch.score[0])) {
110110
topMatch = item;
111111
}

0 commit comments

Comments
 (0)