| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"fmt" |
| 10 |
"go/token" |
| 11 |
|
| 12 |
"golang.org/x/tools/go/analysis" |
| 13 |
"golang.org/x/tools/gopls/internal/analysis/fillstruct" |
| 14 |
"golang.org/x/tools/gopls/internal/analysis/unusedparams" |
| 15 |
"golang.org/x/tools/gopls/internal/cache" |
| 16 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 17 |
"golang.org/x/tools/gopls/internal/file" |
| 18 |
"golang.org/x/tools/gopls/internal/protocol" |
| 19 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
type fixer func(ctx context.Context, s *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) |
| 39 |
|
| 40 |
|
| 41 |
type singleFileFixer func(pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
func singleFile(fixer1 singleFileFixer) fixer { |
| 46 |
return func(_ context.Context, _ *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { |
| 47 |
return fixer1(pkg, pgf, start, end) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
const ( |
| 53 |
fixExtractVariable = "extract_variable" |
| 54 |
fixExtractVariableAll = "extract_variable_all" |
| 55 |
fixExtractFunction = "extract_function" |
| 56 |
fixExtractMethod = "extract_method" |
| 57 |
fixInlineCall = "inline_call" |
| 58 |
fixInlineVariable = "inline_variable" |
| 59 |
fixInvertIfCondition = "invert_if_condition" |
| 60 |
fixSplitLines = "split_lines" |
| 61 |
fixJoinLines = "join_lines" |
| 62 |
fixCreateUndeclared = "create_undeclared" |
| 63 |
fixMissingInterfaceMethods = "stub_missing_interface_method" |
| 64 |
fixMissingCalledFunction = "stub_missing_called_function" |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func ApplyFix(ctx context.Context, fix string, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.DocumentChange, error) { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if fix == unusedparams.FixCategory { |
| 89 |
return removeParam(ctx, snapshot, fh, rng) |
| 90 |
} |
| 91 |
|
| 92 |
fixers := map[string]fixer{ |
| 93 |
|
| 94 |
|
| 95 |
fillstruct.FixCategory: singleFile(fillstruct.SuggestedFix), |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
fixExtractFunction: singleFile(extractFunction), |
| 100 |
fixExtractMethod: singleFile(extractMethod), |
| 101 |
fixExtractVariable: singleFile(extractVariableOne), |
| 102 |
fixExtractVariableAll: singleFile(extractVariableAll), |
| 103 |
fixInlineCall: inlineCall, |
| 104 |
fixInlineVariable: singleFile(inlineVariableOne), |
| 105 |
fixInvertIfCondition: singleFile(invertIfCondition), |
| 106 |
fixSplitLines: singleFile(splitLines), |
| 107 |
fixJoinLines: singleFile(joinLines), |
| 108 |
fixCreateUndeclared: singleFile(createUndeclared), |
| 109 |
fixMissingInterfaceMethods: stubMissingInterfaceMethodsFixer, |
| 110 |
fixMissingCalledFunction: stubMissingCalledFunctionFixer, |
| 111 |
} |
| 112 |
fixer, ok := fixers[fix] |
| 113 |
if !ok { |
| 114 |
return nil, fmt.Errorf("no suggested fix function for %s", fix) |
| 115 |
} |
| 116 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI()) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
start, end, err := pgf.RangePos(rng) |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
fixFset, suggestion, err := fixer(ctx, snapshot, pkg, pgf, start, end) |
| 125 |
if err != nil { |
| 126 |
return nil, err |
| 127 |
} |
| 128 |
if suggestion == nil { |
| 129 |
return nil, nil |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
for _, edit := range suggestion.TextEdits { |
| 135 |
if fixFset.File(edit.Pos) == nil { |
| 136 |
switch fix { |
| 137 |
case fillstruct.FixCategory: |
| 138 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 139 |
case fixExtractFunction: |
| 140 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 141 |
case fixExtractMethod: |
| 142 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 143 |
case fixExtractVariable: |
| 144 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 145 |
case fixExtractVariableAll: |
| 146 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 147 |
case fixInlineCall: |
| 148 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 149 |
case fixInlineVariable: |
| 150 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 151 |
case fixInvertIfCondition: |
| 152 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 153 |
case fixSplitLines: |
| 154 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 155 |
case fixJoinLines: |
| 156 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 157 |
case fixCreateUndeclared: |
| 158 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 159 |
case fixMissingInterfaceMethods: |
| 160 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 161 |
case fixMissingCalledFunction: |
| 162 |
bug.Report("no token.File for TextEdit.Pos (#68818)") |
| 163 |
} |
| 164 |
break |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return suggestedFixToDocumentChange(ctx, snapshot, fixFset, suggestion) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
func suggestedFixToDocumentChange(ctx context.Context, snapshot *cache.Snapshot, fset *token.FileSet, suggestion *analysis.SuggestedFix) ([]protocol.DocumentChange, error) { |
| 173 |
type fileInfo struct { |
| 174 |
fh file.Handle |
| 175 |
mapper *protocol.Mapper |
| 176 |
edits []protocol.TextEdit |
| 177 |
} |
| 178 |
files := make(map[protocol.DocumentURI]*fileInfo) |
| 179 |
for _, edit := range suggestion.TextEdits { |
| 180 |
tokFile := fset.File(edit.Pos) |
| 181 |
if tokFile == nil { |
| 182 |
return nil, bug.Errorf("no file for edit position (#68818)") |
| 183 |
} |
| 184 |
end := edit.End |
| 185 |
if !end.IsValid() { |
| 186 |
end = edit.Pos |
| 187 |
} |
| 188 |
uri := protocol.URIFromPath(tokFile.Name()) |
| 189 |
info, ok := files[uri] |
| 190 |
if !ok { |
| 191 |
|
| 192 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 193 |
if err != nil { |
| 194 |
return nil, err |
| 195 |
} |
| 196 |
content, err := fh.Content() |
| 197 |
if err != nil { |
| 198 |
return nil, err |
| 199 |
} |
| 200 |
mapper := protocol.NewMapper(uri, content) |
| 201 |
info = &fileInfo{fh, mapper, nil} |
| 202 |
files[uri] = info |
| 203 |
} |
| 204 |
rng, err := info.mapper.PosRange(tokFile, edit.Pos, end) |
| 205 |
if err != nil { |
| 206 |
return nil, err |
| 207 |
} |
| 208 |
info.edits = append(info.edits, protocol.TextEdit{ |
| 209 |
Range: rng, |
| 210 |
NewText: string(edit.NewText), |
| 211 |
}) |
| 212 |
} |
| 213 |
var changes []protocol.DocumentChange |
| 214 |
for _, info := range files { |
| 215 |
change := protocol.DocumentChangeEdit(info.fh, info.edits) |
| 216 |
changes = append(changes, change) |
| 217 |
} |
| 218 |
return changes, nil |
| 219 |
} |