Skip to content

Commit cbd92b1

Browse files
xzbdmwgopherbot
authored andcommitted
gopls/internal: stubcalledfunction: improve logic of selecting insert position
This CL adds the logic to insert generated method stubs immediately after the enclosing method declaration when the receiver types match. I often find myself move the generated code to the position after current method immediately, and this added logic is more consistent with undeclared.go's generate missing function quickfix. Related CL 617619 Updates golang/go#69692 Change-Id: Ia07422a0da7ee38ce648508dbb3e392f3dc8c93d GitHub-Last-Rev: 0abde90 GitHub-Pull-Request: #537 Reviewed-on: https://go-review.googlesource.com/c/tools/+/621841 Reviewed-by: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 45a28e1 commit cbd92b1

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

gopls/internal/golang/stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func stubMissingCalledFunctionFixer(ctx context.Context, snapshot *cache.Snapsho
4848
if si == nil {
4949
return nil, nil, fmt.Errorf("invalid type request")
5050
}
51-
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.Receiver.Obj(), si.Emit)
51+
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.After, si.Emit)
5252
}
5353

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

gopls/internal/golang/stubmethods/stubcalledfunc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ 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
3031
info *types.Info
3132
path []ast.Node // path enclosing the CallExpr
@@ -58,10 +59,26 @@ 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,
6683
path: path[i:],
6784
info: info,

gopls/internal/test/marker/testdata/quickfix/stubmethods/fromcall_basic.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,17 @@ func fun() {
5252
+func (t TypeDeclInOtherFile) other(i int) {
5353
+ panic("unimplemented")
5454
+}
55+
-- should_insert_after.go --
56+
package fromcallbasic
57+
58+
type HasMethod struct{}
59+
60+
func (h *HasMethod) m() {
61+
h.should_insert_after() //@quickfix("should_insert_after", re"has no field or method", insert)
62+
}
63+
-- @insert/should_insert_after.go --
64+
@@ -8 +8,4 @@
65+
+
66+
+func (h *HasMethod) should_insert_after() {
67+
+ panic("unimplemented")
68+
+}

gopls/internal/test/marker/testdata/quickfix/stubmethods/fromcall_returns.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
This test checks the return type of the generated missing method based on CallExpr.
22

3-
-- basic_stub.go --
4-
package fromcallreturns
5-
63
-- param.go --
74
package fromcallreturns
85

0 commit comments

Comments
 (0)