A "code lens" is a command associated with a range of a source file.
The VS Code manual describes code lenses as
"actionable, contextual information, interspersed in your source
code".
The LSP textDocument/codeLens
operation requests the
current set of code lenses for a file.
Gopls generates code lenses from a number of sources. This document describes them.
They can be enabled and disabled using the
codelenses
setting.
Their features are subject to change.
Client support:
- VS Code: Code Lenses appear as small text links above a line of source code.
- Emacs + eglot: Not supported, but prototype exists at joaotavora/eglot#71.
- Vim + coc.nvim: ??
- CLI:
gopls codelens
. For example,gopls codelens -exec file.go:123 "run test"
runs the test at the specified line.
This codelens source annotates any //go:generate
comments
with commands to run go generate
in this directory, on
all directories recursively beneath this one.
See Generating code for more details.
Default: on
File type: Go
This codelens source annotates an import "C"
declaration
with a command to re-run the cgo
command to regenerate the
corresponding Go declarations.
Use this after editing the C code in comments attached to the import, or in C header files included by it.
Default: on
File type: Go
This codelens source annotates each Test
and Benchmark
function in a *_test.go
file with a command to run it.
This source is off by default because VS Code has a client-side custom UI for testing, and because progress notifications are not a great UX for streamed test output. See:
- golang/go#67400 for a discussion of this feature.
- joaotavora/eglot#1402 for an alternative approach.
Default: off
File type: Go
This codelens source annotates the module
directive in a go.mod file
with a command to run Govulncheck asynchronously.
Govulncheck is a static analysis tool that computes the set of functions reachable within your application, including dependencies; queries a database of known security vulnerabilities; and reports any potential problems it finds.
Default: off
File type: go.mod
This codelens source annotates the module
directive in a
go.mod file with a command to run go mod tidy
, which ensures
that the go.mod file matches the source code in the module.
Default: on
File type: go.mod
This codelens source annotates the module
directive in a
go.mod file with commands to:
- check for available upgrades,
- upgrade direct dependencies, and
- upgrade all dependencies transitively.
Default: on
File type: go.mod
This codelens source annotates the module
directive in a
go.mod file with a command to run go mod vendor
, which
creates or updates the directory named vendor
in the
module root so that it contains an up-to-date copy of all
necessary package dependencies.
Default: on
File type: go.mod
This codelens source annotates the module
directive in a go.mod file
with a command to run govulncheck synchronously.
Govulncheck is a static analysis tool that computes the set of functions reachable within your application, including dependencies; queries a database of known security vulnerabilities; and reports any potential problems it finds.
Default: off
File type: go.mod