1 // Copyright 2020 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.
14 "golang.org/x/tools/go/analysis"
15 "golang.org/x/tools/internal/lsp/analysis/fillstruct"
16 "golang.org/x/tools/internal/lsp/analysis/undeclaredname"
17 "golang.org/x/tools/internal/lsp/protocol"
18 "golang.org/x/tools/internal/span"
19 errors "golang.org/x/xerrors"
22 // SuggestedFixFunc is a function used to get the suggested fixes for a given
23 // gopls command, some of which are provided by go/analysis.Analyzers. Some of
24 // the analyzers in internal/lsp/analysis are not efficient enough to include
25 // suggested fixes with their diagnostics, so we have to compute them
26 // separately. Such analyzers should provide a function with a signature of
28 type SuggestedFixFunc func(fset *token.FileSet, rng span.Range, src []byte, file *ast.File, pkg *types.Package, info *types.Info) (*analysis.SuggestedFix, error)
31 FillStruct = "fill_struct"
32 UndeclaredName = "undeclared_name"
33 ExtractVariable = "extract_variable"
34 ExtractFunction = "extract_function"
37 // suggestedFixes maps a suggested fix command id to its handler.
38 var suggestedFixes = map[string]SuggestedFixFunc{
39 FillStruct: fillstruct.SuggestedFix,
40 UndeclaredName: undeclaredname.SuggestedFix,
41 ExtractVariable: extractVariable,
42 ExtractFunction: extractFunction,
45 func SuggestedFixFromCommand(cmd protocol.Command) SuggestedFix {
52 // ApplyFix applies the command's suggested fix to the given file and
53 // range, returning the resulting edits.
54 func ApplyFix(ctx context.Context, fix string, snapshot Snapshot, fh VersionedFileHandle, pRng protocol.Range) ([]protocol.TextDocumentEdit, error) {
55 handler, ok := suggestedFixes[fix]
57 return nil, fmt.Errorf("no suggested fix function for %s", fix)
59 fset, rng, src, file, m, pkg, info, err := getAllSuggestedFixInputs(ctx, snapshot, fh, pRng)
63 suggestion, err := handler(fset, rng, src, file, pkg, info)
67 if suggestion == nil {
71 var edits []protocol.TextDocumentEdit
72 for _, edit := range suggestion.TextEdits {
73 rng := span.NewRange(fset, edit.Pos, edit.End)
74 spn, err := rng.Span()
78 clRng, err := m.Range(spn)
82 edits = append(edits, protocol.TextDocumentEdit{
83 TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{
84 Version: fh.Version(),
85 TextDocumentIdentifier: protocol.TextDocumentIdentifier{
86 URI: protocol.URIFromSpanURI(fh.URI()),
89 Edits: []protocol.TextEdit{
92 NewText: string(edit.NewText),
100 // getAllSuggestedFixInputs is a helper function to collect all possible needed
101 // inputs for an AppliesFunc or SuggestedFixFunc.
102 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) {
103 pkg, pgf, err := GetParsedFile(ctx, snapshot, fh, NarrowestPackage)
105 return nil, span.Range{}, nil, nil, nil, nil, nil, errors.Errorf("getting file for Identifier: %w", err)
107 rng, err := pgf.Mapper.RangeToSpanRange(pRng)
109 return nil, span.Range{}, nil, nil, nil, nil, nil, err
111 return snapshot.FileSet(), rng, pgf.Src, pgf.File, pgf.Mapper, pkg.GetTypes(), pkg.GetTypesInfo(), nil