Skip to content

Commit 0abde90

Browse files
committed
move logic to stubcalledfunc.go
1 parent af0a75d commit 0abde90

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

gopls/internal/golang/stub.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"context"
1010
"fmt"
11-
"go/ast"
1211
"go/format"
1312
"go/parser"
1413
"go/token"
@@ -26,7 +25,6 @@ import (
2625
"golang.org/x/tools/gopls/internal/util/safetoken"
2726
"golang.org/x/tools/internal/diff"
2827
"golang.org/x/tools/internal/tokeninternal"
29-
"golang.org/x/tools/internal/typesinternal"
3028
)
3129

3230
// stubMissingInterfaceMethodsFixer returns a suggested fix to declare the missing
@@ -50,24 +48,7 @@ func stubMissingCalledFunctionFixer(ctx context.Context, snapshot *cache.Snapsho
5048
if si == nil {
5149
return nil, nil, fmt.Errorf("invalid type request")
5250
}
53-
var sym types.Object
54-
sym = si.Receiver.Obj()
55-
// If the enclosing function declaration is a method declaration,
56-
// and matches the receiver type of the diagnostic,
57-
// insert after the enclosing method.
58-
if len(si.Path) > 1 {
59-
decl, ok := si.Path[len(si.Path)-2].(*ast.FuncDecl)
60-
if ok && decl.Recv != nil {
61-
for _, mr := range decl.Recv.List {
62-
mrt := si.Info.TypeOf(mr.Type)
63-
if mrt != nil && types.Unalias(typesinternal.Unpointer(mrt)) == sym.Type() {
64-
sym = si.Info.ObjectOf(decl.Name)
65-
break
66-
}
67-
}
68-
}
69-
}
70-
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, sym, si.Emit)
51+
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.After, si.Emit)
7152
}
7253

7354
// An emitter writes new top-level declarations into an existing

gopls/internal/golang/stubmethods/stubcalledfunc.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ type CallStubInfo struct {
2626
Fset *token.FileSet // the FileSet used to type-check the types below
2727
Receiver typesinternal.NamedOrAlias // the method's receiver type
2828
MethodName string
29+
After types.Object // decl after which to insert the new decl
2930
pointer bool
30-
Info *types.Info
31-
Path []ast.Node // path enclosing the CallExpr
31+
info *types.Info
32+
path []ast.Node // path enclosing the CallExpr
3233
}
3334

3435
// GetCallStubInfo extracts necessary information to generate a method definition from
@@ -58,13 +59,29 @@ func GetCallStubInfo(fset *token.FileSet, info *types.Info, path []ast.Node, pos
5859
if recv.Parent() != recv.Pkg().Scope() {
5960
return nil
6061
}
62+
63+
after := types.Object(recv)
64+
// If the enclosing function declaration is a method declaration,
65+
// and matches the receiver type of the diagnostic,
66+
// insert after the enclosing method.
67+
decl, ok := path[len(path)-2].(*ast.FuncDecl)
68+
if ok && decl.Recv != nil {
69+
if len(decl.Recv.List) != 1 {
70+
return nil
71+
}
72+
mrt := info.TypeOf(decl.Recv.List[0].Type)
73+
if mrt != nil && types.Identical(types.Unalias(typesinternal.Unpointer(mrt)), recv.Type()) {
74+
after = info.ObjectOf(decl.Name)
75+
}
76+
}
6177
return &CallStubInfo{
6278
Fset: fset,
6379
Receiver: recvType,
6480
MethodName: s.Sel.Name,
81+
After: after,
6582
pointer: pointer,
66-
Path: path[i:],
67-
Info: info,
83+
path: path[i:],
84+
info: info,
6885
}
6986
}
7087
}
@@ -74,7 +91,7 @@ func GetCallStubInfo(fset *token.FileSet, info *types.Info, path []ast.Node, pos
7491
// Emit writes to out the missing method based on type info of si.Receiver and CallExpr.
7592
func (si *CallStubInfo) Emit(out *bytes.Buffer, qual types.Qualifier) error {
7693
params := si.collectParams()
77-
rets := typesFromContext(si.Info, si.Path, si.Path[0].Pos())
94+
rets := typesFromContext(si.info, si.path, si.path[0].Pos())
7895
recv := si.Receiver.Obj()
7996
// Pointer receiver?
8097
var star string
@@ -159,9 +176,9 @@ func (si *CallStubInfo) collectParams() []param {
159176
params = append(params, p)
160177
}
161178

162-
args := si.Path[0].(*ast.CallExpr).Args
179+
args := si.path[0].(*ast.CallExpr).Args
163180
for _, arg := range args {
164-
t := si.Info.TypeOf(arg)
181+
t := si.info.TypeOf(arg)
165182
switch t := t.(type) {
166183
// This is the case where another function call returning multiple
167184
// results is used as an argument.

0 commit comments

Comments
 (0)