Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full IDE features support for golang in Doom Emacs [closed]

Tags:

emacs

go

I'm a vim user for years and start to play emacs recently, after having tried Spacemacs and Prelude, Doom Emacs is my final choose. I'm configuring golang development those days with (go +lsp) and (lsp) module, and all the out-of-box features of Doom are impressive, thanks for your outstanding job hlissner.

Now I'm happy with the basic features like:

Auto completion

  • flycheck
  • snippets
  • other go-mode features

But to be more productive, I want more IDE-like features such as:

  • Navigation: Until now I only found counsel-imenu by which I can select symbols in current buffer(candidates not grouped by method/function/var/const/interface and so on), but how could I do things like:
    • Find symbols in a project
    • Find Interfaces in a project
    • Find Structs in a project
    • Find methods in a project
    • Find exported functions, maybe in a specified package
    • Switch back and force between source code and related test file/method
    • And so on...
  • More sophisticated actions to generate/operate code: snippets and go-gen-test are a great help, but is it possible to do the following this:
    • Code actions to generate undeclared method/field/function/package in different scope, now it seems it can only generate variable. (Is this based on the lsp backend support? I found a video for java but not able to find one for go)
    • Auto-generate methods template if I specify an Interface to implement for a Struct
    • Remove methods from a struct
    • Auto-remove relative test/bench functions if I remove a function/method

Could anybody help me on this? And any experiences/tips are appreciated.

like image 999
shizhz Avatar asked May 25 '20 08:05

shizhz


People also ask

How do you close a file in Doom Emacs?

You press “Ctrl” and “x” at the same time, then release them, then press “Ctrl” and “c” at the same time and then release them and voila, you are in the process of exiting Emacs. Doom Note: Emacs has a special extension called “evil mode” that emulates a lot of vi like functionality.

How do you get Doom in Emacs?

To install Doom Emacs, we need to get a remote code repository from the internet. To do this, install Git. It allows us to instantly copy the code and, more importantly, update between major versions. In my case, I am installing Git in Void Linux.


Video Answer


1 Answers

Welcome aboard!

I'm not a go programmer myself, so these are mostly 2nd hand accounts, but Doom's :lang go module provides most of what you're asking for. I would suggest using the gopls langserver. To do so:

  1. Enable these modules:
    • :lang (go +lsp)
    • :tools lsp
    • :editor snippets (should be enabled by default)
  2. Install gopls through your OS' package manager (iirc it comes with Go 1.12+).
  3. Run ~/.emacs.d/bin/doom sync to ensure Doom is properly synchronized with these changes.

And you should be good to go. Here are some helpful (evil centric) keybinds you should know:

  • gd +lookup/definition -> go to definition of the symbol at point
  • gD +lookup/references -> jump to reference of the symbol at point
  • K +lookup/documentation -> look up documentation of the symbol at point
  • SPC c a lsp-execute-code-action -> executes an arbitrary code action (which should include auto-generate what you're asking for).
  • SPC m h d go-guru-describe -> Describe this
  • SPC m h v go-guru-freevars -> List free variables
  • SPC m h i go-guru-implements -> Implements relations for package types
  • SPC m h p go-guru-peers -> List peers for channel
  • SPC m h P go-guru-pointsto -> What does this point to
  • SPC m h r go-guru-referrers -> List references to object
  • SPC m h c go-guru-callers -> Show callers of this function
  • SPC m h C go-guru-callees -> Show callees of this function
  • SPC m t t +go/test-rerun -> rerun last test
  • SPC m t a +go/test-all -> run all tests
  • SPC m t s +go/test-single -> run test at point
  • SPC m t n +go/test-nested -> run tests below

You'll find a more comprehensive list of keybinds in the :lang go module (Note that SPC is Doom's leader key, and SPC m is its localleader key. For non-evil users, these are C-c and C-c l, respectively).

The snippets module comes with a snippets library. You can find our modest go snippets here, and you can add your own to ~/.doom.d/snippets/go-mode/.

I also suggest you visit the :lang go readme. It needs updating, and I'm hardly a good resource on how much of it you need with gopls, but perhaps there are corner cases it can help cover.

Hope that helps!

like image 78
Henrik L. Avatar answered Sep 23 '22 07:09

Henrik L.