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.
gc_details
: Toggle display of Go compiler optimization decisionsThis codelens source causes the package
declaration of each file to be annotated with a command to toggle the state of the per-session variable that controls whether optimization decisions from the Go compiler (formerly known as “gc”) should be displayed as diagnostics.
Optimization decisions include:
TODO(adonovan): this source is off by default because the annotation is annoying and because VS Code has a separate “Toggle gc details” command. Replace it with a Code Action (“Source action...”).
Default: off
File type: Go
generate
: Run go generate
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
regenerate_cgo
: Re-generate cgo declarationsThis 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
test
: Run tests and benchmarksThis 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:
Default: off
File type: Go
run_govulncheck
: Run govulncheckThis codelens source annotates the module
directive in a go.mod file with a command to run Govulncheck.
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
tidy
: Tidy go.mod fileThis 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
upgrade_dependency
: Update dependenciesThis codelens source annotates the module
directive in a go.mod file with commands to:
Default: on
File type: go.mod
vendor
: Update vendor directoryThis 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