New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autocomplete for Go, based on gocode, fixes #174 #977

Open
wants to merge 35 commits into
base: master
from

Conversation

Projects
None yet
10 participants
@a-h
Copy link

a-h commented Jan 3, 2018

Adds autocompletion for Go as a feature (disabled by default, but can be enabled in settings).

To enable

press CtrlE to go to command mode, and enter setlocal autocomplete true. This will enable autocomplete for the file until closed (the setting will not persist).

Requirements

Must have gocode installed and available on the path.

Features

  • Triggered by . or ( characters.
  • Allows use of arrow keys to select item.
  • Press enter or tab to accept a selection.
  • Press escape to hide the list.

Other language support

Support for other languages can be added by accessing the properties and methods from plugins (see plugins.md) or by adding support within micro by:

  • Creating a function which has the OptionProvider signature (the function accepts the contents of the file and the byte offset within that file).
type OptionProvider func(buffer []byte, offset int) (options []optionprovider.Option, err error)
  • Updating the NewCompleterForView function to choose the correct completion function based on the filename or other aspects of the file, e.g. the Go implementation simply checks for a .go file extension.
// Load the provider based on filename.
fileName := v.Buf.GetName()
if strings.HasSuffix(fileName, ".go") {
	provider = optionprovider.GoCode
}

@a-h a-h referenced this pull request Jan 3, 2018

Open

Auto completion #174

@zyedidia

This comment has been minimized.

Copy link
Owner

zyedidia commented Jan 6, 2018

Sorry I'll try to look at this soon. As you maybe can tell, I am very slow with large pull requests.

@ghost

This comment has been minimized.

Copy link

ghost commented Jan 6, 2018

maybe a default option for auto-completing based on all words in currently opened files could be added? or we all have to start writing go 😄
In the long run other plugins will probably exist (as you say) but just to not make other language users feel so left behind until that eventually happens?

@sum01

This comment has been minimized.

Copy link
Contributor

sum01 commented Jan 6, 2018

If it auto-completed based on all words, you'd have an ever-growing list that would become useless the longer the file got.

I didn't look into how plugins extend this yet, but if it's just a function with a word list, it should be easy to drop-in word lists in a one-off plugin.

@ghost

This comment has been minimized.

Copy link

ghost commented Jan 6, 2018

I use „all auto complete“ in sublime and actually like it to be honest.
It is not as great as full IDE like support but works reasonably well.

@zyedidia

This comment has been minimized.

Copy link
Owner

zyedidia commented Jan 6, 2018

Yes I agree that this should be the default autocomplete if there is no additional support for the language. I know it as "buffer complete" from vim and it's pretty effective for having no knowledge of the semantics of the language.

@a-h

This comment has been minimized.

Copy link

a-h commented Jan 6, 2018

No need to apologise @zyedidia I appreciate the work you’ve put in and continue to put in. There’s no pressure from me. :)

@a-h

This comment has been minimized.

Copy link

a-h commented Jan 11, 2018

I've added a basic generic autocomplete to the pull request that orders the suggestions based on:

  • The prefix you've entered so far since the last activation character (e.g. ., ;, \n or )
  • The frequency of the word (more frequent is better)
  • Alphabetic ordering

screen shot 2018-01-11 at 08 58 08

a-h added some commits Feb 4, 2018

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 4, 2018

