From b6476d6c09b273f02513a8d8ba8a58bd6f4eaa9a Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Thu, 23 May 2019 20:36:31 +0200 Subject: [PATCH] bring back anyScore but make it more strict (== easer to understand), #74203 --- src/vs/base/common/filters.ts | 25 +++++++++++++++++++ .../editor/contrib/suggest/completionModel.ts | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/vs/base/common/filters.ts b/src/vs/base/common/filters.ts index d2290780850..abafd683093 100644 --- a/src/vs/base/common/filters.ts +++ b/src/vs/base/common/filters.ts @@ -362,6 +362,31 @@ export function matchesFuzzy2(pattern: string, word: string): IMatch[] | null { return score ? createMatches(score) : null; } +export function anyScore(pattern: string, lowPattern: string, _patternPos: number, word: string, lowWord: string, _wordPos: number): FuzzyScore { + const result = fuzzyScore(pattern, lowPattern, 0, word, lowWord, 0, true); + if (result) { + return result; + } + let matches = 0; + let score = 0; + let idx = _wordPos; + for (let patternPos = 0; patternPos < lowPattern.length && patternPos < _maxLen; ++patternPos) { + const wordPos = lowWord.indexOf(lowPattern.charAt(patternPos), idx); + if (wordPos >= 0) { + score += 1; + matches += 2 ** wordPos; + idx = wordPos + 1; + + } else if (matches !== 0) { + // once we have started matching things + // we need to match the remaining pattern + // characters + break; + } + } + return [score, matches, _wordPos]; +} + //#region --- fuzzyScore --- export function createMatches(score: undefined | FuzzyScore): IMatch[] { diff --git a/src/vs/editor/contrib/suggest/completionModel.ts b/src/vs/editor/contrib/suggest/completionModel.ts index 9fbb6a38178..63836d21286 100644 --- a/src/vs/editor/contrib/suggest/completionModel.ts +++ b/src/vs/editor/contrib/suggest/completionModel.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { fuzzyScore, fuzzyScoreGracefulAggressive, FuzzyScorer, FuzzyScore } from 'vs/base/common/filters'; +import { fuzzyScore, fuzzyScoreGracefulAggressive, FuzzyScorer, FuzzyScore, anyScore } from 'vs/base/common/filters'; import { isDisposable } from 'vs/base/common/lifecycle'; import { CompletionList, CompletionItemProvider, CompletionItemKind } from 'vs/editor/common/modes'; import { CompletionItem } from './suggest'; @@ -222,7 +222,7 @@ export class CompletionModel { } else { // re-run the scorer on the label in the hope of a result BUT use the rank // of the filterText-match - item.score = scoreFn(word, wordLow, wordPos, item.completion.label, item.labelLow, 0, false) || [-100, 0, 0]; + item.score = anyScore(word, wordLow, wordPos, item.completion.label, item.labelLow, 0); item.score[0] = match[0]; // use score from filterText } -- GitLab