Skip to content

Commit

Permalink
feature: code action to generate a stub function from a "no function …
Browse files Browse the repository at this point in the history
…f" type error
  • Loading branch information
xzbdmw committed Oct 6, 2024
1 parent f8f3c13 commit bd6d7e3
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 139 deletions.
10 changes: 9 additions & 1 deletion gopls/internal/golang/codeaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,21 @@ func quickFix(ctx context.Context, req *codeActionsRequest) error {
strings.HasPrefix(msg, "cannot convert") ||
strings.Contains(msg, "not implement") {
path, _ := astutil.PathEnclosingInterval(req.pgf.File, start, end)
si := stubmethods.GetStubInfo(req.pkg.FileSet(), info, path, start)
si := stubmethods.GetIfaceStubInfo(req.pkg.FileSet(), info, path, start)
if si != nil {
qf := typesutil.FileQualifier(req.pgf.File, si.Concrete.Obj().Pkg(), info)
iface := types.TypeString(si.Interface.Type(), qf)
msg := fmt.Sprintf("Declare missing methods of %s", iface)
req.addApplyFixAction(msg, fixStubMethods, req.loc)
}
} else if strings.Contains(msg, "has no field or method") {
path, _ := astutil.PathEnclosingInterval(req.pgf.File, start, end)

si := stubmethods.GetCallStubInfo(req.pkg, info, path, start)
if si != nil {
msg := fmt.Sprintf("Declare missing methods of %s", si.Receiver.Obj().Name())
req.addApplyFixAction(msg, fixMissingMethods, req.loc)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion gopls/internal/golang/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const (
fixSplitLines = "split_lines"
fixJoinLines = "join_lines"
fixStubMethods = "stub_methods"
fixMissingMethods = "missing_methods"
)

// ApplyFix applies the specified kind of suggested fix to the given
Expand Down Expand Up @@ -109,7 +110,8 @@ func ApplyFix(ctx context.Context, fix string, snapshot *cache.Snapshot, fh file
fixInvertIfCondition: singleFile(invertIfCondition),
fixSplitLines: singleFile(splitLines),
fixJoinLines: singleFile(joinLines),
fixStubMethods: stubMethodsFixer,
fixStubMethods: stubMethodsIfaceFixer,
fixMissingMethods: stubMethodsCallFixer,
}
fixer, ok := fixers[fix]
if !ok {
Expand Down
Loading

0 comments on commit bd6d7e3

Please sign in to comment.