1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package command defines the interface provided by gopls for the
6 // workspace/executeCommand LSP request.
8 // This interface is fully specified by the Interface type, provided it
9 // conforms to the restrictions outlined in its doc string.
11 // Bindings for server-side command dispatch and client-side serialization are
12 // also provided by this package, via code generation.
15 //go:generate go run -tags=generate generate.go
20 "golang.org/x/tools/internal/lsp/protocol"
23 // Interface defines the interface gopls exposes for the
24 // workspace/executeCommand request.
26 // This interface is used to generate marshaling/unmarshaling code, dispatch,
27 // and documentation, and so has some additional restrictions:
28 // 1. All method arguments must be JSON serializable.
29 // 2. Methods must return either error or (T, error), where T is a
30 // JSON serializable type.
31 // 3. The first line of the doc string is special. Everything after the colon
32 // is considered the command 'Title'.
33 // TODO(rFindley): reconsider this -- Title may be unnecessary.
34 type Interface interface {
35 // ApplyFix: Apply a fix
37 // Applies a fix to a region of source code.
38 ApplyFix(context.Context, ApplyFixArgs) error
39 // Test: Run test(s) (legacy)
41 // Runs `go test` for a specific set of test or benchmark functions.
42 Test(context.Context, protocol.DocumentURI, []string, []string) error
44 // TODO: deprecate Test in favor of RunTests below.
48 // Runs `go test` for a specific set of test or benchmark functions.
49 RunTests(context.Context, RunTestsArgs) error
51 // Generate: Run go generate
53 // Runs `go generate` for a given directory.
54 Generate(context.Context, GenerateArgs) error
56 // RegenerateCgo: Regenerate cgo
58 // Regenerates cgo definitions.
59 RegenerateCgo(context.Context, URIArg) error
61 // Tidy: Run go mod tidy
63 // Runs `go mod tidy` for a module.
64 Tidy(context.Context, URIArgs) error
66 // Vendor: Run go mod vendor
68 // Runs `go mod vendor` for a module.
69 Vendor(context.Context, URIArg) error
71 // UpdateGoSum: Update go.sum
73 // Updates the go.sum file for a module.
74 UpdateGoSum(context.Context, URIArgs) error
76 // CheckUpgrades: Check for upgrades
78 // Checks for module upgrades.
79 CheckUpgrades(context.Context, CheckUpgradesArgs) error
81 // AddDependency: Add dependency
83 // Adds a dependency to the go.mod file for a module.
84 AddDependency(context.Context, DependencyArgs) error
86 // UpgradeDependency: Upgrade dependency
88 // Upgrades a dependency in the go.mod file for a module.
89 UpgradeDependency(context.Context, DependencyArgs) error
91 // RemoveDependency: Remove dependency
93 // Removes a dependency from the go.mod file of a module.
94 RemoveDependency(context.Context, RemoveDependencyArgs) error
96 // GoGetPackage: go get package
98 // Runs `go get` to fetch a package.
99 GoGetPackage(context.Context, GoGetPackageArgs) error
101 // GCDetails: Toggle gc_details
103 // Toggle the calculation of gc annotations.
104 GCDetails(context.Context, protocol.DocumentURI) error
106 // TODO: deprecate GCDetails in favor of ToggleGCDetails below.
108 // ToggleGCDetails: Toggle gc_details
110 // Toggle the calculation of gc annotations.
111 ToggleGCDetails(context.Context, URIArg) error
113 // GenerateGoplsMod: Generate gopls.mod
115 // (Re)generate the gopls.mod file for a workspace.
116 GenerateGoplsMod(context.Context, URIArg) error
118 ListKnownPackages(context.Context, URIArg) (ListKnownPackagesResult, error)
120 AddImport(context.Context, AddImportArgs) (AddImportResult, error)
123 type RunTestsArgs struct {
124 // The test file containing the tests to run.
125 URI protocol.DocumentURI
127 // Specific test names to run, e.g. TestFoo.
130 // Specific benchmarks to run, e.g. BenchmarkFoo.
134 type GenerateArgs struct {
135 // URI for the directory to generate.
136 Dir protocol.DocumentURI
138 // Whether to generate recursively (go generate ./...)
142 // TODO(rFindley): document the rest of these once the docgen is fleshed out.
144 type ApplyFixArgs struct {
147 // The file URI for the document to fix.
148 URI protocol.DocumentURI
149 // The document range to scan for fixes.
155 URI protocol.DocumentURI
158 type URIArgs struct {
160 URIs []protocol.DocumentURI
163 type CheckUpgradesArgs struct {
164 // The go.mod file URI.
165 URI protocol.DocumentURI
166 // The modules to check.
170 type DependencyArgs struct {
171 // The go.mod file URI.
172 URI protocol.DocumentURI
173 // Additional args to pass to the go command.
175 // Whether to add a require directive.
179 type RemoveDependencyArgs struct {
180 // The go.mod file URI.
181 URI protocol.DocumentURI
182 // The module path to remove.
187 type GoGetPackageArgs struct {
188 // Any document URI within the relevant module.
189 URI protocol.DocumentURI
190 // The package to go get.
195 // TODO (Marwan): document :)
197 type AddImportArgs struct {
199 URI protocol.DocumentURI
202 type AddImportResult struct {
203 Edits []protocol.TextDocumentEdit
206 type ListKnownPackagesResult struct {