Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Handle hover on func and method decl
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Mar 30, 2024
1 parent 75cca25 commit afdbefe
Showing 1 changed file with 83 additions and 2 deletions.
85 changes: 83 additions & 2 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2

// Get path enclosing
path := pathEnclosingObjNode(pgf.File, token.Pos(offset))
if len(path) < 1 {
if len(path) < 2 {
return reply(ctx, nil, nil)
}

Expand All @@ -76,7 +76,16 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
offset,
)
if tv == nil || tv.Type == nil {
break
var body string
switch t := path[1].(type) {
case *ast.FuncDecl:
if t.Recv != nil {
return hoverMethodDecl(ctx, reply, params, pkg, i, t)
}
return hoverFuncDecl(ctx, reply, params, pkg, i, t)
default:
return reply(ctx, nil, nil)
}
}
typeStr := tv.Type.String()
m := mode(*tv)
Expand Down Expand Up @@ -279,6 +288,78 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
return reply(ctx, nil, nil)
}

func hoverMethodDecl(ctx context.Context, reply jsonrpc2.Replier, params protocol.HoverParams, pkg *Package, i *ast.Ident, decl *ast.FuncDecl) error {
if decl.Recv.NumFields() != 1 || decl.Recv.List[0].Type == nil {
reply(ctx, nil, nil)
}

var key string
switch rt := decl.Recv.List[0].Type.(type) {
case *ast.StarExpr:
key = fmt.Sprintf("%s", rt.X)
case *ast.Ident:
key = fmt.Sprintf("%s", rt.Name)
default:
reply(ctx, nil, nil)
}

methods, ok := pkg.Methods.Get(key)
if !ok {
reply(ctx, nil, nil)
}

var header, body string
for _, m := range methods {
if m.Name == i.Name {
header = m.Signature
body = m.Doc
break
}
}

if header == "" {
return reply(ctx, nil, nil)
}

return reply(ctx, protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: FormatHoverContent(header, body),
},
Range: posToRange(
int(params.Position.Line),
[]int{int(i.Pos()), int(i.End())},
),
}, nil)
}

// TODO: handle var doc
func hoverFuncDecl(ctx context.Context, reply jsonrpc2.Replier, params protocol.HoverParams, pkg *Package, i *ast.Ident, decl *ast.FuncDecl) error {
var header, body string
for _, s := range pkg.Symbols {
if s.Name == i.Name {
header = s.Signature
body = s.Doc
break
}
}

if header == "" {
return reply(ctx, nil, nil)
}

return reply(ctx, protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.Markdown,
Value: FormatHoverContent(header, body),
},
Range: posToRange(
int(params.Position.Line),
[]int{int(i.Pos()), int(i.End())},
),
}, nil)
}

// TODO: handle var doc
func hoverPackageLevelValue(ctx context.Context, reply jsonrpc2.Replier, params protocol.HoverParams, pkg *Package, i *ast.Ident, tv *types.TypeAndValue, mode, typeStr string, isPackageLevelGlobal bool) error {
var header, body string
Expand Down

0 comments on commit afdbefe

Please sign in to comment.