Fixed filtering of suggestions with ranges

This commit is contained in:
Maurício Szabo 2023-10-13 22:27:44 -03:00
parent 3cea42c614
commit 4995c7a1e6
2 changed files with 26 additions and 5 deletions

View File

@ -377,14 +377,15 @@ class AutocompleteManager {
const text = (suggestion.snippet || suggestion.text)
const suggestionPrefix = suggestion.replacementPrefix != null ? suggestion.replacementPrefix : prefix
const prefixIsEmpty = !suggestionPrefix || suggestionPrefix === ' '
const firstCharIsMatch = !prefixIsEmpty && suggestionPrefix[0].toLowerCase() === text[0].toLowerCase()
if (prefixIsEmpty) {
results.push(suggestion)
}
if (firstCharIsMatch && (score = fuzzaldrinProvider.score(text, suggestionPrefix)) > 0) {
suggestion.score = score * suggestion.sortScore
results.push(suggestion)
} else {
const keepMatching = suggestion.ranges || suggestionPrefix[0].toLowerCase() === text[0].toLowerCase()
if (keepMatching && (score = fuzzaldrinProvider.score(text, suggestionPrefix)) > 0) {
suggestion.score = score * suggestion.sortScore
results.push(suggestion)
}
}
}

View File

@ -327,5 +327,25 @@ describe('Provider API', () => {
expect(editor.getText()).toEqual("ohai, world\n")
})
it('ignores `prefix` if `range` is present', async () => {
testProvider = {
scopeSelector: '.source.js',
filterSuggestions: true,
getSuggestions (options) {
return [
{text: 'notmatch/foololohairange', ranges: [[[0, 0], [0, 5]]]},
{text: 'notmatch/foololohaiprefix'},
{text: 'foololohaiprefix2'}
]
}
}
registration = atom.packages.serviceHub.provide('autocomplete.provider', '5.0.0', testProvider)
editor.insertText('foololohai')
await triggerAutocompletion()
expect(document.querySelector('autocomplete-suggestion-list').innerText).toMatch(/notmatch\/foololohairange/)
expect(document.querySelector('autocomplete-suggestion-list').innerText).toMatch(/foololohaiprefix2/)
expect(document.querySelector('autocomplete-suggestion-list').innerText).toNotMatch(/notmatch\/foololohaiprefix/)
})
})
})