Skip to content

Commit

Permalink
Fixed some suggestion elements from Autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricioszabo committed Oct 17, 2023
1 parent 3917ba4 commit 5bc996a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
35 changes: 6 additions & 29 deletions packages/autocomplete-plus/lib/suggestion-list-element.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const {CompositeDisposable} = require('atom')
const SnippetParser = require('./snippet-parser')
const {isString} = require('./type-helpers')
const fuzzaldrinPlus = require('fuzzaldrin-plus')
const {marked} = require('marked')
const createDOMPurify = require('dompurify')

Expand Down Expand Up @@ -83,9 +82,6 @@ module.exports = class SuggestionListElement {
this.subscriptions.add(atom.config.observe('autocomplete-plus.maxVisibleSuggestions', maxVisibleSuggestions => {
this.maxVisibleSuggestions = maxVisibleSuggestions
}))
this.subscriptions.add(atom.config.observe('autocomplete-plus.useAlternateScoring', useAlternateScoring => {
this.useAlternateScoring = useAlternateScoring
}))
this.subscriptions.add(atom.config.observe('autocomplete-plus.moveToCancel', moveToCancel => {
this.moveToCancel = moveToCancel
}))
Expand Down Expand Up @@ -544,12 +540,8 @@ module.exports = class SuggestionListElement {

if (!characterMatchIndices) {
characterMatchIndices = this.findCharacterMatchIndices(replacementText, replacementPrefix)
} else {
characterMatchIndices = characterMatchIndices.reduce((matches, index) => {
matches[index] = true
return matches
}, {})
}
characterMatchIndices = new Set(characterMatchIndices);

const appendNonMatchChars = (el, nonMatchChars) => {
if (nonMatchChars) {
Expand All @@ -572,7 +564,7 @@ module.exports = class SuggestionListElement {
workingEl = s
}

if (characterMatchIndices && characterMatchIndices[index]) {
if (characterMatchIndices && characterMatchIndices.has(index)) {
appendNonMatchChars(workingEl, nonMatchChars)
nonMatchChars = ''

Expand Down Expand Up @@ -668,26 +660,11 @@ module.exports = class SuggestionListElement {
//
// Returns an {Object}
findCharacterMatchIndices (text, replacementPrefix) {
if (!text || !text.length || !replacementPrefix || !replacementPrefix.length) { return }
if (!text?.length || !replacementPrefix?.length) { return }
const matches = {}
if (this.useAlternateScoring) {
const matchIndices = fuzzaldrinPlus.match(text, replacementPrefix)
for (const i of matchIndices) {
matches[i] = true
}
} else {
let wordIndex = 0
for (let i = 0; i < replacementPrefix.length; i++) {
const ch = replacementPrefix[i]
while (wordIndex < text.length && text[wordIndex].toLowerCase() !== ch.toLowerCase()) {
wordIndex += 1
}
if (wordIndex >= text.length) { break }
matches[wordIndex] = true
wordIndex += 1
}
}
return matches
return atom.ui.fuzzyMatcher.match(
text, replacementPrefix, {recordMatchIndexes: true}
)?.matchIndexes || [];
}

dispose () {
Expand Down
20 changes: 13 additions & 7 deletions packages/autocomplete-plus/spec/suggestion-list-element-spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env jasmine */
/* eslint-disable no-template-curly-in-string */
const SuggestionListElement = require('../lib/suggestion-list-element')
const { conditionPromise } = require('./spec-helper')

const fragmentToHtml = fragment => {
const el = document.createElement('span')
Expand Down Expand Up @@ -63,11 +64,12 @@ describe('Suggestion List Element', () => {
})

describe('itemChanged', () => {
beforeEach(() => jasmine.attachToDOM(suggestionListElement.element))

it('updates the list item', async () => {
beforeEach(() => {
jasmine.useRealClock()
jasmine.attachToDOM(suggestionListElement.element)
})

it('updates the list item', async () => {
const suggestion = {text: 'foo'}
const newSuggestion = {text: 'foo', description: 'foobar', rightLabel: 'foo'}
suggestionListElement.model = {items: [newSuggestion]}
Expand All @@ -77,6 +79,9 @@ describe('Suggestion List Element', () => {

suggestionListElement.itemChanged({suggestion: newSuggestion, index: 0})

await conditionPromise(() =>
suggestionListElement.element.querySelector('.right-label').innerText
)
expect(suggestionListElement.element.querySelector('.right-label').innerText)
.toBe('foo')

Expand Down Expand Up @@ -190,14 +195,15 @@ describe('Suggestion List Element', () => {
let snippets = suggestionListElement.snippetParser.findSnippets(text)
text = suggestionListElement.removeSnippetsFromText(snippets, text)
let matches = suggestionListElement.findCharacterMatchIndices(text, replacementPrefix)
matches = new Set(matches)

for (var i = 0; i <= text.length; i++) {
if (truthyIndices.indexOf(i) !== -1) {
expect(matches[i]).toBeTruthy()
expect(matches.has(i)).toBeTruthy()
} else {
let m = matches
if (m) {
m = m[i]
m = m.has(i)
}
expect(m).toBeFalsy()
}
Expand All @@ -208,14 +214,14 @@ describe('Suggestion List Element', () => {
assertMatches('hello', '', [])
assertMatches('hello', 'h', [0])
assertMatches('hello', 'hl', [0, 2])
assertMatches('hello', 'hlo', [0, 2, 4])
assertMatches('hello', 'hlo', [0, 3, 4])
})

it('finds matches when snippets exist', () => {
assertMatches('${0:hello}', '', [])
assertMatches('${0:hello}', 'h', [0])
assertMatches('${0:hello}', 'hl', [0, 2])
assertMatches('${0:hello}', 'hlo', [0, 2, 4])
assertMatches('${0:hello}', 'hlo', [0, 3, 4])
assertMatches('${0:hello}world', '', [])
assertMatches('${0:hello}world', 'h', [0])
assertMatches('${0:hello}world', 'hw', [0, 5])
Expand Down

0 comments on commit 5bc996a

Please sign in to comment.