Skip to content
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

Hover for types #602

Merged
merged 3 commits into from
Sep 5, 2022
Merged
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
45 changes: 36 additions & 9 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,59 @@ package lsp
import (
"bytes"
"context"
"fmt"
"strings"

"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/lsp/protocol"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/ast"
"github.com/nokia/ntt/ttcn3/printer"
)

func getSignature(def *ttcn3.Definition) string {
var sig bytes.Buffer
var prefix = ""
fh := fs.Open(def.Filename())
content, _ := fh.Bytes()
switch node := def.Node.(type) {
case *ast.FuncDecl:
sig.WriteString(node.Kind.Lit + " " + node.Name.String())
printer.Print(&sig, def.FileSet, node.Params)
sig.Write(content[node.Params.Pos()-1 : node.Params.End()])
if node.RunsOn != nil {
sig.WriteString("\n ")
printer.Print(&sig, def.FileSet, node.RunsOn)
sig.Write(content[node.RunsOn.Pos()-1 : node.RunsOn.End()])
}
if node.System != nil {
sig.WriteString("\n ")
printer.Print(&sig, def.FileSet, node.System)
sig.Write(content[node.System.Pos()-1 : node.System.End()])
}
if node.Return != nil {
sig.WriteString("\n ")
printer.Print(&sig, def.FileSet, node.Return)
sig.Write(content[node.Return.Pos()-1 : node.Return.End()])
}
case *ast.ValueDecl, *ast.TemplateDecl, *ast.FormalPar:
printer.Print(&sig, def.FileSet, node)
case *ast.ValueDecl, *ast.TemplateDecl, *ast.FormalPar, *ast.StructTypeDecl, *ast.ComponentTypeDecl, *ast.EnumTypeDecl, *ast.PortTypeDecl:
sig.Write(content[def.Node.Pos()-1 : def.Node.End()])
case *ast.Field:
if parent := def.ParentOf(node); parent != nil {
if _, ok := parent.(*ast.SubTypeDecl); ok {
prefix = "type "
}
}
sig.Write(content[def.Node.Pos()-1 : def.Node.End()])
case *ast.Module:
fmt.Fprintf(&sig, "module %s\n", node.Name)
default:
log.Debugf("getSignature: unknown Type:%T\n", node)
}
return sig.String()
return prefix + sig.String()
}

func mdLinkForNode(def *ttcn3.Definition) string {
var mdLink bytes.Buffer
p := def.Position(def.Node.Pos())
fmt.Fprintf(&mdLink, "[module %s](%s#L%dC%d)", def.ModuleOf(def.Node).Name.String(), def.Filename(), p.Line, p.Column)
return mdLink.String()
}

func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
Expand All @@ -42,6 +65,7 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
col = int(params.Position.Character) + 1
comment string
signature string
posRef string
defFound = false
)

Expand All @@ -60,6 +84,9 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
// make line breaks conform to markdown spec
comment = strings.ReplaceAll(firstTok.Comments(), "\n", " \n")
signature = getSignature(def)
if tree.Root != def.Root {
posRef = "\n - - -\n" + mdLinkForNode(def)
}
}
}
if !defFound {
Expand All @@ -73,7 +100,7 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
}
}
}
hoverContents := protocol.MarkupContent{Kind: "markdown", Value: "```typescript\n" + string(signature) + "\n```\n - - -\n" + comment}
hoverContents := protocol.MarkupContent{Kind: "markdown", Value: "```typescript\n" + string(signature) + "\n```\n - - -\n" + comment + posRef}
hover := &protocol.Hover{Contents: hoverContents}

return hover, nil
Expand Down