| 1 | --- |
| 2 | title: "Gopls: Passive features" |
| 3 | --- |
| 4 | |
| 5 | This page documents the fundamental LSP features of gopls that may be |
| 6 | described as "passive", since many editors use them to continuously |
| 7 | provide information about your source files without requiring any |
| 8 | special action. |
| 9 | |
| 10 | See also [Code Lenses](../codelenses.md), some of which annotate your |
| 11 | source code with additional information and may thus also be |
| 12 | considered passive features. |
| 13 | |
| 14 | |
| 15 | ## Hover |
| 16 | |
| 17 | The LSP [`textDocument/hover`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover) |
| 18 | query returns a description of the code currently under the cursor, such |
| 19 | as its name, kind, type, value (for a constant), abbreviated |
| 20 | declaration (for a type), doc comment (if any), and a link to the |
| 21 | symbol's documentation on `pkg.go.dev`. The client may request either |
| 22 | plain text or Markdown. |
| 23 | |
| 24 | <img src='../assets/hover-basic.png'> |
| 25 | |
| 26 | Depending on the selection, the response may include additional information. |
| 27 | For example, hovering over a type shows its declared methods, |
| 28 | plus any methods promoted from embedded fields. |
| 29 | |
| 30 | **Doc links**: A doc comment may refer to another symbol using square |
| 31 | brackets, for example `[fmt.Printf]`. Hovering over one of these |
| 32 | [doc links](https://go.dev/doc/comment#doclinks) reveals |
| 33 | information about the referenced symbol. |
| 34 | |
| 35 | <img src='../assets/hover-doclink.png'> |
| 36 | |
| 37 | **Struct size/offset info**: for declarations of struct types, |
| 38 | hovering over the name reveals the struct's size in bytes: |
| 39 | |
| 40 | <img title="struct size info" src="../assets/hover-size-struct.png"> |
| 41 | |
| 42 | And hovering over each field name shows the size and offset of that field: |
| 43 | |
| 44 | <img title="field size/offset info" src="../assets/hover-size-field.png"> |
| 45 | |
| 46 | This information may be useful when optimizing the layout of your data |
| 47 | structures, or when reading assembly files or stack traces that refer |
| 48 | to each field by its cryptic byte offset. |
| 49 | |
| 50 | In addition, Hover reports: |
| 51 | - the struct's size class, which is the number of bytes actually |
| 52 | allocated by the Go runtime for a single object of this type; and |
| 53 | - the percentage of wasted space due to suboptimal ordering of struct |
| 54 | fields, if this figure is 20% or higher: |
| 55 | |
| 56 | <img title="a struct with wasted space" src="../assets/hover-size-wasteful.png"> |
| 57 | |
| 58 | In the struct above, alignment rules require each of the two boolean |
| 59 | fields (1 byte) to occupy a complete word (8 bytes), leading to (7 + |
| 60 | 7) / (3 * 8) = 58% waste. |
| 61 | Placing the two booleans together would save a word. |
| 62 | (In most structures clarity is more important than compactness, so you |
| 63 | should reorder fields to save space only in data structures that have |
| 64 | been shown by profiling to be very frequently allocated.) |
| 65 | |
| 66 | **Embed directives**: hovering over the file name pattern in |
| 67 | [`//go:embed` directive](https://pkg.go.dev/embed), for example |
| 68 | `*.html`, reveals the list of file names to which the wildcard |
| 69 | expands. |
| 70 | |
| 71 | <img src='../assets/hover-embed.png'> |
| 72 | <!-- NB: Emacs+eglot displays only the first line of markdown, not the useful part! --> |
| 73 | |
| 74 | **Linkname directives**: a [`//go:linkname` directive](https://pkg.go.dev/cmd/compile#hdr-Compiler_Directives) creates a linker-level alias for another symbol. |
| 75 | Hovering over the directive shows information about the other symbol. |
| 76 | |
| 77 | <img src='../assets/hover-linkname.png'> |
| 78 | |
| 79 | The hover information for symbols from the standard library added |
| 80 | after Go 1.0 states the Go release that added the symbol. |
| 81 | |
| 82 | Settings: |
| 83 | - The [`hoverKind`](../settings.md#hoverKind) setting controls the verbosity of documentation. |
| 84 | - The [`linkTarget`](../settings.md#linkTarget) setting specifies |
| 85 | the base URI for Go package links |
| 86 | |
| 87 | Caveats: |
| 88 | - It is an unfortunate limitation of the LSP that a `Hover` request |
| 89 | currently includes only a position but not a selection, as this |
| 90 | means it is impossible to request information about the type and |
| 91 | methods of, say, the `f(x)` portion of the larger expression |
| 92 | `f(x).y`. Please upvote microsoft/language-server-protocol#1466 if |
| 93 | you would like to see this addressed. |
| 94 | |
| 95 | Client support: |
| 96 | - **VS Code**: enabled by default. Displays rendered Markdown in a panel near the cursor. |
| 97 | - **Emacs + eglot**: enabled by default. Displays a one-line summary in the echo area. |
| 98 | - **Vim + coc.nvim**: ?? |
| 99 | - **CLI**: `gopls definition file.go:#start-#end` includes information from a Hover query. |
| 100 | |
| 101 | |
| 102 | ## Signature Help |
| 103 | |
| 104 | The LSP [`textDocument/signatureHelp`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_signatureHelp) |
| 105 | query returns information about the innermost function call enclosing |
| 106 | the cursor or selection, including the signature of the function and |
| 107 | the names, types, and documentation of each parameter. |
| 108 | |
| 109 | Clients may provide this information to help remind the user of the |
| 110 | purpose of each parameter and their order, while reading or editing a |
| 111 | function call. |
| 112 | |
| 113 | <img src='../assets/signature-help.png'> |
| 114 | |
| 115 | Call parens are not necessary if the cursor is within an identifier |
| 116 | that denotes a function or method. For example, Signature Help at |
| 117 | `once.Do(initialize‸)` will describe `initialize`, not `once.Do`. |
| 118 | |
| 119 | Client support: |
| 120 | - **VS Code**: enabled by default. |
| 121 | Also known as "[parameter hints](https://code.visualstudio.com/api/references/vscode-api#SignatureHelpProvider)" in the [IntelliSense settings](https://code.visualstudio.com/docs/editor/intellisense#_settings). |
| 122 | Displays signature and doc comment alongside Hover information. |
| 123 | - **Emacs + eglot**: enabled by default. Displays signature in the echo area. |
| 124 | - **Vim + coc.nvim**: ?? |
| 125 | - **CLI**: `gopls signature file.go:#start-#end` |
| 126 | |
| 127 | |
| 128 | ## Document Highlight |
| 129 | |
| 130 | The LSP [`textDocument/documentHighlight`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentHighlight) |
| 131 | query reports a set of source ranges that should be highlighted based |
| 132 | on the current cursor position or selection, to emphasize the |
| 133 | relationship between them. |
| 134 | |
| 135 | Each of the following parts of syntax forms a set so that if you |
| 136 | select any one member, gopls will highlight the complete set: |
| 137 | |
| 138 | - each identifier that refers to the same symbol (as in the screenshot below); |
| 139 | - a named result variable and all its corresponding operands of `return` statements; |
| 140 | - the `for`, `break`, and `continue` tokens of the same loop; |
| 141 | - the `switch` and `break` tokens of the same switch statement; |
| 142 | - the `func` keyword of a function and all of its `return` statements. |
| 143 | |
| 144 | More than one of these rules may be activated by a single selection, |
| 145 | for example, by an identifier that is also a return operand. |
| 146 | |
| 147 | Different occurrences of the same identifier may be color-coded to distinguish |
| 148 | "read" from "write" references to a given variable symbol. |
| 149 | |
| 150 | <img src='../assets/document-highlight.png'> |
| 151 | |
| 152 | Client support: |
| 153 | - **VS Code**: enabled by default. Triggered by cursor motion, or single click. |
| 154 | (Note: double clicking activates a simple syntax-oblivious textual match.) |
| 155 | - **Emacs + eglot**: enabled by default. Triggered by cursor motion or selection. |
| 156 | - **Vim + coc.nvim**: ?? |
| 157 | - **CLI**: `gopls signature file.go:#start-#end` |
| 158 | |
| 159 | |
| 160 | ## Inlay Hint |
| 161 | |
| 162 | The LSP [`textDocument/inlayHint`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint) |
| 163 | query returns a set of annotations to be spliced into the current file |
| 164 | that reveal implicit information. |
| 165 | |
| 166 | <img src='../assets/inlayhint-parameternames.png'> |
| 167 | |
| 168 | Examples: |
| 169 | |
| 170 | - In a function call `f(1, 2)`, hints provide the |
| 171 | names of the parameters (`parameterNames`), as in the screenshot above. |
| 172 | - In a call to a generic function, hints provide the type arguments |
| 173 | (`functionTypeParameters`). |
| 174 | - In an assignment `x, y = 1, 2`, hints provide the types of the |
| 175 | variables (`assignVariableTypes`). |
| 176 | - In a struct literal such as `Point2D{1, 2}`, hints provide the field |
| 177 | names (`compositeLiteralFields`). |
| 178 | - In a nested composite literal `T{{...}}`, a hint provides the type of |
| 179 | the inner literal, `{...}` (`compositeLiteralTypes`). |
| 180 | - In a `for k, v := range x {}` loop, hints provide the types of the |
| 181 | variables k and v (`rangeVariableTypes`). |
| 182 | - For a constant expression (perhaps using `iota`), a hint provides |
| 183 | its computed value (`constantValues`). |
| 184 | |
| 185 | See [Inlay hints](../inlayHints.md) for a complete list with examples. |
| 186 | |
| 187 | <!-- |
| 188 | TODO: Do we really need that separate doc? We could put all the |
| 189 | information here with much less fuss. It changes so rarely that a |
| 190 | culture of "update the tests and docs in every CL" should be sufficient. |
| 191 | IIUC, VS Code needs only the api-json representation. |
| 192 | --> |
| 193 | |
| 194 | Settings: |
| 195 | - The [`hints`](../settings.md#hints) setting indicates the desired set of hints. |
| 196 | To reduce distractions, its default value is empty. |
| 197 | To enable hints, add one or more of the identifiers above to the hints |
| 198 | map. For example: |
| 199 | ```json5 |
| 200 | "hints": {"parameterNames": true} |
| 201 | ``` |
| 202 | |
| 203 | Client support: |
| 204 | - **VS Code**: in addition to the `hints` configuration value, VS Code provides a graphical |
| 205 | configuration menu ("Preferences: Open Settings (UI)" the search for "Go Inlay Hints") |
| 206 | for each supported kind of inlay hint. |
| 207 | - **Emacs + eglot**: disabled by default. Needs `M-x eglot-inlay-hints-mode` plus the configuration [described here](https://www.reddit.com/r/emacs/comments/11bqzvk/emacs29_and_eglot_inlay_hints/) |
| 208 | - **Vim + coc.nvim**: ?? |
| 209 | - **CLI**: not supported |
| 210 | |
| 211 | ## Semantic Tokens |
| 212 | |
| 213 | The LSP [`textDocument/semanticTokens`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens) |
| 214 | query reports information about all the tokens in the current file, or |
| 215 | a portion of it. |
| 216 | The client may use this information to provide syntax highlighting |
| 217 | that conveys semantic distinctions between, for example, functions and |
| 218 | types, constants and variables, or library functions and built-ins. |
| 219 | |
| 220 | The client must specify the sets of types and modifiers it is interested in. |
| 221 | |
| 222 | Gopls reports the following token types: |
| 223 | |
| 224 | - `"comment"`: a comment |
| 225 | - `"function"`: a function |
| 226 | - `"keyword"`: a keyword |
| 227 | - `"label"`: a control label (not an LSP standard type) |
| 228 | - `"macro"`: text/template tokens |
| 229 | - `"method"`: a method |
| 230 | - `"namespace"`: an imported package name |
| 231 | - `"number"`: a numeric literal |
| 232 | - `"operator"`: an operator |
| 233 | - `"parameter"`: a parameter variable |
| 234 | - `"string"`: a string literal |
| 235 | - `"type"`: a type name (plus other uses) |
| 236 | - `"typeParameter"`: a type parameter |
| 237 | - `"variable"`: a var or const (see `readonly` modifier) |
| 238 | |
| 239 | Gopls also reports the following standard modifiers: |
| 240 | |
| 241 | - `"defaultLibrary"`: predeclared symbols |
| 242 | - `"definition"`: the declaring identifier of a symbol |
| 243 | - `"readonly"`: for constants |
| 244 | |
| 245 | plus these non-standard modifiers each representing the top-level |
| 246 | constructor of each symbols's type: |
| 247 | |
| 248 | - `"array"` |
| 249 | - `"bool"` |
| 250 | - `"chan"` |
| 251 | - `"interface"` |
| 252 | - `"map"` |
| 253 | - `"number"` |
| 254 | - `"pointer"` |
| 255 | - `"signature"` |
| 256 | - `"slice"` |
| 257 | - `"string"` |
| 258 | - `"struct"` |
| 259 | |
| 260 | Each identifier that refers to a symbol that shadows an existing symbol |
| 261 | from an outer scope is marked with a non-standard `"shadowing"` modifier. |
| 262 | This modifier allows editors to provide visual hints for shadowing declarations. |
| 263 | |
| 264 | Settings: |
| 265 | - The [`semanticTokens`](../settings.md#semanticTokens) setting determines whether |
| 266 | gopls responds to semantic token requests. This option allows users to disable |
| 267 | semantic tokens even when their client provides no client-side control over the |
| 268 | feature. Because gopls' semantic-tokens algorithm depends on type checking, |
| 269 | which adds a tangible latency, this feature is currently disabled by default |
| 270 | to avoid any delay in syntax highlighting; see https://go.dev/issue/#45313, https://go.dev/issue/#47465. |
| 271 | - The experimental [`noSemanticString`](../settings.md#noSemanticString) and |
| 272 | [`noSemanticNumber`](../settings.md#noSemanticNumber) settings cause the server |
| 273 | to exclude the `string` and `number` kinds from the response, as some clients |
| 274 | may do a more colorful job highlighting these tokens; see https://go.dev/issue/45753. |
| 275 | |
| 276 | Client Support: |
| 277 | - **VS Code**: See [Semantic Highlighting Guide](https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide). |
| 278 | - **Emacs + eglot**: Not supported; see joaotavora/eglot#615. |
| 279 | - **Vim + coc.nvim**: ?? |
| 280 | - **CLI**: `gopls semtok file.go` |
| 281 | |
| 282 | ## Folding Range |
| 283 | |
| 284 | The LSP [`textDocument/foldingRange`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_foldingRange) |
| 285 | query reports the list of regions in the current file that may be |
| 286 | independently collapsed or expanded. For example, it may be convenient |
| 287 | to collapse large comments or functions when studying some code so |
| 288 | that more of it fits in a single screen. |
| 289 | |
| 290 | <img title="A function FoldingRange, collapsed, in VS Code" src="../assets/foldingrange.png" width="640"> |
| 291 | |
| 292 | The protocol [allows](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#foldingRangeClientCapabilities) clients to indicate whether they prefer |
| 293 | fine-grained ranges such as matched pairs of brackets, or only ranges |
| 294 | consisting of complete lines. |
| 295 | |
| 296 | Client support: |
| 297 | - **VS Code**: displayed in left margin. Toggle the chevrons (`∨` and `>`) to collapse or expand. |
| 298 | - **Emacs + eglot**: not supported. |
| 299 | - **Vim + coc.nvim**: ?? |
| 300 | - **CLI**: `gopls folding_ranges file.go` |
| 301 | |
| 302 | ## Document Link |
| 303 | |
| 304 | The LSP [`textDocument/documentLink`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentLink) |
| 305 | query uses heuristics to extracts URLs from doc comments and string |
| 306 | literals in the current file so that the client can present them as |
| 307 | clickable links. |
| 308 | |
| 309 | <img src='../assets/documentlink.png'> |
| 310 | |
| 311 | In addition to explicit URLs, gopls also turns string literals in |
| 312 | import declarations into links to the pkg.go.dev documentation for the |
| 313 | imported package. |
| 314 | |
| 315 | Settings: |
| 316 | - The [`importShortcut`](../settings.md#importShortcut) setting determines |
| 317 | what kind of link is returned for an `import` declaration. |
| 318 | - The [`linkTarget`](../settings.md#linkTarget) setting specifies |
| 319 | the base URI for Go package links. |
| 320 | |
| 321 | Client support: |
| 322 | - **VS Code**: Hovering over a link displays a "Follow link (cmd+click)" popup. |
| 323 | - **Emacs + eglot**: not currently used. |
| 324 | - **Vim + coc.nvim**: ?? |
| 325 | - **CLI**: `gopls links file.go` |