Skip to content

Commit

Permalink
fix #31591
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Oct 4, 2017
1 parent 12d4925 commit 7fb86f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/vs/base/parts/quickopen/common/quickOpenScorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function doScoreItem<T>(label: string, description: string, path: string, query:
return NO_ITEM_SCORE;
}

export function compareItemsByScore<T>(itemA: T, itemB: T, query: string, fuzzy: boolean, accessor: IItemAccessor<T>, cache: ScorerCache): number {
export function compareItemsByScore<T>(itemA: T, itemB: T, query: string, fuzzy: boolean, accessor: IItemAccessor<T>, cache: ScorerCache, fallbackComparer = fallbackCompare): number {
const scoreA = scoreItem(itemA, query, fuzzy, accessor, cache).score;
const scoreB = scoreItem(itemB, query, fuzzy, accessor, cache).score;

Expand Down Expand Up @@ -395,7 +395,11 @@ export function compareItemsByScore<T>(itemA: T, itemB: T, query: string, fuzzy:
return scoreA > scoreB ? -1 : 1;
}

// 6.) at this point, scores are identical for both items so we start to sort by length
// 6.) at this point, scores are identical for both items so we start to use the fallback compare
return fallbackComparer(itemA, itemB, query, accessor);
}

export function fallbackCompare<T>(itemA: T, itemB: T, query: string, accessor: IItemAccessor<T>): number {

// check for label + description length and prefer shorter
const labelA = accessor.getItemLabel(itemA);
Expand Down
15 changes: 15 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 @@ -407,4 +407,19 @@ suite('Quick Open Scorer', () => {
assert.equal(res[1], resourceA);
assert.equal(res[2], resourceC);
});

test('compareFilesByScore - allow to provide fallback sorter (bug #31591)', function () {
const resourceA = URI.file('virtual/vscode.d.ts');
const resourceB = URI.file('vscode/src/vs/vscode.d.ts');

let query = 'vscode';

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

res = [resourceB, resourceA].sort((r1, r2) => scorer.compareItemsByScore(r1, r2, query, true, ResourceAccessor, cache, (r1, r2, query, ResourceAccessor) => -1));
assert.equal(res[0], resourceB);
assert.equal(res[1], resourceA);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,9 @@ class EditorHistoryHandler {
return true;
})

// Sort by score
.sort((e1, e2) => compareItemsByScore(e1, e2, searchValue, false, accessor, this.scorerCache));
// Sort by score and provide a fallback sorter that keeps the
// recency of items in case the score for items is the same
.sort((e1, e2) => compareItemsByScore(e1, e2, searchValue, false, accessor, this.scorerCache, (e1, e2, searchValue, accessor) => -1));
}
}

Expand Down

0 comments on commit 7fb86f8

Please sign in to comment.