Skip to content

gopls/internal: stubcalledfunction: improve logic of selecting insert position #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gopls/internal/golang/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func stubMissingCalledFunctionFixer(ctx context.Context, snapshot *cache.Snapsho
if si == nil {
return nil, nil, fmt.Errorf("invalid type request")
}
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.Receiver.Obj(), si.Emit)
return insertDeclsAfter(ctx, snapshot, pkg.Metadata(), si.Fset, si.After, si.Emit)
}

// An emitter writes new top-level declarations into an existing
Expand Down
17 changes: 17 additions & 0 deletions gopls/internal/golang/stubmethods/stubcalledfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CallStubInfo struct {
Fset *token.FileSet // the FileSet used to type-check the types below
Receiver typesinternal.NamedOrAlias // the method's receiver type
MethodName string
After types.Object // decl after which to insert the new decl
pointer bool
info *types.Info
path []ast.Node // path enclosing the CallExpr
Expand Down Expand Up @@ -58,10 +59,26 @@ func GetCallStubInfo(fset *token.FileSet, info *types.Info, path []ast.Node, pos
if recv.Parent() != recv.Pkg().Scope() {
return nil
}

after := types.Object(recv)
// If the enclosing function declaration is a method declaration,
// and matches the receiver type of the diagnostic,
// insert after the enclosing method.
decl, ok := path[len(path)-2].(*ast.FuncDecl)
if ok && decl.Recv != nil {
if len(decl.Recv.List) != 1 {
return nil
}
mrt := info.TypeOf(decl.Recv.List[0].Type)
if mrt != nil && types.Identical(types.Unalias(typesinternal.Unpointer(mrt)), recv.Type()) {
after = info.ObjectOf(decl.Name)
}
}
return &CallStubInfo{
Fset: fset,
Receiver: recvType,
MethodName: s.Sel.Name,
After: after,
pointer: pointer,
path: path[i:],
info: info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ func fun() {
+func (t TypeDeclInOtherFile) other(i int) {
+ panic("unimplemented")
+}
-- should_insert_after.go --
package fromcallbasic

type HasMethod struct{}

func (h *HasMethod) m() {
h.should_insert_after() //@quickfix("should_insert_after", re"has no field or method", insert)
}
-- @insert/should_insert_after.go --
@@ -8 +8,4 @@
+
+func (h *HasMethod) should_insert_after() {
+ panic("unimplemented")
+}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
This test checks the return type of the generated missing method based on CallExpr.

-- basic_stub.go --
package fromcallreturns

-- param.go --
package fromcallreturns

Expand Down