Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #930 from atom/fix-nonwordchar-replacement
Browse files Browse the repository at this point in the history
Fix non-word character replacement
  • Loading branch information
leroix committed Nov 23, 2017
2 parents dee395b + 07fcb56 commit f85ad55
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
29 changes: 10 additions & 19 deletions lib/autocomplete-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class AutocompleteManager {

const bufferPosition = cursor.getBufferPosition()
const scopeDescriptor = cursor.getScopeDescriptor()
const prefix = this.getPrefix(this.editor, bufferPosition)
const prefix = cursor.getCurrentWordPrefix().replace(/.* +/, ' ')

return this.getSuggestionsFromProviders({editor: this.editor, bufferPosition, scopeDescriptor, prefix, activatedManually})
}
Expand Down Expand Up @@ -300,7 +300,15 @@ class AutocompleteManager {
for (let i = 0; i < providerSuggestions.length; i++) {
const suggestion = providerSuggestions[i]
if (!suggestion.snippet && !suggestion.text) { hasEmpty = true }
if (suggestion.replacementPrefix == null) { suggestion.replacementPrefix = this.getDefaultReplacementPrefix(options.prefix) }
if (suggestion.replacementPrefix == null) {
const cursor = options.editor.getLastCursor()
const wordRegExp = cursor.wordRegExp({includeNonWordCharacters: false})
if (wordRegExp.test(options.prefix) === false) {
suggestion.replacementPrefix = ''
} else {
suggestion.replacementPrefix = options.prefix
}
}
suggestion.provider = provider
}

Expand Down Expand Up @@ -472,23 +480,6 @@ See https://github.com/atom/autocomplete-plus/wiki/Provider-API`
return result
}

getPrefix (editor, bufferPosition) {
const line = editor.getTextInRange([[bufferPosition.row, 0], bufferPosition])
const prefix = this.prefixRegex.exec(line)
if (!prefix || !prefix[2]) {
return ''
}
return prefix[2]
}

getDefaultReplacementPrefix (prefix) {
if (this.wordPrefixRegex.test(prefix)) {
return prefix
} else {
return ''
}
}

// Private: Gets called when the user successfully confirms a suggestion
//
// match - An {Object} representing the confirmed suggestion
Expand Down
4 changes: 2 additions & 2 deletions spec/autocomplete-manager-integration-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,10 @@ describe('Autocomplete Manager', () => {
})

it('calls with word prefix containing a dash', () => {
editor.insertText('-okyea')
editor.insertText('_okyea')
editor.insertText('h')
waitForAutocomplete()
runs(() => expect(prefix).toBe('abc-okyeah'))
runs(() => expect(prefix).toBe('abc_okyeah'))
})

it('calls with space character', () => {
Expand Down
6 changes: 5 additions & 1 deletion spec/subsequence-provider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ describe('SubsequenceProvider', () => {
waitsForPromise(() =>
suggestionsForPrefix(provider, editor, 'good').then(sugs => {
expect(sugs).not.toContain('good-noodles')
atom.config.set('editor.nonWordCharacters', '/\\()"\':,.;<>~!@#$%^&*|+=[]{}`?…')
atom.config.set(
'editor.nonWordCharacters',
'/\\()"\':,.;<>~!@#$%^&*|+=[]{}`?…',
{scopeSelector: editor.getLastCursor().getScopeDescriptor().getScopeChain()}
)
return suggestionsForPrefix(provider, editor, 'good')
}).then(sugs => {
expect(sugs).toContain('good-noodles')
Expand Down

0 comments on commit f85ad55

Please sign in to comment.