Skip to content

Commit

Permalink
more tests (for #35572)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Oct 23, 2017
1 parent 6779069 commit 27a2bfe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/common/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function printTable(table: number[][], pattern: string, patternLen: number, word
return ret;
}

export function isSeparatorAtPos(value: string, index: number): boolean {
function isSeparatorAtPos(value: string, index: number): boolean {
if (index < 0 || index >= value.length) {
return false;
}
Expand Down
51 changes: 37 additions & 14 deletions src/vs/base/parts/quickopen/common/quickOpenScorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
'use strict';

import { compareAnything } from 'vs/base/common/comparers';
import { matchesPrefix, IMatch, createMatches, matchesCamelCase, isSeparatorAtPos, isUpper } from 'vs/base/common/filters';
import { matchesPrefix, IMatch, createMatches, matchesCamelCase, isUpper } from 'vs/base/common/filters';
import { isEqual, nativeSep } from 'vs/base/common/paths';
import { isWindows } from 'vs/base/common/platform';
import { stripWildcards } from 'vs/base/common/strings';
import { CharCode } from 'vs/base/common/charCode';

export type Score = [number /* score */, number[] /* match positions */];
export type ScorerCache = { [key: string]: IItemScore };
Expand Down Expand Up @@ -189,22 +190,26 @@ function computeCharScore(query: string, queryLower: string, queryIndex: number,
// }
}

// After separator bonus
else if (isSeparatorAtPos(target, targetIndex - 1)) {
score += 4;
else {

// if (DEBUG) {
// console.log('After separtor bonus: +4');
// }
}
// After separator bonus
const separatorBonus = scoreSeparatorAtPos(target.charCodeAt(targetIndex - 1));
if (separatorBonus) {
score += separatorBonus;

// Inside word upper case bonus
else if (isUpper(target.charCodeAt(targetIndex))) {
score += 1;
// if (DEBUG) {
// console.log('After separtor bonus: +4');
// }
}

// if (DEBUG) {
// console.log('Inside word upper case bonus: +1');
// }
// Inside word upper case bonus (camel case)
else if (isUpper(target.charCodeAt(targetIndex))) {
score += 1;

// if (DEBUG) {
// console.log('Inside word upper case bonus: +1');
// }
}
}

// if (DEBUG) {
Expand All @@ -214,6 +219,24 @@ function computeCharScore(query: string, queryLower: string, queryIndex: number,
return score;
}

function scoreSeparatorAtPos(charCode: number): number {
switch (charCode) {
case CharCode.Slash:
case CharCode.Backslash:
return 5; // prefer path separators...
case CharCode.Underline:
case CharCode.Dash:
case CharCode.Period:
case CharCode.Space:
case CharCode.SingleQuote:
case CharCode.DoubleQuote:
case CharCode.Colon:
return 4; // ...over other separators
default:
return 0;
}
}

// function printMatrix(query: string, target: string, matches: number[], scores: number[]): void {
// console.log('\t' + target.split('').join('\t'));
// for (let queryIndex = 0; queryIndex < query.length; queryIndex++) {
Expand Down
13 changes: 13 additions & 0 deletions src/vs/base/parts/quickopen/test/common/quickOpenScorer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,19 @@ suite('Quick Open Scorer', () => {
assert.equal(res[0], resourceB);
});

test('compareFilesByScore - avoid match scattering (bug #35572)', function () {
const resourceA = URI.file('static/app/source/angluar/-admin/-organization/-settings/layout/layout.js');
const resourceB = URI.file('static/app/source/angular/-admin/-project/-settings/_settings/settings.js');

let query = 'partisettings';

let res = [resourceA, resourceB].sort((r1, r2) => compareItemsByScore(r1, r2, query, true, ResourceAccessor, cache));
assert.equal(res[0], resourceB);

res = [resourceB, resourceA].sort((r1, r2) => compareItemsByScore(r1, r2, query, true, ResourceAccessor, cache));
assert.equal(res[0], resourceB);
});

test('compareFilesByScore - prefer shorter hit (bug #20546)', function () {
const resourceA = URI.file('editor/core/components/tests/list-view-spec.js');
const resourceB = URI.file('editor/core/components/list-view.js');
Expand Down

0 comments on commit 27a2bfe

Please sign in to comment.