Gopls: Passive features

This page documents the fundamental LSP features of gopls that may be described as “passive”, since many editors use them to continuously provide information about your source files without requiring any special action.

See also Code Lenses, some of which annotate your source code with additional information and may thus also be considered passive features.

Hover

The LSP textDocument/hover query returns a description of the code currently under the cursor, such as its name, kind, type, value (for a constant), abbreviated declaration (for a type), doc comment (if any), and a link to the symbol’s documentation on pkg.go.dev. The client may request either plain text or Markdown.

Depending on the selection, the response may include additional information. For example, hovering over a type shows its declared methods, plus any methods promoted from embedded fields.

Doc links: A doc comment may refer to another symbol using square brackets, for example [fmt.Printf]. Hovering over one of these doc links reveals information about the referenced symbol.

Struct size/offset info: for declarations of struct types, hovering over the name reveals the struct’s size in bytes:

And hovering over each field name shows the size and offset of that field:

This information may be useful when optimizing the layout of your data structures, or when reading assembly files or stack traces that refer to each field by its cryptic byte offset.

In addition, Hover reports:

In the struct above, alignment rules require each of the two boolean fields (1 byte) to occupy a complete word (8 bytes), leading to (7 + 7) / (3 * 8) = 58% waste. Placing the two booleans together would save a word. (In most structures clarity is more important than compactness, so you should reorder fields to save space only in data structures that have been shown by profiling to be very frequently allocated.)

Embed directives: hovering over the file name pattern in //go:embed directive, for example *.html, reveals the list of file names to which the wildcard expands.

Linkname directives: a //go:linkname directive creates a linker-level alias for another symbol. Hovering over the directive shows information about the other symbol.

The hover information for symbols from the standard library added after Go 1.0 states the Go release that added the symbol.

Settings:

Caveats:

Client support:

Signature Help

The LSP textDocument/signatureHelp query returns information about the innermost function call enclosing the cursor or selection, including the signature of the function and the names, types, and documentation of each parameter.

Clients may provide this information to help remind the user of the purpose of each parameter and their order, while reading or editing a function call.

Call parens are not necessary if the cursor is within an identifier that denotes a function or method. For example, Signature Help at once.Do(initialize‸) will describe initialize, not once.Do.

Client support:

Document Highlight

The LSP textDocument/documentHighlight query reports a set of source ranges that should be highlighted based on the current cursor position or selection, to emphasize the relationship between them.

Each of the following parts of syntax forms a set so that if you select any one member, gopls will highlight the complete set:

More than one of these rules may be activated by a single selection, for example, by an identifier that is also a return operand.

Different occurrences of the same identifier may be color-coded to distinguish “read” from “write” references to a given variable symbol.

Client support:

Inlay Hint

The LSP textDocument/inlayHint query returns a set of annotations to be spliced into the current file that reveal implicit information.

Examples:

See Inlay hints for a complete list with examples.

Settings:

Client support:

Semantic Tokens

The LSP textDocument/semanticTokens query reports information about all the tokens in the current file, or a portion of it. The client may use this information to provide syntax highlighting that conveys semantic distinctions between, for example, functions and types, constants and variables, or library functions and built-ins.

The client must specify the sets of types and modifiers it is interested in.

Gopls reports the following token types:

Gopls also reports the following standard modifiers:

plus these non-standard modifiers each representing the top-level constructor of each symbols’s type:

Settings:

Client Support:

Folding Range

The LSP textDocument/foldingRange query reports the list of regions in the current file that may be independently collapsed or expanded. For example, it may be convenient to collapse large comments or functions when studying some code so that more of it fits in a single screen.

The protocol allows clients to indicate whether they prefer fine-grained ranges such as matched pairs of brackets, or only ranges consisting of complete lines.

Client support:

The LSP textDocument/documentLink query uses heuristics to extracts URLs from doc comments and string literals in the current file so that the client can present them as clickable links.

In addition to explicit URLs, gopls also turns string literals in import declarations into links to the pkg.go.dev documentation for the imported package.

Settings:

Client support:


The source files for this documentation can be found beneath golang.org/x/tools/gopls/doc.