cuelang.org/go@v0.10.1/internal/golangorgx/gopls/protocol/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  import (
    16  	"context"
    17  
    18  	"cuelang.org/go/internal/golangorgx/gopls/protocol"
    19  )
    20  
    21  // Interface defines the interface gopls exposes for the
    22  // workspace/executeCommand request.
    23  //
    24  // This interface is used to generate marshaling/unmarshaling code, dispatch,
    25  // and documentation, and so has some additional restrictions:
    26  //  1. All method arguments must be JSON serializable.
    27  //  2. Methods must return either error or (T, error), where T is a
    28  //     JSON serializable type.
    29  //  3. The first line of the doc string is special. Everything after the colon
    30  //     is considered the command 'Title'.
    31  //     TODO(rFindley): reconsider this -- Title may be unnecessary.
    32  //
    33  // The doc comment on each method is eventually published at
    34  // https://github.com/golang/tools/blob/master/gopls/doc/commands.md,
    35  // so please be consistent in using this form:
    36  //
    37  //	Command: Capitalized verb phrase with no period
    38  //
    39  //	Longer description here...
    40  type Interface interface {
    41  	// ApplyFix: Apply a fix
    42  	//
    43  	// Applies a fix to a region of source code.
    44  	ApplyFix(context.Context, ApplyFixArgs) (*protocol.WorkspaceEdit, error)
    45  
    46  	// Test: Run test(s) (legacy)
    47  	//
    48  	// Runs `go test` for a specific set of test or benchmark functions.
    49  	Test(context.Context, protocol.DocumentURI, []string, []string) error
    50  
    51  	// TODO: deprecate Test in favor of RunTests below.
    52  
    53  	// Test: Run test(s)
    54  	//
    55  	// Runs `go test` for a specific set of test or benchmark functions.
    56  	RunTests(context.Context, RunTestsArgs) error
    57  
    58  	// Generate: Run go generate
    59  	//
    60  	// Runs `go generate` for a given directory.
    61  	Generate(context.Context, GenerateArgs) error
    62  
    63  	// RegenerateCgo: Regenerate cgo
    64  	//
    65  	// Regenerates cgo definitions.
    66  	RegenerateCgo(context.Context, URIArg) error
    67  
    68  	// Tidy: Run go mod tidy
    69  	//
    70  	// Runs `go mod tidy` for a module.
    71  	Tidy(context.Context, URIArgs) error
    72  
    73  	// Vendor: Run go mod vendor
    74  	//
    75  	// Runs `go mod vendor` for a module.
    76  	Vendor(context.Context, URIArg) error
    77  
    78  	// EditGoDirective: Run go mod edit -go=version
    79  	//
    80  	// Runs `go mod edit -go=version` for a module.
    81  	EditGoDirective(context.Context, EditGoDirectiveArgs) error
    82  
    83  	// UpdateGoSum: Update go.sum
    84  	//
    85  	// Updates the go.sum file for a module.
    86  	UpdateGoSum(context.Context, URIArgs) error
    87  
    88  	// CheckUpgrades: Check for upgrades
    89  	//
    90  	// Checks for module upgrades.
    91  	CheckUpgrades(context.Context, CheckUpgradesArgs) error
    92  
    93  	// AddDependency: Add a dependency
    94  	//
    95  	// Adds a dependency to the go.mod file for a module.
    96  	AddDependency(context.Context, DependencyArgs) error
    97  
    98  	// UpgradeDependency: Upgrade a dependency
    99  	//
   100  	// Upgrades a dependency in the go.mod file for a module.
   101  	UpgradeDependency(context.Context, DependencyArgs) error
   102  
   103  	// RemoveDependency: Remove a dependency
   104  	//
   105  	// Removes a dependency from the go.mod file of a module.
   106  	RemoveDependency(context.Context, RemoveDependencyArgs) error
   107  
   108  	// ResetGoModDiagnostics: Reset go.mod diagnostics
   109  	//
   110  	// Reset diagnostics in the go.mod file of a module.
   111  	ResetGoModDiagnostics(context.Context, ResetGoModDiagnosticsArgs) error
   112  
   113  	// GoGetPackage: 'go get' a package
   114  	//
   115  	// Runs `go get` to fetch a package.
   116  	GoGetPackage(context.Context, GoGetPackageArgs) error
   117  
   118  	// GCDetails: Toggle gc_details
   119  	//
   120  	// Toggle the calculation of gc annotations.
   121  	GCDetails(context.Context, protocol.DocumentURI) error
   122  
   123  	// TODO: deprecate GCDetails in favor of ToggleGCDetails below.
   124  
   125  	// ToggleGCDetails: Toggle gc_details
   126  	//
   127  	// Toggle the calculation of gc annotations.
   128  	ToggleGCDetails(context.Context, URIArg) error
   129  
   130  	// ListKnownPackages: List known packages
   131  	//
   132  	// Retrieve a list of packages that are importable from the given URI.
   133  	ListKnownPackages(context.Context, URIArg) (ListKnownPackagesResult, error)
   134  
   135  	// ListImports: List imports of a file and its package
   136  	//
   137  	// Retrieve a list of imports in the given Go file, and the package it
   138  	// belongs to.
   139  	ListImports(context.Context, URIArg) (ListImportsResult, error)
   140  
   141  	// AddImport: Add an import
   142  	//
   143  	// Ask the server to add an import path to a given Go file.  The method will
   144  	// call applyEdit on the client so that clients don't have to apply the edit
   145  	// themselves.
   146  	AddImport(context.Context, AddImportArgs) error
   147  
   148  	// StartDebugging: Start the gopls debug server
   149  	//
   150  	// Start the gopls debug server if it isn't running, and return the debug
   151  	// address.
   152  	StartDebugging(context.Context, DebuggingArgs) (DebuggingResult, error)
   153  
   154  	// StartProfile: Start capturing a profile of gopls' execution
   155  	//
   156  	// Start a new pprof profile. Before using the resulting file, profiling must
   157  	// be stopped with a corresponding call to StopProfile.
   158  	//
   159  	// This command is intended for internal use only, by the gopls benchmark
   160  	// runner.
   161  	StartProfile(context.Context, StartProfileArgs) (StartProfileResult, error)
   162  
   163  	// StopProfile: Stop an ongoing profile
   164  	//
   165  	// This command is intended for internal use only, by the gopls benchmark
   166  	// runner.
   167  	StopProfile(context.Context, StopProfileArgs) (StopProfileResult, error)
   168  
   169  	// MemStats: Fetch memory statistics
   170  	//
   171  	// Call runtime.GC multiple times and return memory statistics as reported by
   172  	// runtime.MemStats.
   173  	//
   174  	// This command is used for benchmarking, and may change in the future.
   175  	MemStats(context.Context) (MemStatsResult, error)
   176  
   177  	// WorkspaceStats: Fetch workspace statistics
   178  	//
   179  	// Query statistics about workspace builds, modules, packages, and files.
   180  	//
   181  	// This command is intended for internal use only, by the gopls stats
   182  	// command.
   183  	WorkspaceStats(context.Context) (WorkspaceStatsResult, error)
   184  
   185  	// RunGoWorkCommand: Run `go work [args...]`, and apply the resulting go.work
   186  	// edits to the current go.work file
   187  	RunGoWorkCommand(context.Context, RunGoWorkArgs) error
   188  
   189  	// AddTelemetryCounters: Update the given telemetry counters
   190  	//
   191  	// Gopls will prepend "fwd/" to all the counters updated using this command
   192  	// to avoid conflicts with other counters gopls collects.
   193  	AddTelemetryCounters(context.Context, AddTelemetryCountersArgs) error
   194  
   195  	// MaybePromptForTelemetry: Prompt user to enable telemetry
   196  	//
   197  	// Checks for the right conditions, and then prompts the user
   198  	// to ask if they want to enable Go telemetry uploading. If
   199  	// the user responds 'Yes', the telemetry mode is set to "on".
   200  	MaybePromptForTelemetry(context.Context) error
   201  
   202  	// ChangeSignature: Perform a "change signature" refactoring
   203  	//
   204  	// This command is experimental, currently only supporting parameter removal.
   205  	// Its signature will certainly change in the future (pun intended).
   206  	ChangeSignature(context.Context, ChangeSignatureArgs) (*protocol.WorkspaceEdit, error)
   207  
   208  	// DiagnoseFiles: Cause server to publish diagnostics for the specified files.
   209  	//
   210  	// This command is needed by the 'gopls {check,fix}' CLI subcommands.
   211  	DiagnoseFiles(context.Context, DiagnoseFilesArgs) error
   212  
   213  	// Views: List current Views on the server.
   214  	//
   215  	// This command is intended for use by gopls tests only.
   216  	Views(context.Context) ([]View, error)
   217  }
   218  
   219  type RunTestsArgs struct {
   220  	// The test file containing the tests to run.
   221  	URI protocol.DocumentURI
   222  
   223  	// Specific test names to run, e.g. TestFoo.
   224  	Tests []string
   225  
   226  	// Specific benchmarks to run, e.g. BenchmarkFoo.
   227  	Benchmarks []string
   228  }
   229  
   230  type GenerateArgs struct {
   231  	// URI for the directory to generate.
   232  	Dir protocol.DocumentURI
   233  
   234  	// Whether to generate recursively (go generate ./...)
   235  	Recursive bool
   236  }
   237  
   238  // TODO(rFindley): document the rest of these once the docgen is fleshed out.
   239  
   240  type ApplyFixArgs struct {
   241  	// The name of the fix to apply.
   242  	//
   243  	// For fixes suggested by analyzers, this is a string constant
   244  	// advertised by the analyzer that matches the Category of
   245  	// the analysis.Diagnostic with a SuggestedFix containing no edits.
   246  	//
   247  	// For fixes suggested by code actions, this is a string agreed
   248  	// upon by the code action and golang.ApplyFix.
   249  	Fix string
   250  
   251  	// The file URI for the document to fix.
   252  	URI protocol.DocumentURI
   253  	// The document range to scan for fixes.
   254  	Range protocol.Range
   255  	// Whether to resolve and return the edits.
   256  	ResolveEdits bool
   257  }
   258  
   259  type URIArg struct {
   260  	// The file URI.
   261  	URI protocol.DocumentURI
   262  }
   263  
   264  type URIArgs struct {
   265  	// The file URIs.
   266  	URIs []protocol.DocumentURI
   267  }
   268  
   269  type CheckUpgradesArgs struct {
   270  	// The go.mod file URI.
   271  	URI protocol.DocumentURI
   272  	// The modules to check.
   273  	Modules []string
   274  }
   275  
   276  type DependencyArgs struct {
   277  	// The go.mod file URI.
   278  	URI protocol.DocumentURI
   279  	// Additional args to pass to the go command.
   280  	GoCmdArgs []string
   281  	// Whether to add a require directive.
   282  	AddRequire bool
   283  }
   284  
   285  type RemoveDependencyArgs struct {
   286  	// The go.mod file URI.
   287  	URI protocol.DocumentURI
   288  	// The module path to remove.
   289  	ModulePath string
   290  	// If the module is tidied apart from the one unused diagnostic, we can
   291  	// run `go get module@none`, and then run `go mod tidy`. Otherwise, we
   292  	// must make textual edits.
   293  	OnlyDiagnostic bool
   294  }
   295  
   296  type EditGoDirectiveArgs struct {
   297  	// Any document URI within the relevant module.
   298  	URI protocol.DocumentURI
   299  	// The version to pass to `go mod edit -go`.
   300  	Version string
   301  }
   302  
   303  type GoGetPackageArgs struct {
   304  	// Any document URI within the relevant module.
   305  	URI protocol.DocumentURI
   306  	// The package to go get.
   307  	Pkg        string
   308  	AddRequire bool
   309  }
   310  
   311  type AddImportArgs struct {
   312  	// ImportPath is the target import path that should
   313  	// be added to the URI file
   314  	ImportPath string
   315  	// URI is the file that the ImportPath should be
   316  	// added to
   317  	URI protocol.DocumentURI
   318  }
   319  
   320  type ListKnownPackagesResult struct {
   321  	// Packages is a list of packages relative
   322  	// to the URIArg passed by the command request.
   323  	// In other words, it omits paths that are already
   324  	// imported or cannot be imported due to compiler
   325  	// restrictions.
   326  	Packages []string
   327  }
   328  
   329  type ListImportsResult struct {
   330  	// Imports is a list of imports in the requested file.
   331  	Imports []FileImport
   332  
   333  	// PackageImports is a list of all imports in the requested file's package.
   334  	PackageImports []PackageImport
   335  }
   336  
   337  type FileImport struct {
   338  	// Path is the import path of the import.
   339  	Path string
   340  	// Name is the name of the import, e.g. `foo` in `import foo "strings"`.
   341  	Name string
   342  }
   343  
   344  type PackageImport struct {
   345  	// Path is the import path of the import.
   346  	Path string
   347  }
   348  
   349  type DebuggingArgs struct {
   350  	// Optional: the address (including port) for the debug server to listen on.
   351  	// If not provided, the debug server will bind to "localhost:0", and the
   352  	// full debug URL will be contained in the result.
   353  	//
   354  	// If there is more than one gopls instance along the serving path (i.e. you
   355  	// are using a daemon), each gopls instance will attempt to start debugging.
   356  	// If Addr specifies a port, only the daemon will be able to bind to that
   357  	// port, and each intermediate gopls instance will fail to start debugging.
   358  	// For this reason it is recommended not to specify a port (or equivalently,
   359  	// to specify ":0").
   360  	//
   361  	// If the server was already debugging this field has no effect, and the
   362  	// result will contain the previously configured debug URL(s).
   363  	Addr string
   364  }
   365  
   366  type DebuggingResult struct {
   367  	// The URLs to use to access the debug servers, for all gopls instances in
   368  	// the serving path. For the common case of a single gopls instance (i.e. no
   369  	// daemon), this will be exactly one address.
   370  	//
   371  	// In the case of one or more gopls instances forwarding the LSP to a daemon,
   372  	// URLs will contain debug addresses for each server in the serving path, in
   373  	// serving order. The daemon debug address will be the last entry in the
   374  	// slice. If any intermediate gopls instance fails to start debugging, no
   375  	// error will be returned but the debug URL for that server in the URLs slice
   376  	// will be empty.
   377  	URLs []string
   378  }
   379  
   380  // StartProfileArgs holds the arguments to the StartProfile command.
   381  //
   382  // It is a placeholder for future compatibility.
   383  type StartProfileArgs struct {
   384  }
   385  
   386  // StartProfileResult holds the result of the StartProfile command.
   387  //
   388  // It is a placeholder for future compatibility.
   389  type StartProfileResult struct {
   390  }
   391  
   392  // StopProfileArgs holds the arguments to the StopProfile command.
   393  //
   394  // It is a placeholder for future compatibility.
   395  type StopProfileArgs struct {
   396  }
   397  
   398  // StopProfileResult holds the result to the StopProfile command.
   399  type StopProfileResult struct {
   400  	// File is the profile file name.
   401  	File string
   402  }
   403  
   404  type ResetGoModDiagnosticsArgs struct {
   405  	URIArg
   406  
   407  	// Optional: source of the diagnostics to reset.
   408  	// If not set, all resettable go.mod diagnostics will be cleared.
   409  	DiagnosticSource string
   410  }
   411  
   412  type VulncheckArgs struct {
   413  	// Any document in the directory from which govulncheck will run.
   414  	URI protocol.DocumentURI
   415  
   416  	// Package pattern. E.g. "", ".", "./...".
   417  	Pattern string
   418  
   419  	// TODO: -tests
   420  }
   421  
   422  // RunVulncheckResult holds the result of asynchronously starting the vulncheck
   423  // command.
   424  type RunVulncheckResult struct {
   425  	// Token holds the progress token for LSP workDone reporting of the vulncheck
   426  	// invocation.
   427  	Token protocol.ProgressToken
   428  }
   429  
   430  // CallStack models a trace of function calls starting
   431  // with a client function or method and ending with a
   432  // call to a vulnerable symbol.
   433  type CallStack []StackEntry
   434  
   435  // StackEntry models an element of a call stack.
   436  type StackEntry struct {
   437  	// See golang.org/x/exp/vulncheck.StackEntry.
   438  
   439  	// User-friendly representation of function/method names.
   440  	// e.g. package.funcName, package.(recvType).methodName, ...
   441  	Name string
   442  	URI  protocol.DocumentURI
   443  	Pos  protocol.Position // Start position. (0-based. Column is always 0)
   444  }
   445  
   446  // MemStatsResult holds selected fields from runtime.MemStats.
   447  type MemStatsResult struct {
   448  	HeapAlloc  uint64
   449  	HeapInUse  uint64
   450  	TotalAlloc uint64
   451  }
   452  
   453  // WorkspaceStatsResult returns information about the size and shape of the
   454  // workspace.
   455  type WorkspaceStatsResult struct {
   456  	Files FileStats   // file stats for the cache
   457  	Views []ViewStats // stats for each view in the session
   458  }
   459  
   460  // FileStats holds information about a set of files.
   461  type FileStats struct {
   462  	Total   int // total number of files
   463  	Largest int // number of bytes in the largest file
   464  	Errs    int // number of files that could not be read
   465  }
   466  
   467  // ViewStats holds information about a single View in the session.
   468  type ViewStats struct {
   469  	GoCommandVersion  string       // version of the Go command resolved for this view
   470  	AllPackages       PackageStats // package info for all packages (incl. dependencies)
   471  	WorkspacePackages PackageStats // package info for workspace packages
   472  	Diagnostics       int          // total number of diagnostics in the workspace
   473  }
   474  
   475  // PackageStats holds information about a collection of packages.
   476  type PackageStats struct {
   477  	Packages        int // total number of packages
   478  	LargestPackage  int // number of files in the largest package
   479  	CompiledGoFiles int // total number of compiled Go files across all packages
   480  	Modules         int // total number of unique modules
   481  }
   482  
   483  type RunGoWorkArgs struct {
   484  	ViewID    string   // ID of the view to run the command from
   485  	InitFirst bool     // Whether to run `go work init` first
   486  	Args      []string // Args to pass to `go work`
   487  }
   488  
   489  // AddTelemetryCountersArgs holds the arguments to the AddCounters command
   490  // that updates the telemetry counters.
   491  type AddTelemetryCountersArgs struct {
   492  	// Names and Values must have the same length.
   493  	Names  []string // Name of counters.
   494  	Values []int64  // Values added to the corresponding counters. Must be non-negative.
   495  }
   496  
   497  // ChangeSignatureArgs specifies a "change signature" refactoring to perform.
   498  type ChangeSignatureArgs struct {
   499  	RemoveParameter protocol.Location
   500  	// Whether to resolve and return the edits.
   501  	ResolveEdits bool
   502  }
   503  
   504  // DiagnoseFilesArgs specifies a set of files for which diagnostics are wanted.
   505  type DiagnoseFilesArgs struct {
   506  	Files []protocol.DocumentURI
   507  }
   508  
   509  // A View holds summary information about a cache.View.
   510  type View struct {
   511  	Type       string               // view type (via cache.ViewType.String)
   512  	Root       protocol.DocumentURI // root dir of the view (e.g. containing go.mod or go.work)
   513  	Folder     protocol.DocumentURI // workspace folder associated with the view
   514  	EnvOverlay []string             // environment variable overrides
   515  }