Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools / gopls@v0.5.2 / doc / emacs.md
1 # Emacs
2
3 Use [lsp-mode]. gopls is built in as a client. You first must install `gopls` and put it somewhere in your `PATH`. Here is a basic config to get you started (assuming you are using [use-package]):
4
5 ```lisp
6 (use-package lsp-mode
7   :ensure t
8   :commands (lsp lsp-deferred)
9   :hook (go-mode . lsp-deferred))
10
11 ;; Set up before-save hooks to format buffer and add/delete imports.
12 ;; Make sure you don't have other gofmt/goimports hooks enabled.
13 (defun lsp-go-install-save-hooks ()
14   (add-hook 'before-save-hook #'lsp-format-buffer t t)
15   (add-hook 'before-save-hook #'lsp-organize-imports t t))
16 (add-hook 'go-mode-hook #'lsp-go-install-save-hooks)
17
18 ;; Optional - provides fancier overlays.
19 (use-package lsp-ui
20   :ensure t
21   :commands lsp-ui-mode)
22
23 ;; Company mode is a standard completion package that works well with lsp-mode.
24 (use-package company
25   :ensure t
26   :config
27   ;; Optionally enable completion-as-you-type behavior.
28   (setq company-idle-delay 0)
29   (setq company-minimum-prefix-length 1))
30
31 ;; Optional - provides snippet support.
32 (use-package yasnippet
33   :ensure t
34   :commands yas-minor-mode
35   :hook (go-mode . yas-minor-mode))
36 ```
37
38 lsp-mode integrates with xref. By default `lsp-find-definition` is bound to `M-.`. To go back, use `M-,`. Explore other `lsp-*` commands (not everything is supported by gopls).
39
40 ## Gopls Configuration
41
42 Stable gopls settings have first-class support in [lsp-mode]. For example, `(setq lsp-gopls-use-placeholders nil)` will disable placeholders in completion snippets. See [lsp-go] for a list of available variables.
43
44 Experimental settings can be configured via `lsp-register-custom-settings`:
45
46 ```lisp
47 (lsp-register-custom-settings
48  '(("gopls.completeUnimported" t t)
49    ("gopls.staticcheck" t t)))
50 ```
51
52 See [settings] for information about gopls settings.
53
54 Note that after changing settings you must restart gopls using e.g. `M-x lsp-restart-workspace`.
55
56 ## Troubleshooting
57
58 Common errors:
59 - When prompted by Emacs for your project folder, if you are using modules you must select the module's root folder (i.e. the directory with the "go.mod"). If you are using GOPATH, select your $GOPATH as your folder.
60 - Emacs must have your environment set properly (PATH, GOPATH, etc). You can run `M-x getenv <RET> PATH <RET>` to see if your PATH is set in Emacs. If not, you can try starting Emacs from your terminal, using [this package][exec-path-from-shell], or moving your shell config from .bashrc into .bashenv (or .zshenv).
61 - Make sure `lsp-mode` and `lsp-ui` are up-to-date, also make sure `lsp-go` and `company-lsp` are _not_ installed.
62 - Look for errors in the `*lsp-log*` buffer.
63 - Ask for help in the #emacs channel on the [Gophers slack].
64
65 [lsp-mode]: https://github.com/emacs-lsp/lsp-mode
66 [use-package]: https://github.com/jwiegley/use-package
67 [exec-path-from-shell]: https://github.com/purcell/exec-path-from-shell
68 [settings]: settings.md
69 [lsp-go]: https://github.com/emacs-lsp/lsp-mode/blob/master/lsp-go.el
70 [Gophers slack]: https://invite.slack.golangbridge.org/