1# Commands 2 3This document describes the LSP-level commands supported by `gopls`. They cannot be invoked directly by users, and all the details are subject to change, so nobody should rely on this information. 4 5<!-- BEGIN Commands: DO NOT MANUALLY EDIT THIS SECTION --> 6### **Add a dependency** 7Identifier: `gopls.add_dependency` 8 9Adds a dependency to the go.mod file for a module. 10 11Args: 12 13``` 14{ 15 // The go.mod file URI. 16 "URI": string, 17 // Additional args to pass to the go command. 18 "GoCmdArgs": []string, 19 // Whether to add a require directive. 20 "AddRequire": bool, 21} 22``` 23 24### **Add an import** 25Identifier: `gopls.add_import` 26 27Ask the server to add an import path to a given Go file. The method will 28call applyEdit on the client so that clients don't have to apply the edit 29themselves. 30 31Args: 32 33``` 34{ 35 // ImportPath is the target import path that should 36 // be added to the URI file 37 "ImportPath": string, 38 // URI is the file that the ImportPath should be 39 // added to 40 "URI": string, 41} 42``` 43 44### **Apply a fix** 45Identifier: `gopls.apply_fix` 46 47Applies a fix to a region of source code. 48 49Args: 50 51``` 52{ 53 // The fix to apply. 54 "Fix": string, 55 // The file URI for the document to fix. 56 "URI": string, 57 // The document range to scan for fixes. 58 "Range": { 59 "start": { 60 "line": uint32, 61 "character": uint32, 62 }, 63 "end": { 64 "line": uint32, 65 "character": uint32, 66 }, 67 }, 68} 69``` 70 71### **Check for upgrades** 72Identifier: `gopls.check_upgrades` 73 74Checks for module upgrades. 75 76Args: 77 78``` 79{ 80 // The go.mod file URI. 81 "URI": string, 82 // The modules to check. 83 "Modules": []string, 84} 85``` 86 87### **Toggle gc_details** 88Identifier: `gopls.gc_details` 89 90Toggle the calculation of gc annotations. 91 92Args: 93 94``` 95string 96``` 97 98### **Run go generate** 99Identifier: `gopls.generate` 100 101Runs `go generate` for a given directory. 102 103Args: 104 105``` 106{ 107 // URI for the directory to generate. 108 "Dir": string, 109 // Whether to generate recursively (go generate ./...) 110 "Recursive": bool, 111} 112``` 113 114### **Generate gopls.mod** 115Identifier: `gopls.generate_gopls_mod` 116 117(Re)generate the gopls.mod file for a workspace. 118 119Args: 120 121``` 122{ 123 // The file URI. 124 "URI": string, 125} 126``` 127 128### **go get a package** 129Identifier: `gopls.go_get_package` 130 131Runs `go get` to fetch a package. 132 133Args: 134 135``` 136{ 137 // Any document URI within the relevant module. 138 "URI": string, 139 // The package to go get. 140 "Pkg": string, 141 "AddRequire": bool, 142} 143``` 144 145### **List known packages** 146Identifier: `gopls.list_known_packages` 147 148Retrieve a list of packages that are importable from the given URI. 149 150Args: 151 152``` 153{ 154 // The file URI. 155 "URI": string, 156} 157``` 158 159Result: 160 161``` 162{ 163 // Packages is a list of packages relative 164 // to the URIArg passed by the command request. 165 // In other words, it omits paths that are already 166 // imported or cannot be imported due to compiler 167 // restrictions. 168 "Packages": []string, 169} 170``` 171 172### **Regenerate cgo** 173Identifier: `gopls.regenerate_cgo` 174 175Regenerates cgo definitions. 176 177Args: 178 179``` 180{ 181 // The file URI. 182 "URI": string, 183} 184``` 185 186### **Remove a dependency** 187Identifier: `gopls.remove_dependency` 188 189Removes a dependency from the go.mod file of a module. 190 191Args: 192 193``` 194{ 195 // The go.mod file URI. 196 "URI": string, 197 // The module path to remove. 198 "ModulePath": string, 199 "OnlyDiagnostic": bool, 200} 201``` 202 203### **Run test(s)** 204Identifier: `gopls.run_tests` 205 206Runs `go test` for a specific set of test or benchmark functions. 207 208Args: 209 210``` 211{ 212 // The test file containing the tests to run. 213 "URI": string, 214 // Specific test names to run, e.g. TestFoo. 215 "Tests": []string, 216 // Specific benchmarks to run, e.g. BenchmarkFoo. 217 "Benchmarks": []string, 218} 219``` 220 221### **Start the gopls debug server** 222Identifier: `gopls.start_debugging` 223 224Start the gopls debug server if it isn't running, and return the debug 225address. 226 227Args: 228 229``` 230{ 231 // Optional: the address (including port) for the debug server to listen on. 232 // If not provided, the debug server will bind to "localhost:0", and the 233 // full debug URL will be contained in the result. 234 // 235 // If there is more than one gopls instance along the serving path (i.e. you 236 // are using a daemon), each gopls instance will attempt to start debugging. 237 // If Addr specifies a port, only the daemon will be able to bind to that 238 // port, and each intermediate gopls instance will fail to start debugging. 239 // For this reason it is recommended not to specify a port (or equivalently, 240 // to specify ":0"). 241 // 242 // If the server was already debugging this field has no effect, and the 243 // result will contain the previously configured debug URL(s). 244 "Addr": string, 245} 246``` 247 248Result: 249 250``` 251{ 252 // The URLs to use to access the debug servers, for all gopls instances in 253 // the serving path. For the common case of a single gopls instance (i.e. no 254 // daemon), this will be exactly one address. 255 // 256 // In the case of one or more gopls instances forwarding the LSP to a daemon, 257 // URLs will contain debug addresses for each server in the serving path, in 258 // serving order. The daemon debug address will be the last entry in the 259 // slice. If any intermediate gopls instance fails to start debugging, no 260 // error will be returned but the debug URL for that server in the URLs slice 261 // will be empty. 262 "URLs": []string, 263} 264``` 265 266### **Run test(s) (legacy)** 267Identifier: `gopls.test` 268 269Runs `go test` for a specific set of test or benchmark functions. 270 271Args: 272 273``` 274string, 275[]string, 276[]string 277``` 278 279### **Run go mod tidy** 280Identifier: `gopls.tidy` 281 282Runs `go mod tidy` for a module. 283 284Args: 285 286``` 287{ 288 // The file URIs. 289 "URIs": []string, 290} 291``` 292 293### **Toggle gc_details** 294Identifier: `gopls.toggle_gc_details` 295 296Toggle the calculation of gc annotations. 297 298Args: 299 300``` 301{ 302 // The file URI. 303 "URI": string, 304} 305``` 306 307### **Update go.sum** 308Identifier: `gopls.update_go_sum` 309 310Updates the go.sum file for a module. 311 312Args: 313 314``` 315{ 316 // The file URIs. 317 "URIs": []string, 318} 319``` 320 321### **Upgrade a dependency** 322Identifier: `gopls.upgrade_dependency` 323 324Upgrades a dependency in the go.mod file for a module. 325 326Args: 327 328``` 329{ 330 // The go.mod file URI. 331 "URI": string, 332 // Additional args to pass to the go command. 333 "GoCmdArgs": []string, 334 // Whether to add a require directive. 335 "AddRequire": bool, 336} 337``` 338 339### **Run go mod vendor** 340Identifier: `gopls.vendor` 341 342Runs `go mod vendor` for a module. 343 344Args: 345 346``` 347{ 348 // The file URI. 349 "URI": string, 350} 351``` 352 353### **Query workspace metadata** 354Identifier: `gopls.workspace_metadata` 355 356Query the server for information about active workspaces. 357 358Result: 359 360``` 361{ 362 // All workspaces for this session. 363 "Workspaces": []{ 364 "Name": string, 365 "ModuleDir": string, 366 }, 367} 368``` 369 370<!-- END Commands: DO NOT MANUALLY EDIT THIS SECTION --> 371