Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
Fix prefix calculation when caching
Browse files Browse the repository at this point in the history
- Fixes #745

Co-authored by: Zac Bergquist <zbergquist99@gmail.com>
  • Loading branch information
joefitzgerald committed Apr 2, 2018
1 parent b5500bf commit 1353da9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/autocomplete/gocodeprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GocodeProvider implements AutocompleteProvider {
subscriptions: CompositeDisposable
subscribers: Array<(Promise<any>) => void>
currentSuggestions: Array<Suggestion>
currentPrefixStartIndex: number
proposeBuiltins: bool
unimportedPackages: bool
selector: string
Expand All @@ -50,6 +51,7 @@ class GocodeProvider implements AutocompleteProvider {
this.subscriptions = new CompositeDisposable()
this.subscribers = []
this.currentSuggestions = []
this.currentPrefixStartIndex = -1
this.currentFile = ''
this.currentRow = -1
this.currentColumn = -1
Expand Down Expand Up @@ -179,12 +181,13 @@ class GocodeProvider implements AutocompleteProvider {
this.currentFile = ''
this.currentRow = -1
this.currentColumn = -1
this.currentPrefixStartIndex = -1
}

getSuggestions (options: SuggestionRequest): Promise<Array<Suggestion>> {
// only invoke gocode when a new word starts or the '.' char is entered
// on all other keystrokes we just fuzzy filter the previous set of suggestions
let {prefix, bufferPosition, editor} = options
let {prefix, bufferPosition, editor, scopeDescriptor} = options
prefix = prefix.trim()
if (prefix === '') {
if (!options.activatedManually) {
Expand All @@ -203,8 +206,9 @@ class GocodeProvider implements AutocompleteProvider {
if (useCache && this.currentSuggestions.length && prefix.length > 0 && !prefix.endsWith('.')) {
// fuzzy filter on this.currentSuggestions
const p = new Promise((resolve) => {
const newPrefix = editor.getTextInBufferRange([[bufferPosition.row, bufferPosition.column - this.currentPrefixStartIndex - 1], bufferPosition])
const fil = filter(this.currentSuggestions, prefix, {key: 'fuzzyMatch'})
.map(s => Object.assign({}, s, {replacementPrefix: prefix}))
.map(s => Object.assign({}, s, {replacementPrefix: newPrefix}))
this.currentFile = editor.getPath()
this.currentRow = bufferPosition.row
this.currentColumn = bufferPosition.column
Expand Down Expand Up @@ -351,8 +355,8 @@ class GocodeProvider implements AutocompleteProvider {
if (candidates[0] && candidates[0].class === 'PANIC' && candidates[0].type === 'PANIC' && candidates[0].name === 'PANIC') {
this.bounceGocode()
}
const numPrefix: number = res[0]
const prefix = editor.getTextInBufferRange([[position.row, position.column - numPrefix], position])
this.currentPrefixStartIndex = res[0]
const prefix = editor.getTextInBufferRange([[position.row, position.column - this.currentPrefixStartIndex], position])
let suffix = ''
try {
suffix = editor.getTextInBufferRange([position, [position.row, position.column + 1]])
Expand Down
73 changes: 72 additions & 1 deletion spec/autocomplete/gocodeprovider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,78 @@ describe('gocodeprovider', () => {
})
})

describe('when the go-plus-issue-745 file is opened', () => {
let suggestions = null
beforeEach(() => {
waitsForPromise(() => {
return atom.workspace.open('go-plus-issue-745' + path.sep + 'main.go').then((e) => {
editor = e
editorView = atom.views.getView(editor)
})
})
})

it('calculates the prefix correctly', () => {
runs(() => {
expect(provider).toBeDefined()
expect(provider.getSuggestions).not.toHaveBeenCalled()
expect(editorView.querySelector('.autocomplete-plus')).not.toExist()
editor.setCursorBufferPosition([4, 10])
editor.backspace()
editor.backspace()
editor.backspace()
suggestions = null
suggestionsPromise = null
advanceClock(completionDelay)
})

runs(() => {
expect(provider.getSuggestions.calls.length).toBe(0)
expect(suggestionsPromise).toBeFalsy()
editor.insertText('t')
advanceClock(completionDelay)
})

waitsFor(() => {
return provider.getSuggestions.calls.length === 1 && suggestionsPromise !== null
})

waitsForPromise(() => {
return suggestionsPromise.then((s) => {
suggestions = s
})
})

runs(() => {
expect(provider.getSuggestions.calls.length).toBe(1)
expect(suggestionsPromise).toBeTruthy()
suggestionsPromise = null
editor.insertText('t')
advanceClock(completionDelay)
})

waitsFor(() => {
return provider.getSuggestions.calls.length === 2 && suggestionsPromise !== null
})

waitsForPromise(() => {
return suggestionsPromise.then((s) => {
suggestions = s
})
})

runs(() => {
expect(provider.getSuggestions).toHaveBeenCalled()
expect(provider.getSuggestions.calls.length).toBe(2)
expect(suggestions).toBeTruthy()
expect(suggestions.length).toBeGreaterThan(0)
expect(suggestions[0]).toBeTruthy()
expect(suggestions[0].text).toBe('net/http')
expect(suggestions[0].replacementPrefix).toBe('net/htt')
})
})
})

describe('when the go-plus-issue-307 file is opened', () => {
let suggestions = null
beforeEach(() => {
Expand Down Expand Up @@ -410,7 +482,6 @@ describe('gocodeprovider', () => {
runs(() => {
expect(provider.getSuggestions.calls.length).toBe(0)
expect(suggestionsPromise).toBeFalsy()
console.log('insert .')

editor.insertText('.')
advanceClock(completionDelay)
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/go-plus-issue-745/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"net/http"
)

func main() {
fmt.Println(string(http.MethodGet))
}

0 comments on commit 1353da9

Please sign in to comment.