Add lsp support for go

This commit is contained in:
Lupco Kotev 2018-06-12 00:05:13 +02:00 committed by Codruț Constantin Gușoi
parent 6b0e696208
commit bb3e3b2103
4 changed files with 92 additions and 1 deletions

View File

@ -7,6 +7,7 @@
- [[#features][Features:]]
- [[#install][Install]]
- [[#pre-requisites][Pre-requisites]]
- [[#lsp-backend][LSP backend]]
- [[#layer][Layer]]
- [[#configuration][Configuration]]
- [[#indentation][Indentation]]
@ -32,6 +33,7 @@ This layer adds extensive support for go to Spacemacs.
- Test generation with [[https://github.com/s-kostyaev/go-gen-test][go-gen-test]]
- Get packages faster with [[https://github.com/haya14busa/gopkgs][gopkgs]]
- Fill a structure with default values using the [[https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct][fillstruct]]
- LSP backend
* Install
** Pre-requisites
@ -87,6 +89,32 @@ $PATH RET~).
For best results, make sure that the =auto-completion= and =syntax-checking=
layers are enabled as well.
** LSP backend
To enable the LSP backend set the layer variable =go-backend=:
#+BEGIN_SRC elisp
(go :variables go-backend 'lsp)
#+END_SRC
You also need to install the Go Language Server:
#+BEGIN_SRC sh
go get -u github.com/sourcegraph/go-langserver
#+END_SRC
Backend can be chosen on a per project basis using directory local variables
(files named =.dir-locals.el= at the root of a project), an example to use the
=default= backend:
#+BEGIN_SRC elisp
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")
((go-mode (go-backend . default)))
#+END_SRC
*Note:* you can easily add a directory local variable with ~SPC f v d~.
** Layer
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =go= to the existing =dotspacemacs-configuration-layers= list in this

View File

@ -13,6 +13,8 @@
(spacemacs|define-jump-handlers go-mode godef-jump)
(defvar go-backend 'default)
(defvar go-use-gocheck-for-testing nil
"If using gocheck for testing when running the tests -check.f will be used instead of -run to specify the test that will be ran. Gocheck is mandatory for testing suites.")

View File

@ -14,6 +14,50 @@
(when go-tab-width
(setq-local tab-width go-tab-width)))
(defun spacemacs//go-setup-backend ()
"Conditionally setup go backend"
(pcase go-backend
('default (spacemacs//go-setup-backend-default))
('lsp (spacemacs//go-setup-backend-lsp))))
(defun spacemacs//go-setup-company ()
"Conditionally setup go company based on backend"
(pcase go-backend
('default (spacemacs//go-setup-company-go))
('lsp (spacemacs//go-setup-company-lsp))))
(defun spacemacs//go-setup-backend-default ()
(company-go))
(defun spacemacs//go-setup-company-go ()
(spacemacs|add-company-backends
:backends company-go
:modes go-mode
:variables company-go-show-annotation t
:append-hooks nil
:call-hooks t)
(company-mode))
(defun spacemacs//go-setup-backend-lsp ()
"Setup lsp backend"
(if (configuration-layer/layer-used-p 'lsp)
(progn
(require 'lsp-go)
(lsp-go-enable))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(defun spacemacs//go-setup-company-lsp ()
"Setup lsp auto-completion"
(if (configuration-layer/layer-used-p 'lsp)
(progn
(spacemacs|add-company-backends
:backends company-lsp
:modes go-mode
:append-hooks nil
:call-hooks t)
(company-mode))
(message "`lsp' layer is not installed, please add `lsp' layer to your dotfile.")))
(defun spacemacs//go-enable-gometalinter ()
"Enable `flycheck-gometalinter' and disable overlapping `flycheck' linters."
(setq flycheck-disabled-checkers '(go-gofmt

View File

@ -11,6 +11,7 @@
(setq go-packages
'(
company
(company-go :requires company)
counsel-gtags
flycheck
@ -28,6 +29,10 @@
go-rename
go-tag
godoctor
(lsp-go
:requires lsp-mode
:location (recipe :fetcher github
:repo "emacs-lsp/lsp-go"))
popwin
))
@ -39,6 +44,13 @@
:modes go-mode
:variables company-go-show-annotation t)))
(defun go/init-lsp-go ()
(use-package lsp-go
:commands lsp-go-enable))
(defun go/post-init-company ()
(add-hook 'go-mode-local-vars-hook #'spacemacs//go-setup-company))
(defun go/post-init-counsel-gtags ()
(spacemacs/counsel-gtags-define-keys-for-mode 'go-mode))
@ -108,7 +120,12 @@
(progn
;; get go packages much faster
(setq go-packages-function 'spacemacs/go-packages-gopkgs)
(add-hook 'go-mode-hook 'spacemacs//go-set-tab-width))
(add-hook 'go-mode-hook 'spacemacs//go-set-tab-width)
(add-hook 'go-mode-local-vars-hook
#'spacemacs//go-setup-backend)
(dolist (value '(lsp default))
(add-to-list 'safe-local-variable-values
(cons 'go-backend value))))
:config
(progn
(when go-format-before-save