I've merged the latest commits from master into this pull request. Since it's disabled by default, it shouldn't cause any problems if merged to master and released, since it's disabled by default. (I'd like people to test it out and provide feedback before later switching it on by default.)

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 4, 2018

I wanted to try your branch but it did not build, (zyedidia/micro worked)

thanks for your work!

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 4, 2018

@monouser7dig - just checked it again. It worked for me, and it's working for Travis CI. I had to use a make clean and a make build. I've got two remotes setup, one pointing at my repo (a-h), and the other pointing at https://github.com/zyedidia/micro (origin).

Happy to help with any build errors if you've got logs etc.

@sum01

This comment has been minimized.

Copy link
Contributor

sum01 commented Feb 4, 2018

I just tried and got cmd/micro/completer.go:7:2: cannot find package "github.com/zyedidia/micro/cmd/micro/optionprovider" but I'm probably doing something stupid with the Go build process again.

@zyedidia

This comment has been minimized.

Copy link
Owner

zyedidia commented Feb 4, 2018

That's an issue with the naming of the fork. You should clone this fork clone into $GOPATH/src/github.com/zyedidia/micro (not a-h/micro). That way the import paths will work out.

@sum01

This comment has been minimized.

Copy link
Contributor

sum01 commented Feb 4, 2018

Weird. It worked on a new file I opened to test, just recommened the words I've already typed, but after closing it and trying others (making sure to set autocomplete true each time) it stopped doing anything.

Side-note: The box stays floating there if you backspace, instead of vanishing.

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 4, 2018

works zyedidia thanks, go get is not my favourite part of go, for sure

so my nits are:

  • tab does not accept the first match by default
  • hitting enter/ entering a new line pops up the autocomplete which is disturbing
  • I'd like to always have current completions (during typing), that is how it's setup in my sublime which I think is pretty default
  • just pressing space triggers completion, this makes not much sense because there is nothing to base the completion on

(I'm talking about the all-auto-conplete feature)

hope that makes some sense

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 14, 2018

Did some testing and made a few other improvements.

@monouser7dig - good idea on the Go autocomplete.
@sum01 - just got it to remove the options if you backspace.
@avently - thanks for that, for the generic completion, I've removed non-matching suggestions in the case that you start with a letter. If you start with a period or bracket, then you're probably calling a function, so it gives you a list of everything.

@uznemirivac - I'm not sure about the OmniSharp part, but if you read at the top of this pull request, there's instructions for adding another completer alongside (search for Other language support).

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 14, 2018

Hang on, just a couple of things I want to do before it's ready for re-test.

@avently

This comment has been minimized.

Copy link

avently commented Feb 14, 2018

@a-h if you don't mind i will write more thoughts about that PR after testing. You are making a huge work and if you will make it great this will be a start of autocomplition for micro. So then devs can create completions for other languages based on your work.

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 17, 2018

OK, I'm pretty happy with it now. Example generic replacement:

start_partway

Example Go replacement using GoCode. It now falls back to Generic if gocode is not available as per @monouser7dig's suggestion.

@monouser7dig - the garbling is now fixed too.

go_complete

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 17, 2018

Did not do a whole lot testing but seems good now,
one nit: I'd prefer to have the first word highlighted cause that is the one that will be selected automatically (that is how sublime behaves as well fwiw).

@a-h

This comment has been minimized.

Copy link

a-h commented Feb 17, 2018

OK, I've done that @monouser7dig.

@zyedidia - any thoughts?

@ghost

This comment has been minimized.

Copy link

ghost commented Feb 17, 2018

just installed gocode and and added it to the path thus it's running fine from the command line but even in a new terminal/ micro refreshed and with a go file, micro does not switch to syntactic completions.

and I noticed the dropdown will eventually draw below/ beyond the bottom bar which has the file path etc. in it

@ghost

This comment has been minimized.

Copy link

ghost commented Mar 29, 2018

ping @zyedidia some opinion?

@zyedidia

This comment has been minimized.

Copy link
Owner

zyedidia commented Mar 30, 2018

I think this looks good at this point. I noticed some little problems, namely I tried typing fmt.Println("... and as I kept typing into the ... the same suggestions stayed even though what I had typed no longer matched.

I'll look a bit deeper into the code to see if I can find any problems there. This is a pretty big pull request!

@a-h

This comment has been minimized.

Copy link

a-h commented Apr 29, 2018

Hi @zyedidia deactivating the autocomplete options when they no longer become relevant is the domain of the Deactivators. When someone types a deactivator, it stops prompting.

The defaults are: const defaultDeactivators = "), \n."

As you can probably work out from that snippet, the intention was that if you're in the middle of an argument list, then you probably want to keep having suggestions, since you might be prompted as to what the arguments are, or you might backspace and try a different method.

The suggestions also disappear if you hit Esc.

@Nufflee

This comment has been minimized.

Copy link

Nufflee commented Aug 10, 2018

Any updates on this?

@a-h

This comment has been minimized.

Copy link

a-h commented Aug 11, 2018

@zyedidia - if I make the autocomplete options disappear after the prefix no longer matches, will it get merged then? Or will something else have to be done, then something else etc.?

I'm willing to do more work to get it merged, I just don't know what that work would be and don't want to waste time on zombie pull requests. 😁

@liwei00wade

This comment has been minimized.

Copy link

liwei00wade commented Sep 27, 2018

press CtrlE to go to command mode, and enter setlocal autocomplete true. This will enable autocomplete for the file until closed (the setting will not persist).

It does not work,

when enter setlocal autocomplete true , print Invalid option

wade@wade-work:~/goWorkSpace/src/192.168.1.188/ipc/ipc-bridge$ micro -version
Version: 1.4.1
Commit hash: 1856891
Compiled on August 10, 2018

@AkihiroSuda

This comment has been minimized.

Copy link

AkihiroSuda commented Oct 17, 2018

What's current status?

@franzflasch

This comment has been minimized.

Copy link

franzflasch commented Dec 7, 2018

@zyedidia What is your timeplan for this PR? I've tested it and it seems to work great so far.

@atakanyenel

This comment has been minimized.

Copy link

atakanyenel commented Dec 13, 2018

@liwei00wade that's because you are using the micro release from the master branch. It still doesn't have that option as it is not merged. To enable this feature, you have to clone this repo, go to this pull request and compile yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment