1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package source
import (
"context"
"fmt"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/internal/lsp/analysis/fillstruct"
"golang.org/x/tools/internal/lsp/analysis/undeclaredname"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/span"
errors "golang.org/x/xerrors"
)
type Command struct {
Title string
Name string
// Async controls whether the command executes asynchronously.
Async bool
// appliesFn is an optional field to indicate whether or not a command can
// be applied to the given inputs. If it returns false, we should not
// suggest this command for these inputs.
appliesFn AppliesFunc
// suggestedFixFn is an optional field to generate the edits that the
// command produces for the given inputs.
suggestedFixFn SuggestedFixFunc
}
// CommandPrefix is the prefix of all command names gopls uses externally.
const CommandPrefix = "gopls."
// ID adds the CommandPrefix to the command name, in order to avoid
// collisions with other language servers.
func (c Command) ID() string {
return CommandPrefix + c.Name
}
type AppliesFunc func(fset *token.FileSet, rng span.Range, src []byte, file *ast.File, pkg *types.Package, info *types.Info) bool
// SuggestedFixFunc is a function used to get the suggested fixes for a given
// gopls command, some of which are provided by go/analysis.Analyzers. Some of
// the analyzers in internal/lsp/analysis are not efficient enough to include
// suggested fixes with their diagnostics, so we have to compute them
// separately. Such analyzers should provide a function with a signature of
// SuggestedFixFunc.
type SuggestedFixFunc func(fset *token.FileSet, rng span.Range, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error)
// Commands are the commands currently supported by gopls.
var Commands = []*Command{
CommandGenerate,
CommandFillStruct,
CommandRegenerateCgo,
CommandTest,
CommandTidy,
CommandUpdateGoSum,
CommandUndeclaredName,
CommandGoGetPackage,
CommandAddDependency,
CommandUpgradeDependency,
CommandRemoveDependency,
CommandVendor,
CommandExtractVariable,
CommandExtractFunction,
CommandToggleDetails,
CommandGenerateGoplsMod,
}
var (
// CommandTest runs `go test` for a specific test function.
CommandTest = &Command{
Name: "test",
Title: "Run test(s)",
Async: true,
}
// CommandGenerate runs `go generate` for a given directory.
CommandGenerate = &Command{
Name: "generate",
Title: "Run go generate",
}
// CommandTidy runs `go mod tidy` for a module.
CommandTidy = &Command{
Name: "tidy",
Title: "Run go mod tidy",
}
// CommandVendor runs `go mod vendor` for a module.
CommandVendor = &Command{
Name: "vendor",
Title: "Run go mod vendor",
}
// CommandGoGetPackage runs `go get` to fetch a package.
CommandGoGetPackage = &Command{
Name: "go_get_package",
Title: "go get package",
}
// CommandUpdateGoSum updates the go.sum file for a module.
CommandUpdateGoSum = &Command{
Name: "update_go_sum",
Title: "Update go.sum",
}
// CommandAddDependency adds a dependency.
CommandAddDependency = &Command{
Name: "add_dependency",
Title: "Add dependency",
}
// CommandUpgradeDependency upgrades a dependency.
CommandUpgradeDependency = &Command{
Name: "upgrade_dependency",
Title: "Upgrade dependency",
}
// CommandRemoveDependency removes a dependency.
CommandRemoveDependency = &Command{
Name: "remove_dependency",
Title: "Remove dependency",
}
// CommandRegenerateCgo regenerates cgo definitions.
CommandRegenerateCgo = &Command{
Name: "regenerate_cgo",
Title: "Regenerate cgo",
}
// CommandToggleDetails controls calculation of gc annotations.
CommandToggleDetails = &Command{
Name: "gc_details",
Title: "Toggle gc_details",
}
// CommandFillStruct is a gopls command to fill a struct with default
// values.
CommandFillStruct = &Command{
Name: "fill_struct",
Title: "Fill struct",
suggestedFixFn: fillstruct.SuggestedFix,
}
// CommandUndeclaredName adds a variable declaration for an undeclared
// name.
CommandUndeclaredName = &Command{
Name: "undeclared_name",
Title: "Undeclared name",
suggestedFixFn: undeclaredname.SuggestedFix,
}
// CommandExtractVariable extracts an expression to a variable.
CommandExtractVariable = &Command{
Name: "extract_variable",
Title: "Extract to variable",
suggestedFixFn: extractVariable,
appliesFn: func(_ *token.FileSet, rng span.Range, _ []byte, file *ast.File, _ *types.Package, _ *types.Info) bool {
_, _, ok, _ := canExtractVariable(rng, file)
return ok
},
}
// CommandExtractFunction extracts statements to a function.
CommandExtractFunction = &Command{
Name: "extract_function",
Title: "Extract to function",
suggestedFixFn: extractFunction,
appliesFn: func(fset *token.FileSet, rng span.Range, src []byte, file *ast.File, _ *types.Package, info *types.Info) bool {
_, ok, _ := canExtractFunction(fset, rng, src, file, info)
return ok
},
}
// CommandGenerateGoplsMod (re)generates the gopls.mod file.
CommandGenerateGoplsMod = &Command{
Name: "generate_gopls_mod",
Title: "Generate gopls.mod",
}
)
// Applies reports whether the command c implements a suggested fix that is
// relevant to the given rng.
func (c *Command) Applies(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) bool {
// If there is no applies function, assume that the command applies.
if c.appliesFn == nil {
return true
}
fset, rng, src, file, _, pkg, info, err := getAllSuggestedFixInputs(ctx, snapshot, fh, pRng)
if err != nil {
return false
}
return c.appliesFn(fset, rng, src, file, pkg, info)
}
// IsSuggestedFix reports whether the given command is intended to work as a
// suggested fix. Suggested fix commands are intended to return edits which are
// then applied to the workspace.
func (c *Command) IsSuggestedFix() bool {
return c.suggestedFixFn != nil
}
// SuggestedFix applies the command's suggested fix to the given file and
// range, returning the resulting edits.
func (c *Command) SuggestedFix(ctx context.Context, snapshot Snapshot, fh VersionedFileHandle, pRng protocol.Range) ([]protocol.TextDocumentEdit, error) {
if c.suggestedFixFn == nil {
return nil, fmt.Errorf("no suggested fix function for %s", c.Name)
}
fset, rng, src, file, m, pkg, info, err := getAllSuggestedFixInputs(ctx, snapshot, fh, pRng)
if err != nil {
return nil, err
}
fix, err := c.suggestedFixFn(fset, rng, src, file, pkg, info)
if err != nil {
return nil, err
}
if fix == nil {
return nil, nil
}
var edits []protocol.TextDocumentEdit
for _, edit := range fix.TextEdits {
rng := span.NewRange(fset, edit.Pos, edit.End)
spn, err := rng.Span()
if err != nil {
return nil, err
}
clRng, err := m.Range(spn)
if err != nil {
return nil, err
}
edits = append(edits, protocol.TextDocumentEdit{
TextDocument: protocol.VersionedTextDocumentIdentifier{
Version: fh.Version(),
TextDocumentIdentifier: protocol.TextDocumentIdentifier{
URI: protocol.URIFromSpanURI(fh.URI()),
},
},
Edits: []protocol.TextEdit{
{
Range: clRng,
NewText: string(edit.NewText),
},
},
})
}
return edits, nil
}
// getAllSuggestedFixInputs is a helper function to collect all possible needed
// inputs for an AppliesFunc or SuggestedFixFunc.
func getAllSuggestedFixInputs(ctx context.Context, snapshot Snapshot, fh FileHandle, pRng protocol.Range) (*token.FileSet, span.Range, []byte, *ast.File, *protocol.ColumnMapper, *types.Package, *types.Info, error) {
pkg, pgf, err := GetParsedFile(ctx, snapshot, fh, NarrowestPackage)
if err != nil {
return nil, span.Range{}, nil, nil, nil, nil, nil, errors.Errorf("getting file for Identifier: %w", err)
}
spn, err := pgf.Mapper.RangeSpan(pRng)
if err != nil {
return nil, span.Range{}, nil, nil, nil, nil, nil, err
}
rng, err := spn.Range(pgf.Mapper.Converter)
if err != nil {
return nil, span.Range{}, nil, nil, nil, nil, nil, err
}
src, err := fh.Read()
if err != nil {
return nil, span.Range{}, nil, nil, nil, nil, nil, err
}
return snapshot.FileSet(), rng, src, pgf.File, pgf.Mapper, pkg.GetTypes(), pkg.GetTypesInfo(), nil
}
|