| 1 | --- |
| 2 | title: "Gopls: Code lenses" |
| 3 | --- |
| 4 | |
| 5 | A "code lens" is a command associated with a range of a source file. |
| 6 | The VS Code manual describes code lenses as |
| 7 | "[actionable, contextual information, interspersed in your source |
| 8 | code](https://code.visualstudio.com/blogs/2017/02/12/code-lens-roundup)". |
| 9 | The LSP [`textDocument/codeLens`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_codeLens) operation requests the |
| 10 | current set of code lenses for a file. |
| 11 | |
| 12 | Gopls generates code lenses from a number of sources. |
| 13 | This document describes them. |
| 14 | |
| 15 | They can be enabled and disabled using the |
| 16 | [`codelenses`](settings.md#codelenses) setting. |
| 17 | Their features are subject to change. |
| 18 | |
| 19 | Client support: |
| 20 | - **VS Code**: Code Lenses appear as small text links above a line of source code. |
| 21 | - **Emacs + eglot**: Not supported, but prototype exists at https://github.com/joaotavora/eglot/pull/71. |
| 22 | - **Vim + coc.nvim**: ?? |
| 23 | - **CLI**: `gopls codelens`. For example, `gopls codelens -exec file.go:123 "run test"` runs the test at the specified line. |
| 24 | |
| 25 | |
| 26 | <!-- This portion is generated by doc/generate from the ../internal/settings package. --> |
| 27 | <!-- BEGIN Lenses: DO NOT MANUALLY EDIT THIS SECTION --> |
| 28 | ## `generate`: Run `go generate` |
| 29 | |
| 30 | |
| 31 | This codelens source annotates any `//go:generate` comments |
| 32 | with commands to run `go generate` in this directory, on |
| 33 | all directories recursively beneath this one. |
| 34 | |
| 35 | See [Generating code](https://go.dev/blog/generate) for |
| 36 | more details. |
| 37 | |
| 38 | |
| 39 | Default: on |
| 40 | |
| 41 | File type: Go |
| 42 | |
| 43 | ## `regenerate_cgo`: Re-generate cgo declarations |
| 44 | |
| 45 | |
| 46 | This codelens source annotates an `import "C"` declaration |
| 47 | with a command to re-run the [cgo |
| 48 | command](https://pkg.go.dev/cmd/cgo) to regenerate the |
| 49 | corresponding Go declarations. |
| 50 | |
| 51 | Use this after editing the C code in comments attached to |
| 52 | the import, or in C header files included by it. |
| 53 | |
| 54 | |
| 55 | Default: on |
| 56 | |
| 57 | File type: Go |
| 58 | |
| 59 | ## `test`: Run tests and benchmarks |
| 60 | |
| 61 | |
| 62 | This codelens source annotates each `Test` and `Benchmark` |
| 63 | function in a `*_test.go` file with a command to run it. |
| 64 | |
| 65 | This source is off by default because VS Code has |
| 66 | a client-side custom UI for testing, and because progress |
| 67 | notifications are not a great UX for streamed test output. |
| 68 | See: |
| 69 | - golang/go#67400 for a discussion of this feature. |
| 70 | - https://github.com/joaotavora/eglot/discussions/1402 |
| 71 | for an alternative approach. |
| 72 | |
| 73 | |
| 74 | Default: off |
| 75 | |
| 76 | File type: Go |
| 77 | |
| 78 | ## `run_govulncheck`: Run govulncheck (legacy) |
| 79 | |
| 80 | |
| 81 | This codelens source annotates the `module` directive in a go.mod file |
| 82 | with a command to run Govulncheck asynchronously. |
| 83 | |
| 84 | [Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that |
| 85 | computes the set of functions reachable within your application, including |
| 86 | dependencies; queries a database of known security vulnerabilities; and |
| 87 | reports any potential problems it finds. |
| 88 | |
| 89 | |
| 90 | Default: on |
| 91 | |
| 92 | File type: go.mod |
| 93 | |
| 94 | ## `tidy`: Tidy go.mod file |
| 95 | |
| 96 | |
| 97 | This codelens source annotates the `module` directive in a |
| 98 | go.mod file with a command to run [`go mod |
| 99 | tidy`](https://go.dev/ref/mod#go-mod-tidy), which ensures |
| 100 | that the go.mod file matches the source code in the module. |
| 101 | |
| 102 | |
| 103 | Default: on |
| 104 | |
| 105 | File type: go.mod |
| 106 | |
| 107 | ## `upgrade_dependency`: Update dependencies |
| 108 | |
| 109 | |
| 110 | This codelens source annotates the `module` directive in a |
| 111 | go.mod file with commands to: |
| 112 | |
| 113 | - check for available upgrades, |
| 114 | - upgrade direct dependencies, and |
| 115 | - upgrade all dependencies transitively. |
| 116 | |
| 117 | |
| 118 | Default: on |
| 119 | |
| 120 | File type: go.mod |
| 121 | |
| 122 | ## `vendor`: Update vendor directory |
| 123 | |
| 124 | |
| 125 | This codelens source annotates the `module` directive in a |
| 126 | go.mod file with a command to run [`go mod |
| 127 | vendor`](https://go.dev/ref/mod#go-mod-vendor), which |
| 128 | creates or updates the directory named `vendor` in the |
| 129 | module root so that it contains an up-to-date copy of all |
| 130 | necessary package dependencies. |
| 131 | |
| 132 | |
| 133 | Default: on |
| 134 | |
| 135 | File type: go.mod |
| 136 | |
| 137 | ## `vulncheck`: Run govulncheck |
| 138 | |
| 139 | **This setting is experimental and may be deleted.** |
| 140 | |
| 141 | |
| 142 | This codelens source annotates the `module` directive in a go.mod file |
| 143 | with a command to run govulncheck synchronously. |
| 144 | |
| 145 | [Govulncheck](https://go.dev/blog/vuln) is a static analysis tool that |
| 146 | computes the set of functions reachable within your application, including |
| 147 | dependencies; queries a database of known security vulnerabilities; and |
| 148 | reports any potential problems it finds. |
| 149 | |
| 150 | |
| 151 | Default: off |
| 152 | |
| 153 | File type: go.mod |
| 154 | |
| 155 | <!-- END Lenses: DO NOT MANUALLY EDIT THIS SECTION --> |