| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
"go/format" |
| 12 |
"go/parser" |
| 13 |
"go/token" |
| 14 |
"go/types" |
| 15 |
pathpkg "path" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/tools/go/analysis" |
| 19 |
"golang.org/x/tools/go/ast/astutil" |
| 20 |
"golang.org/x/tools/gopls/internal/cache" |
| 21 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 22 |
"golang.org/x/tools/gopls/internal/cache/parsego" |
| 23 |
"golang.org/x/tools/gopls/internal/golang/stubmethods" |
| 24 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 25 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 26 |
"golang.org/x/tools/gopls/internal/util/tokeninternal" |
| 27 |
"golang.org/x/tools/internal/diff" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
func stubMissingInterfaceMethodsFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { |
| 34 |
si := stubmethods.GetIfaceStubInfo(pkg.FileSet(), pkg.TypesInfo(), pgf, start, end) |
| 35 |
if si == nil { |
| 36 |
return nil, nil, fmt.Errorf("nil interface request") |
| 37 |
} |
| 38 |
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.Concrete.Obj(), si.Emit) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
func stubMissingCalledFunctionFixer(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, pgf *parsego.File, start, end token.Pos) (*token.FileSet, *analysis.SuggestedFix, error) { |
| 45 |
si := stubmethods.GetCallStubInfo(pkg.FileSet(), pkg.TypesInfo(), pgf, start, end) |
| 46 |
if si == nil { |
| 47 |
return nil, nil, fmt.Errorf("invalid type request") |
| 48 |
} |
| 49 |
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.After, si.Emit) |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
type emitter = func(out *bytes.Buffer, qual types.Qualifier) error |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
func insertDeclsAfter(ctx context.Context, snapshot *cache.Snapshot, mp *metadata.Package, fset *token.FileSet, sym types.Object, emit emitter) (*token.FileSet, *analysis.SuggestedFix, error) { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
declPGF, _, err := parseFull(ctx, snapshot, fset, sym) |
| 70 |
if err != nil { |
| 71 |
return nil, nil, fmt.Errorf("failed to parse file %q declaring implementation symbol: %w", declPGF.URI, err) |
| 72 |
} |
| 73 |
if declPGF.Fixed() { |
| 74 |
return nil, nil, fmt.Errorf("file contains parse errors: %s", declPGF.URI) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
declMeta := findFileInDeps(snapshot, mp, declPGF.URI) |
| 80 |
if declMeta == nil { |
| 81 |
return nil, nil, bug.Errorf("can't find metadata for file %s among dependencies of %s", declPGF.URI, mp) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
importEnv := make(map[ImportPath]string) |
| 88 |
for _, imp := range declPGF.File.Imports { |
| 89 |
importPath := metadata.UnquoteImportPath(imp) |
| 90 |
var name string |
| 91 |
if imp.Name != nil { |
| 92 |
name = imp.Name.Name |
| 93 |
if name == "_" { |
| 94 |
continue |
| 95 |
} else if name == "." { |
| 96 |
name = "" |
| 97 |
} |
| 98 |
} else { |
| 99 |
|
| 100 |
|
| 101 |
mp := snapshot.Metadata(declMeta.DepsByImpPath[importPath]) |
| 102 |
if mp == nil { |
| 103 |
continue |
| 104 |
} |
| 105 |
name = string(mp.Name) |
| 106 |
} |
| 107 |
importEnv[importPath] = name |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
type newImport struct{ name, importPath string } |
| 123 |
var newImports []newImport |
| 124 |
qual := func(pkg *types.Package) string { |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if pkg.Path() == sym.Pkg().Path() { |
| 129 |
return "" |
| 130 |
} |
| 131 |
|
| 132 |
importPath := ImportPath(pkg.Path()) |
| 133 |
name, ok := importEnv[importPath] |
| 134 |
if !ok { |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
name = pkg.Name() |
| 142 |
importEnv[importPath] = name |
| 143 |
new := newImport{importPath: string(importPath)} |
| 144 |
|
| 145 |
|
| 146 |
if name != pathpkg.Base(trimVersionSuffix(new.importPath)) { |
| 147 |
new.name = name |
| 148 |
} |
| 149 |
newImports = append(newImports, new) |
| 150 |
} |
| 151 |
return name |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
insertOffset, err := safetoken.Offset(declPGF.Tok, declPGF.File.End()) |
| 157 |
if err != nil { |
| 158 |
return nil, nil, bug.Errorf("internal error: end position outside file bounds: %v", err) |
| 159 |
} |
| 160 |
symOffset, err := safetoken.Offset(fset.File(sym.Pos()), sym.Pos()) |
| 161 |
if err != nil { |
| 162 |
return nil, nil, bug.Errorf("internal error: finding type decl offset: %v", err) |
| 163 |
} |
| 164 |
for _, decl := range declPGF.File.Decls { |
| 165 |
declEndOffset, err := safetoken.Offset(declPGF.Tok, decl.End()) |
| 166 |
if err != nil { |
| 167 |
return nil, nil, bug.Errorf("internal error: finding decl offset: %v", err) |
| 168 |
} |
| 169 |
if declEndOffset > symOffset { |
| 170 |
insertOffset = declEndOffset |
| 171 |
break |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
var buf bytes.Buffer |
| 177 |
input := declPGF.Mapper.Content |
| 178 |
buf.Write(input[:insertOffset]) |
| 179 |
buf.WriteByte('\n') |
| 180 |
err = emit(&buf, qual) |
| 181 |
if err != nil { |
| 182 |
return nil, nil, err |
| 183 |
} |
| 184 |
buf.Write(input[insertOffset:]) |
| 185 |
|
| 186 |
|
| 187 |
fset = token.NewFileSet() |
| 188 |
newF, err := parser.ParseFile(fset, declPGF.URI.Path(), buf.Bytes(), parser.ParseComments|parser.SkipObjectResolution) |
| 189 |
if err != nil { |
| 190 |
return nil, nil, fmt.Errorf("could not reparse file: %w", err) |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
for _, imp := range newImports { |
| 195 |
astutil.AddNamedImport(fset, newF, imp.name, imp.importPath) |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
var output bytes.Buffer |
| 200 |
if err := format.Node(&output, fset, newF); err != nil { |
| 201 |
return nil, nil, fmt.Errorf("format.Node: %w", err) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
diffs := diff.Bytes(input, output.Bytes()) |
| 206 |
return tokeninternal.FileSetFor(declPGF.Tok), |
| 207 |
&analysis.SuggestedFix{TextEdits: diffToTextEdits(declPGF.Tok, diffs)}, |
| 208 |
nil |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
func diffToTextEdits(tok *token.File, diffs []diff.Edit) []analysis.TextEdit { |
| 213 |
edits := make([]analysis.TextEdit, 0, len(diffs)) |
| 214 |
for _, edit := range diffs { |
| 215 |
edits = append(edits, analysis.TextEdit{ |
| 216 |
Pos: tok.Pos(edit.Start), |
| 217 |
End: tok.Pos(edit.End), |
| 218 |
NewText: []byte(edit.New), |
| 219 |
}) |
| 220 |
} |
| 221 |
return edits |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
func trimVersionSuffix(path string) string { |
| 233 |
dir, base := pathpkg.Split(path) |
| 234 |
if len(base) > 1 && base[0] == 'v' && strings.Trim(base[1:], "0123456789") == "" { |
| 235 |
return dir |
| 236 |
} |
| 237 |
return path |
| 238 |
} |