Skip to content

Commit

Permalink
Merge pull request #602 from moosq/hover-for-types
Browse files Browse the repository at this point in the history
Hover for types
  • Loading branch information
moosq authored Sep 5, 2022
2 parents 1c391c8 + c026706 commit 033fbe9
Showing 1 changed file with 36 additions and 9 deletions.
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

0 comments on commit 033fbe9

Please sign in to comment.