You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
gopls/doc/emacs.md: describe configuration for eglot
Now that joaotavora/eglot#367 is fixed, I think we should recommend it
as a serious alternative to LSP mode. This change describes both
packages, summarizes their different philosophies, and simplifies the
example LSP Mode configuration to avoid relying on another unnecessary
third-party package.
Users who need more detail on alternative configurations should
consult the LSP client vendors' pages — we don't need to recapitulate
all of that detail here.
Change-Id: If125fbde6d609e223ce44936504cc4b08b84dc3d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/278774
Reviewed-by: Muir Manders <[email protected]>
Reviewed-by: Rebecca Stambler <[email protected]>
Trust: Bryan C. Mills <[email protected]>
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]):
3
+
## Installing `gopls`
4
4
5
-
```lisp
6
-
(use-package lsp-mode
7
-
:ensure t
8
-
:commands (lsp lsp-deferred)
9
-
:hook (go-mode . lsp-deferred))
5
+
To use `gopls` with Emacs, you must first
6
+
[install the `gopls` binary](user.md#installation) and ensure that the directory
7
+
containing the resulting binary (either `$(go env GOBIN)` or `$(go env
8
+
GOPATH)/bin`) is in your `PATH`.
9
+
10
+
## Choosing an Emacs LSP client
11
+
12
+
To use `gopls` with Emacs, you will need to choose and install an Emacs LSP
13
+
client package. Two popular client packages are [LSP Mode] and [Eglot].
14
+
15
+
LSP Mode takes a batteries-included approach, with many integrations enabled
16
+
“out of the box” and several additional behaviors provided by `lsp-mode` itself.
17
+
18
+
Eglot takes a minimally-intrusive approach, focusing on smooth integration with
19
+
other established packages. It provides a few of its own `eglot-` commands but
20
+
no additional keybindings by default.
21
+
22
+
Once you have selected which client you want to use, install it per the packages
23
+
instructions: see [Eglot 1-2-3](https://github.com/joaotavora/eglot#1-2-3) or
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).
60
+
### Configuring `gopls` via LSP Mode
39
61
40
-
## Gopls Configuration
62
+
See [settings] for information about available gopls settings.
41
63
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.
64
+
Stable gopls settings have corresponding configuration variables in `lsp-mode`.
65
+
For example, `(setq lsp-gopls-use-placeholders nil)` will disable placeholders
66
+
in completion snippets. See [`lsp-go`] for a list of available variables.
43
67
44
68
Experimental settings can be configured via `lsp-register-custom-settings`:
45
69
@@ -49,22 +73,107 @@ Experimental settings can be configured via `lsp-register-custom-settings`:
49
73
("gopls.staticcheck" t t)))
50
74
```
51
75
52
-
See [settings] for information about gopls settings.
76
+
Note that after changing settings you must restart gopls using e.g. `M-x
77
+
lsp-restart-workspace`.
78
+
79
+
## Configuring Eglot
80
+
81
+
### Configuring `project` for Go modules in `.emacs`
82
+
83
+
Eglot uses the built-in `project` package to identify the LSP workspace for a
84
+
newly-opened buffer. The `project` package does not natively know about `GOPATH`
85
+
or Go modules. Fortunately, you can give it a custom hook to tell it to look for
86
+
the nearest parent `go.mod` file (that is, the root of the Go module) as the
87
+
project root.
88
+
89
+
```elisp
90
+
(require 'project)
91
+
92
+
(defun project-find-go-module (dir)
93
+
(when-let ((root (locate-dominating-file dir "go.mod")))
See [settings] for information about available gopls settings.
124
+
125
+
LSP server settings are controlled by the `eglot-workspace-configuration`
126
+
variable, which can be set either globally in `.emacs` (as below) or in a
127
+
`.dir-locals.el` file in the project root.
128
+
129
+
```elisp
130
+
(setq-default eglot-workspace-configuration
131
+
'((:gopls .
132
+
((staticcheck . t)
133
+
(matcher . "CaseSensitive")))))
134
+
```
135
+
136
+
### Organizing imports with Eglot
137
+
138
+
`gopls` provides the import-organizing functionality of `goimports` as an LSP
139
+
code action, which you can invoke as needed by running `M-x eglot-code-actions`
140
+
(or a key of your choice bound to the `eglot-code-actions` function) and
141
+
selecting `Organize Imports` at the prompt.
142
+
143
+
Eglot does not currently support a standalone function to execute a specific
144
+
code action (see
145
+
[joaotavora/eglot#411](https://github.com/joaotavora/eglot/issues/411)), nor an
146
+
option to organize imports as a `before-save-hook` (see
147
+
[joaotavora/eglot#574](https://github.com/joaotavora/eglot/issues/574)). In the
148
+
meantime, see those issues for discussion and possible workarounds.
55
149
56
150
## Troubleshooting
57
151
58
152
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].
0 commit comments