mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge from vscode 6fded8a497cd0142de3a1c607649a5423a091a25
This commit is contained in:
@@ -42,20 +42,28 @@ class NullAccessorClass implements scorer.IItemAccessor<URI> {
|
||||
}
|
||||
}
|
||||
|
||||
function _doScore(target: string, query: string, fuzzy: boolean): scorer.Score {
|
||||
return scorer.score(target, scorer.prepareQuery(query), fuzzy);
|
||||
function _doScore(target: string, query: string, fuzzy: boolean): scorer.FuzzyScore {
|
||||
const preparedQuery = scorer.prepareQuery(query);
|
||||
|
||||
return scorer.scoreFuzzy(target, preparedQuery.normalized, preparedQuery.normalizedLowercase, fuzzy);
|
||||
}
|
||||
|
||||
function scoreItem<T>(item: T, query: string, fuzzy: boolean, accessor: scorer.IItemAccessor<T>, cache: scorer.ScorerCache): scorer.IItemScore {
|
||||
return scorer.scoreItem(item, scorer.prepareQuery(query), fuzzy, accessor, cache);
|
||||
function _doScore2(target: string, query: string): scorer.FuzzyScore2 {
|
||||
const preparedQuery = scorer.prepareQuery(query);
|
||||
|
||||
return scorer.scoreFuzzy2(target, preparedQuery);
|
||||
}
|
||||
|
||||
function compareItemsByScore<T>(itemA: T, itemB: T, query: string, fuzzy: boolean, accessor: scorer.IItemAccessor<T>, cache: scorer.ScorerCache): number {
|
||||
return scorer.compareItemsByScore(itemA, itemB, scorer.prepareQuery(query), fuzzy, accessor, cache);
|
||||
function scoreItem<T>(item: T, query: string, fuzzy: boolean, accessor: scorer.IItemAccessor<T>, cache: scorer.FuzzyScorerCache): scorer.IItemScore {
|
||||
return scorer.scoreItemFuzzy(item, scorer.prepareQuery(query), fuzzy, accessor, cache);
|
||||
}
|
||||
|
||||
function compareItemsByScore<T>(itemA: T, itemB: T, query: string, fuzzy: boolean, accessor: scorer.IItemAccessor<T>, cache: scorer.FuzzyScorerCache): number {
|
||||
return scorer.compareItemsByFuzzyScore(itemA, itemB, scorer.prepareQuery(query), fuzzy, accessor, cache);
|
||||
}
|
||||
|
||||
const NullAccessor = new NullAccessorClass();
|
||||
let cache: scorer.ScorerCache = Object.create(null);
|
||||
let cache: scorer.FuzzyScorerCache = Object.create(null);
|
||||
|
||||
suite('Fuzzy Scorer', () => {
|
||||
|
||||
@@ -66,7 +74,7 @@ suite('Fuzzy Scorer', () => {
|
||||
test('score (fuzzy)', function () {
|
||||
const target = 'HeLlo-World';
|
||||
|
||||
const scores: scorer.Score[] = [];
|
||||
const scores: scorer.FuzzyScore[] = [];
|
||||
scores.push(_doScore(target, 'HelLo-World', true)); // direct case match
|
||||
scores.push(_doScore(target, 'hello-world', true)); // direct mix-case match
|
||||
scores.push(_doScore(target, 'HW', true)); // direct case prefix (multiple)
|
||||
@@ -109,42 +117,6 @@ suite('Fuzzy Scorer', () => {
|
||||
assert.equal(_doScore(target, 'eo', false)[0], 0);
|
||||
});
|
||||
|
||||
test('score (fuzzy, multiple)', function () {
|
||||
const target = 'HeLlo-World';
|
||||
|
||||
const [firstSingleScore, firstSinglePositions] = _doScore(target, 'HelLo', true);
|
||||
const [secondSingleScore, secondSinglePositions] = _doScore(target, 'World', true);
|
||||
const firstAndSecondSinglePositions = [...firstSinglePositions, ...secondSinglePositions];
|
||||
|
||||
let [multiScore, multiPositions] = _doScore(target, 'HelLo World', true);
|
||||
|
||||
function assertScore() {
|
||||
assert.ok(multiScore >= firstSingleScore + secondSingleScore);
|
||||
for (let i = 0; i < multiPositions.length; i++) {
|
||||
assert.equal(multiPositions[i], firstAndSecondSinglePositions[i]);
|
||||
}
|
||||
}
|
||||
|
||||
function assertNoScore() {
|
||||
assert.equal(multiScore, 0);
|
||||
assert.equal(multiPositions.length, 0);
|
||||
}
|
||||
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiPositions] = _doScore(target, 'World HelLo', true);
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiPositions] = _doScore(target, 'World HelLo World', true);
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiPositions] = _doScore(target, 'World HelLo Nothing', true);
|
||||
assertNoScore();
|
||||
|
||||
[multiScore, multiPositions] = _doScore(target, 'More Nothing', true);
|
||||
assertNoScore();
|
||||
});
|
||||
|
||||
test('scoreItem - matches are proper', function () {
|
||||
let res = scoreItem(null, 'something', true, ResourceAccessor, cache);
|
||||
assert.ok(!res.score);
|
||||
@@ -217,6 +189,49 @@ suite('Fuzzy Scorer', () => {
|
||||
assert.ok(pathRes.score > noRes.score);
|
||||
});
|
||||
|
||||
test('scoreItem - multiple', function () {
|
||||
const resource = URI.file('/xyz/some/path/someFile123.txt');
|
||||
|
||||
let res1 = scoreItem(resource, 'xyz some', true, ResourceAccessor, cache);
|
||||
assert.ok(res1.score);
|
||||
assert.equal(res1.labelMatch?.length, 1);
|
||||
assert.equal(res1.labelMatch![0].start, 0);
|
||||
assert.equal(res1.labelMatch![0].end, 4);
|
||||
assert.equal(res1.descriptionMatch?.length, 1);
|
||||
assert.equal(res1.descriptionMatch![0].start, 1);
|
||||
assert.equal(res1.descriptionMatch![0].end, 4);
|
||||
|
||||
let res2 = scoreItem(resource, 'some xyz', true, ResourceAccessor, cache);
|
||||
assert.ok(res2.score);
|
||||
assert.equal(res1.score, res2.score);
|
||||
assert.equal(res2.labelMatch?.length, 1);
|
||||
assert.equal(res2.labelMatch![0].start, 0);
|
||||
assert.equal(res2.labelMatch![0].end, 4);
|
||||
assert.equal(res2.descriptionMatch?.length, 1);
|
||||
assert.equal(res2.descriptionMatch![0].start, 1);
|
||||
assert.equal(res2.descriptionMatch![0].end, 4);
|
||||
|
||||
let res3 = scoreItem(resource, 'some xyz file file123', true, ResourceAccessor, cache);
|
||||
assert.ok(res3.score);
|
||||
assert.ok(res3.score > res2.score);
|
||||
assert.equal(res3.labelMatch?.length, 1);
|
||||
assert.equal(res3.labelMatch![0].start, 0);
|
||||
assert.equal(res3.labelMatch![0].end, 11);
|
||||
assert.equal(res3.descriptionMatch?.length, 1);
|
||||
assert.equal(res3.descriptionMatch![0].start, 1);
|
||||
assert.equal(res3.descriptionMatch![0].end, 4);
|
||||
|
||||
let res4 = scoreItem(resource, 'path z y', true, ResourceAccessor, cache);
|
||||
assert.ok(res4.score);
|
||||
assert.ok(res4.score < res2.score);
|
||||
assert.equal(res4.labelMatch?.length, 0);
|
||||
assert.equal(res4.descriptionMatch?.length, 2);
|
||||
assert.equal(res4.descriptionMatch![0].start, 2);
|
||||
assert.equal(res4.descriptionMatch![0].end, 4);
|
||||
assert.equal(res4.descriptionMatch![1].start, 10);
|
||||
assert.equal(res4.descriptionMatch![1].end, 14);
|
||||
});
|
||||
|
||||
test('scoreItem - invalid input', function () {
|
||||
|
||||
let res = scoreItem(null, null!, true, ResourceAccessor, cache);
|
||||
@@ -878,6 +893,11 @@ suite('Fuzzy Scorer', () => {
|
||||
assert.equal(query.values?.[1].normalized, 'World');
|
||||
assert.equal(query.values?.[1].normalizedLowercase, 'World'.toLowerCase());
|
||||
|
||||
let restoredQuery = scorer.pieceToQuery(query.values!);
|
||||
assert.equal(restoredQuery.original, query.original);
|
||||
assert.equal(restoredQuery.values?.length, query.values?.length);
|
||||
assert.equal(restoredQuery.containsPathSeparator, query.containsPathSeparator);
|
||||
|
||||
// with spaces that are empty
|
||||
query = scorer.prepareQuery(' Hello World ');
|
||||
assert.equal(query.original, ' Hello World ');
|
||||
@@ -911,4 +931,48 @@ suite('Fuzzy Scorer', () => {
|
||||
assert.equal(scorer.prepareQuery('\\some\\path').containsPathSeparator, true);
|
||||
}
|
||||
});
|
||||
|
||||
test('fuzzyScore2 (multiple queries)', function () {
|
||||
const target = 'HeLlo-World';
|
||||
|
||||
const [firstSingleScore, firstSingleMatches] = _doScore2(target, 'HelLo');
|
||||
const [secondSingleScore, secondSingleMatches] = _doScore2(target, 'World');
|
||||
const firstAndSecondSingleMatches = [...firstSingleMatches || [], ...secondSingleMatches || []];
|
||||
|
||||
let [multiScore, multiMatches] = _doScore2(target, 'HelLo World');
|
||||
|
||||
function assertScore() {
|
||||
assert.ok(multiScore ?? 0 >= ((firstSingleScore ?? 0) + (secondSingleScore ?? 0)));
|
||||
for (let i = 0; multiMatches && i < multiMatches.length; i++) {
|
||||
const multiMatch = multiMatches[i];
|
||||
const firstAndSecondSingleMatch = firstAndSecondSingleMatches[i];
|
||||
|
||||
if (multiMatch && firstAndSecondSingleMatch) {
|
||||
assert.equal(multiMatch.start, firstAndSecondSingleMatch.start);
|
||||
assert.equal(multiMatch.end, firstAndSecondSingleMatch.end);
|
||||
} else {
|
||||
assert.fail();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function assertNoScore() {
|
||||
assert.equal(multiScore, 0);
|
||||
assert.equal(multiMatches.length, 0);
|
||||
}
|
||||
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiMatches] = _doScore2(target, 'World HelLo');
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiMatches] = _doScore2(target, 'World HelLo World');
|
||||
assertScore();
|
||||
|
||||
[multiScore, multiMatches] = _doScore2(target, 'World HelLo Nothing');
|
||||
assertNoScore();
|
||||
|
||||
[multiScore, multiMatches] = _doScore2(target, 'More Nothing');
|
||||
assertNoScore();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user