Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/vs/base/common/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,12 +745,13 @@ function _doScore(
return Number.MIN_SAFE_INTEGER;
}

let score = 1;
let isGapLocation = false;
let score: number;
let isGapLocation: boolean;
if (wordPos === (patternPos - patternStart)) {
// common prefix: `foobar <-> foobaz`
// ^^^^^
score = pattern[patternPos] === word[wordPos] ? 7 : 5;
isGapLocation = false;

} else if (isUpperCaseAtPos(wordPos, word, wordLow) && (wordPos === 0 || !isUpperCaseAtPos(wordPos - 1, word, wordLow))) {
// hitting upper-case: `foo <-> forOthers`
Expand All @@ -762,22 +763,22 @@ function _doScore(
// hitting a separator: `. <-> foo.bar`
// ^
score = 5;
isGapLocation = false;

} else if (isSeparatorAtPos(wordLow, wordPos - 1) || isWhitespaceAtPos(wordLow, wordPos - 1)) {
// post separator: `foo <-> bar_foo`
// ^^^
score = 5;
isGapLocation = true;
} else {
score = 1;
isGapLocation = isUpperCaseAtPos(wordPos, word, wordLow);
}

if (score > 1 && patternPos === patternStart) {
outFirstMatchStrong[0] = true;
}

if (!isGapLocation) {
isGapLocation = isUpperCaseAtPos(wordPos, word, wordLow) || isSeparatorAtPos(wordLow, wordPos - 1) || isWhitespaceAtPos(wordLow, wordPos - 1);
}

//
if (patternPos === patternStart) { // first character in pattern
if (wordPos > wordStart) {
Expand Down
48 changes: 37 additions & 11 deletions src/vs/base/test/common/filters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { anyScore, createMatches, fuzzyScore, fuzzyScoreGraceful, fuzzyScoreGracefulAggressive, FuzzyScorer, IFilter, IMatch, matchesCamelCase, matchesContiguousSubString, matchesPrefix, matchesStrictPrefix, matchesSubString, matchesWords, or } from 'vs/base/common/filters';
import { anyScore, createMatches, fuzzyScore, fuzzyScoreGraceful, fuzzyScoreGracefulAggressive, FuzzyScoreOptions, FuzzyScorer, IFilter, IMatch, matchesCamelCase, matchesContiguousSubString, matchesPrefix, matchesStrictPrefix, matchesSubString, matchesWords, or } from 'vs/base/common/filters';

function filterOk(filter: IFilter, word: string, wordToMatchAgainst: string, highlights?: { start: number; end: number }[]) {
const r = filter(word, wordToMatchAgainst);
Expand Down Expand Up @@ -567,22 +567,48 @@ suite('Filters', () => {
assertMatches('bar', 'foobar', 'foo^b^a^r', anyScore);
});

function assertScores(
filter: typeof fuzzyScore,
assertion: (a: number, b: number) => void,
pattern: string,
a: string,
b: string,
options?: FuzzyScoreOptions,
): void {
const patternLow = pattern.toLowerCase();
const aScore = filter(pattern, patternLow, 0, a, a.toLowerCase(), 0, options);
const bScore = filter(pattern, patternLow, 0, b, b.toLowerCase(), 0, options);
assert.ok(aScore);
assert.ok(bScore);
assertion(aScore[0], bScore[0]);
}

function assertLessThan(a: number, b: number, message?: string): void {
assert.ok(a < b, message);
}

test('configurable full match boost', function () {
const prefix = 'create';
const a = 'createModelServices';
const b = 'create';

const aBoost = fuzzyScore(prefix, prefix, 0, a, a.toLowerCase(), 0, { boostFullMatch: true, firstMatchCanBeWeak: true });
const bBoost = fuzzyScore(prefix, prefix, 0, b, b.toLowerCase(), 0, { boostFullMatch: true, firstMatchCanBeWeak: true });
assert.ok(aBoost);
assert.ok(bBoost);
assert.ok(aBoost[0] < bBoost[0]);
assertScores(fuzzyScore, assertLessThan, prefix, a, b, { boostFullMatch: true, firstMatchCanBeWeak: false });
assertScores(fuzzyScore, assert.strictEqual, prefix, a, b, { boostFullMatch: false, firstMatchCanBeWeak: false });
});

const aScore = fuzzyScore(prefix, prefix, 0, a, a.toLowerCase(), 0, { boostFullMatch: false, firstMatchCanBeWeak: true });
const bScore = fuzzyScore(prefix, prefix, 0, b, b.toLowerCase(), 0, { boostFullMatch: false, firstMatchCanBeWeak: true });
assert.ok(aScore);
assert.ok(bScore);
assert.ok(aScore[0] === bScore[0]);
test('pattern ending in uppercase character should not get extra boost', function () {
assertScores(fuzzyScore, assertLessThan, 'F', 'Foo', 'F', { boostFullMatch: true, firstMatchCanBeWeak: false });
assertScores(fuzzyScore, assert.strictEqual, 'F', 'Foo', 'F', { boostFullMatch: false, firstMatchCanBeWeak: false });

assertScores(fuzzyScore, assertLessThan, 'aB', 'aBc', 'aB', { boostFullMatch: true, firstMatchCanBeWeak: false });
assertScores(fuzzyScore, assert.strictEqual, 'aB', 'aBc', 'aB', { boostFullMatch: false, firstMatchCanBeWeak: false });

// matching `F` against `F` and `f` against `f` should have the same scores
const upperScore = fuzzyScore('F', 'f', 0, 'F', 'f', 0);
const lowerScore = fuzzyScore('f', 'f', 0, 'f', 'f', 0);
assert.ok(upperScore);
assert.ok(lowerScore);
assert.equal(upperScore[0], lowerScore[0]);
});

test('Unexpected suggest highlighting ignores whole word match in favor of matching first letter#147423', function () {
Expand Down