diff --git a/internal/lsp/hover.go b/internal/lsp/hover.go index 4ea61d3c..5d1bf68f 100644 --- a/internal/lsp/hover.go +++ b/internal/lsp/hover.go @@ -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) { @@ -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 ) @@ -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 { @@ -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