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

Commit

Permalink
improve autocomplete provider
Browse files Browse the repository at this point in the history
- don't generate snippets for ...args
- support fuzzy matching

fixes #664, #544, #188
  • Loading branch information
zmb3 committed Jul 22, 2017
1 parent 64863c8 commit 8223637
Show file tree
Hide file tree
Showing 4 changed files with 1,571 additions and 12 deletions.
40 changes: 32 additions & 8 deletions lib/autocomplete/gocodeprovider.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
'use babel'

import {CompositeDisposable} from 'atom'
import {isValidEditor} from '../utils'
import {filter} from 'fuzzaldrin-plus'
import _ from 'lodash'
import os from 'os'
import {isValidEditor} from '../utils'

class GocodeProvider {
constructor (goconfig) {
this.goconfig = goconfig
this.subscriptions = new CompositeDisposable()
this.subscribers = []
this.currentSuggestions = []

this.proposeBuiltins = true
this.unimportedPackages = true
Expand Down Expand Up @@ -61,6 +63,7 @@ class GocodeProvider {
this.subscriptions = null
this.goconfig = null
this.subscribers = null
this.currentSuggestions = null
this.inclusionPriority = null
this.excludeLowerPriority = null
this.suppressForCharacters = null
Expand Down Expand Up @@ -139,6 +142,22 @@ class GocodeProvider {
}

getSuggestions (options) {
// 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
const {prefix} = options
if (prefix.trim() === '') {
this.currentSuggestions = []
return []
}
if (prefix.length !== 1) {
// fuzzy filter on this.currentSuggestions
return new Promise((resolve) => {
resolve(filter(this.currentSuggestions, prefix, {key: 'fuzzyMatch'})
.map(s => Object.assign({}, s, {replacementPrefix: prefix})))
})
}

// get a fresh set of suggestions from gocode
const p = new Promise((resolve) => {
if (!options || !this.ready() || !isValidEditor(options.editor)) {
return resolve()
Expand Down Expand Up @@ -180,6 +199,7 @@ class GocodeProvider {
if (!messages || messages.length < 1) {
return resolve()
}
this.currentSuggestions = messages
resolve(messages)
}).catch((e) => {
console.log(e)
Expand Down Expand Up @@ -247,6 +267,7 @@ class GocodeProvider {
suggestion = this.upgradeSuggestion(suggestion, c)
} else {
suggestion.text = c.name
suggestion.fuzzyMatch = suggestion.text
}
if (suggestion.type === 'package') {
suggestion.iconHTML = '<i class="icon-package"></i>'
Expand Down Expand Up @@ -491,10 +512,10 @@ class GocodeProvider {
suggestion.leftLabel = suggestion.leftLabel + ')'
}
}

const res = this.generateSnippet(c.name, type)
suggestion.snippet = res.snippet
suggestion.displayText = res.displayText
suggestion.fuzzyMatch = c.name
return suggestion
}

Expand Down Expand Up @@ -598,11 +619,14 @@ class GocodeProvider {
return result
}
let snipCount = 0
let argCount = 0
for (const arg of type.args) {
argCount = argCount + 1
if (argCount !== 1) {
result.snippet = result.snippet + ', '
for (let argCount = 0; argCount < type.args.length; argCount++) {
const arg = type.args[argCount]

// omit variadic arguments
const generateArgSnippet = !(argCount === type.args.length - 1 && typeof arg.type === 'string' && arg.type.startsWith('...'))

if (argCount !== 0) {
if (generateArgSnippet) { result.snippet = result.snippet + ', ' }
result.displayText = result.displayText + ', '
}
if (arg.isFunc) {
Expand All @@ -619,7 +643,7 @@ class GocodeProvider {
argText = argText.substring(0, argText.length - 1) + '\\}'
}
snipCount = snipCount + 1
result.snippet = result.snippet + '${' + snipCount + ':' + argText + '}'
if (generateArgSnippet) { result.snippet = result.snippet + '${' + snipCount + ':' + argText + '}' }
result.displayText = result.displayText + arg.name
}
}
Expand Down
Loading

0 comments on commit 8223637

Please sign in to comment.