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

Commit

Permalink
Handle hover over locally declared types
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Mar 29, 2024
1 parent e9cad3e commit 97f7866
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/lsp/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ func mode(tv types.TypeAndValue) string {
return "mapindex"
case tv.IsValue():
return "value"
case tv.IsType():
return "type"
default:
return "unknown"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func PackageFromDir(path string, onlyExports bool) (*Package, error) {
if t.Recv.NumFields() > 0 && t.Recv.List[0].Type != nil {
switch rt := t.Recv.List[0].Type.(type) {
case *ast.StarExpr:
k := fmt.Sprintf("*%s", rt.X)
k := fmt.Sprintf("%s", rt.X)
m := &Method{
Position: fset.Position(t.Pos()),
FileURI: getURI(absPath),
Expand Down
53 changes: 52 additions & 1 deletion internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
break
}
typeStr := tv.Type.String()
m := mode(*tv)

// local
// local type
if (strings.HasPrefix(typeStr, pkg.ImportPath) ||
strings.HasPrefix(typeStr, "*"+pkg.ImportPath)) && m == "type" {
typeStr := parseType(typeStr, pkg.ImportPath)
return hoverLocalTypes(ctx, reply, params, pkg, i, tv, typeStr)
}

// Handle builtins
if doc, ok := isBuiltin(i, tv); ok {
Expand Down Expand Up @@ -262,6 +268,45 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
return reply(ctx, nil, nil)
}

func hoverLocalTypes(ctx context.Context, reply jsonrpc2.Replier, params protocol.HoverParams, pkg *Package, i *ast.Ident, tv *types.TypeAndValue, typeName string) error {
// Look into structures
var structure *Structure
for _, st := range pkg.Structures {
if st.Name == fmt.Sprintf("%s", typeName) {
structure = st
break
}
}
if structure == nil {
return reply(ctx, nil, nil)
}
var header, body string
header = fmt.Sprintf("%s %s %s\n\n", mode(*tv), structure.Name, structure.String)

methods, ok := pkg.Methods.Get(typeName)
if ok {
body = "```gno\n"
for _, m := range methods {
if m.IsExported() {
body += fmt.Sprintf("%s\n", m.Signature)
}
}
body += "```\n"
body += structure.Doc + "\n"
}

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)
}

func hoverBuiltinTypes(ctx context.Context, reply jsonrpc2.Replier, params protocol.HoverParams, i *ast.Ident, tv *types.TypeAndValue, doc string) error {
t := tv.Type.String()
m := mode(*tv)
Expand Down Expand Up @@ -537,3 +582,9 @@ func pathEnclosingObjNode(f *ast.File, pos token.Pos) []ast.Node {
return path
}

// parseType parses the type name from full path and return
// the type as string and if it is isStar expr.
func parseType(t, importpath string) string {
return strings.TrimPrefix(strings.TrimPrefix(t, "*"), importpath+".")
}

0 comments on commit 97f7866

Please sign in to comment.