golang.org/x/tools/gopls@v0.15.3/doc/commands.md (about) 1 # Commands 2 3 This 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** 7 Identifier: `gopls.add_dependency` 8 9 Adds a dependency to the go.mod file for a module. 10 11 Args: 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** 25 Identifier: `gopls.add_import` 26 27 Ask the server to add an import path to a given Go file. The method will 28 call applyEdit on the client so that clients don't have to apply the edit 29 themselves. 30 31 Args: 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 ### **Update the given telemetry counters** 45 Identifier: `gopls.add_telemetry_counters` 46 47 Gopls will prepend "fwd/" to all the counters updated using this command 48 to avoid conflicts with other counters gopls collects. 49 50 Args: 51 52 ``` 53 { 54 // Names and Values must have the same length. 55 "Names": []string, 56 "Values": []int64, 57 } 58 ``` 59 60 ### **Apply a fix** 61 Identifier: `gopls.apply_fix` 62 63 Applies a fix to a region of source code. 64 65 Args: 66 67 ``` 68 { 69 // The name of the fix to apply. 70 // 71 // For fixes suggested by analyzers, this is a string constant 72 // advertised by the analyzer that matches the Category of 73 // the analysis.Diagnostic with a SuggestedFix containing no edits. 74 // 75 // For fixes suggested by code actions, this is a string agreed 76 // upon by the code action and golang.ApplyFix. 77 "Fix": string, 78 // The file URI for the document to fix. 79 "URI": string, 80 // The document range to scan for fixes. 81 "Range": { 82 "start": { 83 "line": uint32, 84 "character": uint32, 85 }, 86 "end": { 87 "line": uint32, 88 "character": uint32, 89 }, 90 }, 91 // Whether to resolve and return the edits. 92 "ResolveEdits": bool, 93 } 94 ``` 95 96 Result: 97 98 ``` 99 { 100 // Holds changes to existing resources. 101 "changes": map[golang.org/x/tools/gopls/internal/protocol.DocumentURI][]golang.org/x/tools/gopls/internal/protocol.TextEdit, 102 // Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes 103 // are either an array of `TextDocumentEdit`s to express changes to n different text documents 104 // where each text document edit addresses a specific version of a text document. Or it can contain 105 // above `TextDocumentEdit`s mixed with create, rename and delete file / folder operations. 106 // 107 // Whether a client supports versioned document edits is expressed via 108 // `workspace.workspaceEdit.documentChanges` client capability. 109 // 110 // If a client neither supports `documentChanges` nor `workspace.workspaceEdit.resourceOperations` then 111 // only plain `TextEdit`s using the `changes` property are supported. 112 "documentChanges": []{ 113 "TextDocumentEdit": { 114 "textDocument": { ... }, 115 "edits": { ... }, 116 }, 117 "RenameFile": { 118 "kind": string, 119 "oldUri": string, 120 "newUri": string, 121 "options": { ... }, 122 "ResourceOperation": { ... }, 123 }, 124 }, 125 // A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and 126 // delete file / folder operations. 127 // 128 // Whether clients honor this property depends on the client capability `workspace.changeAnnotationSupport`. 129 // 130 // @since 3.16.0 131 "changeAnnotations": map[string]golang.org/x/tools/gopls/internal/protocol.ChangeAnnotation, 132 } 133 ``` 134 135 ### **Perform a "change signature" refactoring** 136 Identifier: `gopls.change_signature` 137 138 This command is experimental, currently only supporting parameter removal. 139 Its signature will certainly change in the future (pun intended). 140 141 Args: 142 143 ``` 144 { 145 "RemoveParameter": { 146 "uri": string, 147 "range": { 148 "start": { ... }, 149 "end": { ... }, 150 }, 151 }, 152 // Whether to resolve and return the edits. 153 "ResolveEdits": bool, 154 } 155 ``` 156 157 Result: 158 159 ``` 160 { 161 // Holds changes to existing resources. 162 "changes": map[golang.org/x/tools/gopls/internal/protocol.DocumentURI][]golang.org/x/tools/gopls/internal/protocol.TextEdit, 163 // Depending on the client capability `workspace.workspaceEdit.resourceOperations` document changes 164 // are either an array of `TextDocumentEdit`s to express changes to n different text documents 165 // where each text document edit addresses a specific version of a text document. Or it can contain 166 // above `TextDocumentEdit`s mixed with create, rename and delete file / folder operations. 167 // 168 // Whether a client supports versioned document edits is expressed via 169 // `workspace.workspaceEdit.documentChanges` client capability. 170 // 171 // If a client neither supports `documentChanges` nor `workspace.workspaceEdit.resourceOperations` then 172 // only plain `TextEdit`s using the `changes` property are supported. 173 "documentChanges": []{ 174 "TextDocumentEdit": { 175 "textDocument": { ... }, 176 "edits": { ... }, 177 }, 178 "RenameFile": { 179 "kind": string, 180 "oldUri": string, 181 "newUri": string, 182 "options": { ... }, 183 "ResourceOperation": { ... }, 184 }, 185 }, 186 // A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and 187 // delete file / folder operations. 188 // 189 // Whether clients honor this property depends on the client capability `workspace.changeAnnotationSupport`. 190 // 191 // @since 3.16.0 192 "changeAnnotations": map[string]golang.org/x/tools/gopls/internal/protocol.ChangeAnnotation, 193 } 194 ``` 195 196 ### **Check for upgrades** 197 Identifier: `gopls.check_upgrades` 198 199 Checks for module upgrades. 200 201 Args: 202 203 ``` 204 { 205 // The go.mod file URI. 206 "URI": string, 207 // The modules to check. 208 "Modules": []string, 209 } 210 ``` 211 212 ### **Cause server to publish diagnostics for the specified files.** 213 Identifier: `gopls.diagnose_files` 214 215 This command is needed by the 'gopls {check,fix}' CLI subcommands. 216 217 Args: 218 219 ``` 220 { 221 "Files": []string, 222 } 223 ``` 224 225 ### **Run go mod edit -go=version** 226 Identifier: `gopls.edit_go_directive` 227 228 Runs `go mod edit -go=version` for a module. 229 230 Args: 231 232 ``` 233 { 234 // Any document URI within the relevant module. 235 "URI": string, 236 // The version to pass to `go mod edit -go`. 237 "Version": string, 238 } 239 ``` 240 241 ### **Get known vulncheck result** 242 Identifier: `gopls.fetch_vulncheck_result` 243 244 Fetch the result of latest vulnerability check (`govulncheck`). 245 246 Args: 247 248 ``` 249 { 250 // The file URI. 251 "URI": string, 252 } 253 ``` 254 255 Result: 256 257 ``` 258 map[golang.org/x/tools/gopls/internal/protocol.DocumentURI]*golang.org/x/tools/gopls/internal/vulncheck.Result 259 ``` 260 261 ### **Toggle gc_details** 262 Identifier: `gopls.gc_details` 263 264 Toggle the calculation of gc annotations. 265 266 Args: 267 268 ``` 269 string 270 ``` 271 272 ### **Run go generate** 273 Identifier: `gopls.generate` 274 275 Runs `go generate` for a given directory. 276 277 Args: 278 279 ``` 280 { 281 // URI for the directory to generate. 282 "Dir": string, 283 // Whether to generate recursively (go generate ./...) 284 "Recursive": bool, 285 } 286 ``` 287 288 ### **'go get' a package** 289 Identifier: `gopls.go_get_package` 290 291 Runs `go get` to fetch a package. 292 293 Args: 294 295 ``` 296 { 297 // Any document URI within the relevant module. 298 "URI": string, 299 // The package to go get. 300 "Pkg": string, 301 "AddRequire": bool, 302 } 303 ``` 304 305 ### **List imports of a file and its package** 306 Identifier: `gopls.list_imports` 307 308 Retrieve a list of imports in the given Go file, and the package it 309 belongs to. 310 311 Args: 312 313 ``` 314 { 315 // The file URI. 316 "URI": string, 317 } 318 ``` 319 320 Result: 321 322 ``` 323 { 324 // Imports is a list of imports in the requested file. 325 "Imports": []{ 326 "Path": string, 327 "Name": string, 328 }, 329 // PackageImports is a list of all imports in the requested file's package. 330 "PackageImports": []{ 331 "Path": string, 332 }, 333 } 334 ``` 335 336 ### **List known packages** 337 Identifier: `gopls.list_known_packages` 338 339 Retrieve a list of packages that are importable from the given URI. 340 341 Args: 342 343 ``` 344 { 345 // The file URI. 346 "URI": string, 347 } 348 ``` 349 350 Result: 351 352 ``` 353 { 354 // Packages is a list of packages relative 355 // to the URIArg passed by the command request. 356 // In other words, it omits paths that are already 357 // imported or cannot be imported due to compiler 358 // restrictions. 359 "Packages": []string, 360 } 361 ``` 362 363 ### **Prompt user to enable telemetry** 364 Identifier: `gopls.maybe_prompt_for_telemetry` 365 366 Checks for the right conditions, and then prompts the user 367 to ask if they want to enable Go telemetry uploading. If 368 the user responds 'Yes', the telemetry mode is set to "on". 369 370 ### **Fetch memory statistics** 371 Identifier: `gopls.mem_stats` 372 373 Call runtime.GC multiple times and return memory statistics as reported by 374 runtime.MemStats. 375 376 This command is used for benchmarking, and may change in the future. 377 378 Result: 379 380 ``` 381 { 382 "HeapAlloc": uint64, 383 "HeapInUse": uint64, 384 "TotalAlloc": uint64, 385 } 386 ``` 387 388 ### **Regenerate cgo** 389 Identifier: `gopls.regenerate_cgo` 390 391 Regenerates cgo definitions. 392 393 Args: 394 395 ``` 396 { 397 // The file URI. 398 "URI": string, 399 } 400 ``` 401 402 ### **Remove a dependency** 403 Identifier: `gopls.remove_dependency` 404 405 Removes a dependency from the go.mod file of a module. 406 407 Args: 408 409 ``` 410 { 411 // The go.mod file URI. 412 "URI": string, 413 // The module path to remove. 414 "ModulePath": string, 415 // If the module is tidied apart from the one unused diagnostic, we can 416 // run `go get module@none`, and then run `go mod tidy`. Otherwise, we 417 // must make textual edits. 418 "OnlyDiagnostic": bool, 419 } 420 ``` 421 422 ### **Reset go.mod diagnostics** 423 Identifier: `gopls.reset_go_mod_diagnostics` 424 425 Reset diagnostics in the go.mod file of a module. 426 427 Args: 428 429 ``` 430 { 431 "URIArg": { 432 "URI": string, 433 }, 434 // Optional: source of the diagnostics to reset. 435 // If not set, all resettable go.mod diagnostics will be cleared. 436 "DiagnosticSource": string, 437 } 438 ``` 439 440 ### **Run `go work [args...]`, and apply the resulting go.work** 441 Identifier: `gopls.run_go_work_command` 442 443 edits to the current go.work file 444 445 Args: 446 447 ``` 448 { 449 "ViewID": string, 450 "InitFirst": bool, 451 "Args": []string, 452 } 453 ``` 454 455 ### **Run vulncheck** 456 Identifier: `gopls.run_govulncheck` 457 458 Run vulnerability check (`govulncheck`). 459 460 Args: 461 462 ``` 463 { 464 // Any document in the directory from which govulncheck will run. 465 "URI": string, 466 // Package pattern. E.g. "", ".", "./...". 467 "Pattern": string, 468 } 469 ``` 470 471 Result: 472 473 ``` 474 { 475 // Token holds the progress token for LSP workDone reporting of the vulncheck 476 // invocation. 477 "Token": interface{}, 478 } 479 ``` 480 481 ### **Run test(s)** 482 Identifier: `gopls.run_tests` 483 484 Runs `go test` for a specific set of test or benchmark functions. 485 486 Args: 487 488 ``` 489 { 490 // The test file containing the tests to run. 491 "URI": string, 492 // Specific test names to run, e.g. TestFoo. 493 "Tests": []string, 494 // Specific benchmarks to run, e.g. BenchmarkFoo. 495 "Benchmarks": []string, 496 } 497 ``` 498 499 ### **Start the gopls debug server** 500 Identifier: `gopls.start_debugging` 501 502 Start the gopls debug server if it isn't running, and return the debug 503 address. 504 505 Args: 506 507 ``` 508 { 509 // Optional: the address (including port) for the debug server to listen on. 510 // If not provided, the debug server will bind to "localhost:0", and the 511 // full debug URL will be contained in the result. 512 // 513 // If there is more than one gopls instance along the serving path (i.e. you 514 // are using a daemon), each gopls instance will attempt to start debugging. 515 // If Addr specifies a port, only the daemon will be able to bind to that 516 // port, and each intermediate gopls instance will fail to start debugging. 517 // For this reason it is recommended not to specify a port (or equivalently, 518 // to specify ":0"). 519 // 520 // If the server was already debugging this field has no effect, and the 521 // result will contain the previously configured debug URL(s). 522 "Addr": string, 523 } 524 ``` 525 526 Result: 527 528 ``` 529 { 530 // The URLs to use to access the debug servers, for all gopls instances in 531 // the serving path. For the common case of a single gopls instance (i.e. no 532 // daemon), this will be exactly one address. 533 // 534 // In the case of one or more gopls instances forwarding the LSP to a daemon, 535 // URLs will contain debug addresses for each server in the serving path, in 536 // serving order. The daemon debug address will be the last entry in the 537 // slice. If any intermediate gopls instance fails to start debugging, no 538 // error will be returned but the debug URL for that server in the URLs slice 539 // will be empty. 540 "URLs": []string, 541 } 542 ``` 543 544 ### **Start capturing a profile of gopls' execution** 545 Identifier: `gopls.start_profile` 546 547 Start a new pprof profile. Before using the resulting file, profiling must 548 be stopped with a corresponding call to StopProfile. 549 550 This command is intended for internal use only, by the gopls benchmark 551 runner. 552 553 Args: 554 555 ``` 556 struct{} 557 ``` 558 559 Result: 560 561 ``` 562 struct{} 563 ``` 564 565 ### **Stop an ongoing profile** 566 Identifier: `gopls.stop_profile` 567 568 This command is intended for internal use only, by the gopls benchmark 569 runner. 570 571 Args: 572 573 ``` 574 struct{} 575 ``` 576 577 Result: 578 579 ``` 580 { 581 // File is the profile file name. 582 "File": string, 583 } 584 ``` 585 586 ### **Run test(s) (legacy)** 587 Identifier: `gopls.test` 588 589 Runs `go test` for a specific set of test or benchmark functions. 590 591 Args: 592 593 ``` 594 string, 595 []string, 596 []string 597 ``` 598 599 ### **Run go mod tidy** 600 Identifier: `gopls.tidy` 601 602 Runs `go mod tidy` for a module. 603 604 Args: 605 606 ``` 607 { 608 // The file URIs. 609 "URIs": []string, 610 } 611 ``` 612 613 ### **Toggle gc_details** 614 Identifier: `gopls.toggle_gc_details` 615 616 Toggle the calculation of gc annotations. 617 618 Args: 619 620 ``` 621 { 622 // The file URI. 623 "URI": string, 624 } 625 ``` 626 627 ### **Update go.sum** 628 Identifier: `gopls.update_go_sum` 629 630 Updates the go.sum file for a module. 631 632 Args: 633 634 ``` 635 { 636 // The file URIs. 637 "URIs": []string, 638 } 639 ``` 640 641 ### **Upgrade a dependency** 642 Identifier: `gopls.upgrade_dependency` 643 644 Upgrades a dependency in the go.mod file for a module. 645 646 Args: 647 648 ``` 649 { 650 // The go.mod file URI. 651 "URI": string, 652 // Additional args to pass to the go command. 653 "GoCmdArgs": []string, 654 // Whether to add a require directive. 655 "AddRequire": bool, 656 } 657 ``` 658 659 ### **Run go mod vendor** 660 Identifier: `gopls.vendor` 661 662 Runs `go mod vendor` for a module. 663 664 Args: 665 666 ``` 667 { 668 // The file URI. 669 "URI": string, 670 } 671 ``` 672 673 ### **List current Views on the server.** 674 Identifier: `gopls.views` 675 676 This command is intended for use by gopls tests only. 677 678 Result: 679 680 ``` 681 []{ 682 "ID": string, 683 "Type": string, 684 "Root": string, 685 "Folder": string, 686 "EnvOverlay": []string, 687 } 688 ``` 689 690 ### **Fetch workspace statistics** 691 Identifier: `gopls.workspace_stats` 692 693 Query statistics about workspace builds, modules, packages, and files. 694 695 This command is intended for internal use only, by the gopls stats 696 command. 697 698 Result: 699 700 ``` 701 { 702 "Files": { 703 "Total": int, 704 "Largest": int, 705 "Errs": int, 706 }, 707 "Views": []{ 708 "GoCommandVersion": string, 709 "AllPackages": { 710 "Packages": int, 711 "LargestPackage": int, 712 "CompiledGoFiles": int, 713 "Modules": int, 714 }, 715 "WorkspacePackages": { 716 "Packages": int, 717 "LargestPackage": int, 718 "CompiledGoFiles": int, 719 "Modules": int, 720 }, 721 "Diagnostics": int, 722 }, 723 } 724 ``` 725 726 <!-- END Commands: DO NOT MANUALLY EDIT THIS SECTION -->