Skip to content

Commit 27a2bfe

Browse files
author
Benjamin Pasero
committed
more tests (for microsoft#35572)
1 parent 6779069 commit 27a2bfe

3 files changed

Lines changed: 51 additions & 15 deletions

File tree

src/vs/base/common/filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function printTable(table: number[][], pattern: string, patternLen: number, word
421421
return ret;
422422
}
423423

424-
export function isSeparatorAtPos(value: string, index: number): boolean {
424+
function isSeparatorAtPos(value: string, index: number): boolean {
425425
if (index < 0 || index >= value.length) {
426426
return false;
427427
}

src/vs/base/parts/quickopen/common/quickOpenScorer.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
'use strict';
77

88
import { compareAnything } from 'vs/base/common/comparers';
9-
import { matchesPrefix, IMatch, createMatches, matchesCamelCase, isSeparatorAtPos, isUpper } from 'vs/base/common/filters';
9+
import { matchesPrefix, IMatch, createMatches, matchesCamelCase, isUpper } from 'vs/base/common/filters';
1010
import { isEqual, nativeSep } from 'vs/base/common/paths';
1111
import { isWindows } from 'vs/base/common/platform';
1212
import { stripWildcards } from 'vs/base/common/strings';
13+
import { CharCode } from 'vs/base/common/charCode';
1314

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

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

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

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

205-
// if (DEBUG) {
206-
// console.log('Inside word upper case bonus: +1');
207-
// }
205+
// Inside word upper case bonus (camel case)
206+
else if (isUpper(target.charCodeAt(targetIndex))) {
207+
score += 1;
208+
209+
// if (DEBUG) {
210+
// console.log('Inside word upper case bonus: +1');
211+
// }
212+
}
208213
}
209214

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

222+
function scoreSeparatorAtPos(charCode: number): number {
223+
switch (charCode) {
224+
case CharCode.Slash:
225+
case CharCode.Backslash:
226+
return 5; // prefer path separators...
227+
case CharCode.Underline:
228+
case CharCode.Dash:
229+
case CharCode.Period:
230+
case CharCode.Space:
231+
case CharCode.SingleQuote:
232+
case CharCode.DoubleQuote:
233+
case CharCode.Colon:
234+
return 4; // ...over other separators
235+
default:
236+
return 0;
237+
}
238+
}
239+
217240
// function printMatrix(query: string, target: string, matches: number[], scores: number[]): void {
218241
// console.log('\t' + target.split('').join('\t'));
219242
// for (let queryIndex = 0; queryIndex < query.length; queryIndex++) {

src/vs/base/parts/quickopen/test/common/quickOpenScorer.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,19 @@ suite('Quick Open Scorer', () => {
715715
assert.equal(res[0], resourceB);
716716
});
717717

718+
test('compareFilesByScore - avoid match scattering (bug #35572)', function () {
719+
const resourceA = URI.file('static/app/source/angluar/-admin/-organization/-settings/layout/layout.js');
720+
const resourceB = URI.file('static/app/source/angular/-admin/-project/-settings/_settings/settings.js');
721+
722+
let query = 'partisettings';
723+
724+
let res = [resourceA, resourceB].sort((r1, r2) => compareItemsByScore(r1, r2, query, true, ResourceAccessor, cache));
725+
assert.equal(res[0], resourceB);
726+
727+
res = [resourceB, resourceA].sort((r1, r2) => compareItemsByScore(r1, r2, query, true, ResourceAccessor, cache));
728+
assert.equal(res[0], resourceB);
729+
});
730+
718731
test('compareFilesByScore - prefer shorter hit (bug #20546)', function () {
719732
const resourceA = URI.file('editor/core/components/tests/list-view-spec.js');
720733
const resourceB = URI.file('editor/core/components/list-view.js');

0 commit comments

Comments
 (0)