github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/internal/lsp/command/interface.go (about) 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. 4 5 // Package command defines the interface provided by gopls for the 6 // workspace/executeCommand LSP request. 7 // 8 // This interface is fully specified by the Interface type, provided it 9 // conforms to the restrictions outlined in its doc string. 10 // 11 // Bindings for server-side command dispatch and client-side serialization are 12 // also provided by this package, via code generation. 13 package command 14 15 //go:generate go run -tags=generate generate.go 16 17 import ( 18 "context" 19 20 "github.com/jhump/golang-x-tools/internal/lsp/protocol" 21 ) 22 23 // Interface defines the interface gopls exposes for the 24 // workspace/executeCommand request. 25 // 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 36 // 37 // Applies a fix to a region of source code. 38 ApplyFix(context.Context, ApplyFixArgs) error 39 // Test: Run test(s) (legacy) 40 // 41 // Runs `go test` for a specific set of test or benchmark functions. 42 Test(context.Context, protocol.DocumentURI, []string, []string) error 43 44 // TODO: deprecate Test in favor of RunTests below. 45 46 // Test: Run test(s) 47 // 48 // Runs `go test` for a specific set of test or benchmark functions. 49 RunTests(context.Context, RunTestsArgs) error 50 51 // Generate: Run go generate 52 // 53 // Runs `go generate` for a given directory. 54 Generate(context.Context, GenerateArgs) error 55 56 // RegenerateCgo: Regenerate cgo 57 // 58 // Regenerates cgo definitions. 59 RegenerateCgo(context.Context, URIArg) error 60 61 // Tidy: Run go mod tidy 62 // 63 // Runs `go mod tidy` for a module. 64 Tidy(context.Context, URIArgs) error 65 66 // Vendor: Run go mod vendor 67 // 68 // Runs `go mod vendor` for a module. 69 Vendor(context.Context, URIArg) error 70 71 // UpdateGoSum: Update go.sum 72 // 73 // Updates the go.sum file for a module. 74 UpdateGoSum(context.Context, URIArgs) error 75 76 // CheckUpgrades: Check for upgrades 77 // 78 // Checks for module upgrades. 79 CheckUpgrades(context.Context, CheckUpgradesArgs) error 80 81 // AddDependency: Add a dependency 82 // 83 // Adds a dependency to the go.mod file for a module. 84 AddDependency(context.Context, DependencyArgs) error 85 86 // UpgradeDependency: Upgrade a dependency 87 // 88 // Upgrades a dependency in the go.mod file for a module. 89 UpgradeDependency(context.Context, DependencyArgs) error 90 91 // RemoveDependency: Remove a dependency 92 // 93 // Removes a dependency from the go.mod file of a module. 94 RemoveDependency(context.Context, RemoveDependencyArgs) error 95 96 // GoGetPackage: go get a package 97 // 98 // Runs `go get` to fetch a package. 99 GoGetPackage(context.Context, GoGetPackageArgs) error 100 101 // GCDetails: Toggle gc_details 102 // 103 // Toggle the calculation of gc annotations. 104 GCDetails(context.Context, protocol.DocumentURI) error 105 106 // TODO: deprecate GCDetails in favor of ToggleGCDetails below. 107 108 // ToggleGCDetails: Toggle gc_details 109 // 110 // Toggle the calculation of gc annotations. 111 ToggleGCDetails(context.Context, URIArg) error 112 113 // GenerateGoplsMod: Generate gopls.mod 114 // 115 // (Re)generate the gopls.mod file for a workspace. 116 GenerateGoplsMod(context.Context, URIArg) error 117 118 // ListKnownPackages: List known packages 119 // 120 // Retrieve a list of packages that are importable from the given URI. 121 ListKnownPackages(context.Context, URIArg) (ListKnownPackagesResult, error) 122 123 // ListImports: List imports of a file and its package 124 // 125 // Retrieve a list of imports in the given Go file, and the package it 126 // belongs to. 127 ListImports(context.Context, URIArg) (ListImportsResult, error) 128 129 // AddImport: Add an import 130 // 131 // Ask the server to add an import path to a given Go file. The method will 132 // call applyEdit on the client so that clients don't have to apply the edit 133 // themselves. 134 AddImport(context.Context, AddImportArgs) error 135 136 // StartDebugging: Start the gopls debug server 137 // 138 // Start the gopls debug server if it isn't running, and return the debug 139 // address. 140 StartDebugging(context.Context, DebuggingArgs) (DebuggingResult, error) 141 } 142 143 type RunTestsArgs struct { 144 // The test file containing the tests to run. 145 URI protocol.DocumentURI 146 147 // Specific test names to run, e.g. TestFoo. 148 Tests []string 149 150 // Specific benchmarks to run, e.g. BenchmarkFoo. 151 Benchmarks []string 152 } 153 154 type GenerateArgs struct { 155 // URI for the directory to generate. 156 Dir protocol.DocumentURI 157 158 // Whether to generate recursively (go generate ./...) 159 Recursive bool 160 } 161 162 // TODO(rFindley): document the rest of these once the docgen is fleshed out. 163 164 type ApplyFixArgs struct { 165 // The fix to apply. 166 Fix string 167 // The file URI for the document to fix. 168 URI protocol.DocumentURI 169 // The document range to scan for fixes. 170 Range protocol.Range 171 } 172 173 type URIArg struct { 174 // The file URI. 175 URI protocol.DocumentURI 176 } 177 178 type URIArgs struct { 179 // The file URIs. 180 URIs []protocol.DocumentURI 181 } 182 183 type CheckUpgradesArgs struct { 184 // The go.mod file URI. 185 URI protocol.DocumentURI 186 // The modules to check. 187 Modules []string 188 } 189 190 type DependencyArgs struct { 191 // The go.mod file URI. 192 URI protocol.DocumentURI 193 // Additional args to pass to the go command. 194 GoCmdArgs []string 195 // Whether to add a require directive. 196 AddRequire bool 197 } 198 199 type RemoveDependencyArgs struct { 200 // The go.mod file URI. 201 URI protocol.DocumentURI 202 // The module path to remove. 203 ModulePath string 204 OnlyDiagnostic bool 205 } 206 207 type GoGetPackageArgs struct { 208 // Any document URI within the relevant module. 209 URI protocol.DocumentURI 210 // The package to go get. 211 Pkg string 212 AddRequire bool 213 } 214 215 type AddImportArgs struct { 216 // ImportPath is the target import path that should 217 // be added to the URI file 218 ImportPath string 219 // URI is the file that the ImportPath should be 220 // added to 221 URI protocol.DocumentURI 222 } 223 224 type ListKnownPackagesResult struct { 225 // Packages is a list of packages relative 226 // to the URIArg passed by the command request. 227 // In other words, it omits paths that are already 228 // imported or cannot be imported due to compiler 229 // restrictions. 230 Packages []string 231 } 232 233 type ListImportsResult struct { 234 // Imports is a list of imports in the requested file. 235 Imports []FileImport 236 237 // PackageImports is a list of all imports in the requested file's package. 238 PackageImports []PackageImport 239 } 240 241 type FileImport struct { 242 // Path is the import path of the import. 243 Path string 244 // Name is the name of the import, e.g. `foo` in `import foo "strings"`. 245 Name string 246 } 247 248 type PackageImport struct { 249 // Path is the import path of the import. 250 Path string 251 } 252 253 type WorkspaceMetadataArgs struct { 254 } 255 256 type WorkspaceMetadataResult struct { 257 // All workspaces for this session. 258 Workspaces []Workspace 259 } 260 261 type Workspace struct { 262 // The workspace name. 263 Name string 264 // The workspace module directory. 265 ModuleDir string 266 } 267 268 type DebuggingArgs struct { 269 // Optional: the address (including port) for the debug server to listen on. 270 // If not provided, the debug server will bind to "localhost:0", and the 271 // full debug URL will be contained in the result. 272 // 273 // If there is more than one gopls instance along the serving path (i.e. you 274 // are using a daemon), each gopls instance will attempt to start debugging. 275 // If Addr specifies a port, only the daemon will be able to bind to that 276 // port, and each intermediate gopls instance will fail to start debugging. 277 // For this reason it is recommended not to specify a port (or equivalently, 278 // to specify ":0"). 279 // 280 // If the server was already debugging this field has no effect, and the 281 // result will contain the previously configured debug URL(s). 282 Addr string 283 } 284 285 type DebuggingResult struct { 286 // The URLs to use to access the debug servers, for all gopls instances in 287 // the serving path. For the common case of a single gopls instance (i.e. no 288 // daemon), this will be exactly one address. 289 // 290 // In the case of one or more gopls instances forwarding the LSP to a daemon, 291 // URLs will contain debug addresses for each server in the serving path, in 292 // serving order. The daemon debug address will be the last entry in the 293 // slice. If any intermediate gopls instance fails to start debugging, no 294 // error will be returned but the debug URL for that server in the URLs slice 295 // will be empty. 296 URLs []string 297 }