Skip to content

Commit

Permalink
Merge pull request #703 from mliszcz/fix-unexpected-characters-in-hover
Browse files Browse the repository at this point in the history
Fix unexpected characters in hover output
  • Loading branch information
moosq authored Oct 4, 2023
2 parents 19df547 + ac43031 commit bd93cad
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ func getSignature(def *ttcn3.Node) string {
switch node := def.Node.(type) {
case *syntax.FuncDecl:
sig.WriteString(node.Kind.String() + " " + node.Name.String())
sig.Write(content[node.Params.Pos()-1 : node.Params.End()])
sig.Write(content[node.Params.Pos() : node.Params.End()])
if node.RunsOn != nil {
sig.WriteString("\n ")
sig.Write(content[node.RunsOn.Pos()-1 : node.RunsOn.End()])
sig.Write(content[node.RunsOn.Pos() : node.RunsOn.End()])
}
if node.System != nil {
sig.WriteString("\n ")
sig.Write(content[node.System.Pos()-1 : node.System.End()])
sig.Write(content[node.System.Pos() : node.System.End()])
}
if node.Return != nil {
sig.WriteString("\n ")
sig.Write(content[node.Return.Pos()-1 : node.Return.End()])
sig.Write(content[node.Return.Pos() : node.Return.End()])
}
case *syntax.ValueDecl, *syntax.TemplateDecl, *syntax.FormalPar, *syntax.StructTypeDecl, *syntax.ComponentTypeDecl, *syntax.EnumTypeDecl, *syntax.PortTypeDecl:
sig.Write(content[def.Node.Pos()-1 : def.Node.End()])
Expand All @@ -85,6 +85,10 @@ func getSignature(def *ttcn3.Node) string {
}

func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
return ProcessHover(params, &s.db, s.clientCapability.HoverContent)
}

func ProcessHover(params *protocol.HoverParams, db *ttcn3.DB, capability HoverContentProvider) (*protocol.Hover, error) {
var (
file = string(params.TextDocument.URI.SpanURI())
line = int(params.Position.Line) + 1
Expand All @@ -101,13 +105,13 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
return nil, nil
}

for _, def := range tree.LookupWithDB(x, &s.db) {
for _, def := range tree.LookupWithDB(x, db) {
defFound = true

comment = syntax.Doc(def.Node)
signature = getSignature(def)
if tree.Root != def.Root {
posRef = s.clientCapability.HoverContent.LinkForNode(def)
posRef = capability.LinkForNode(def)
}
}
if !defFound {
Expand All @@ -122,7 +126,7 @@ func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*prot
}
}

hoverContents := s.clientCapability.HoverContent.Print(signature, comment, posRef)
hoverContents := capability.Print(signature, comment, posRef)
hover := &protocol.Hover{Contents: hoverContents}

return hover, nil
Expand Down
84 changes: 84 additions & 0 deletions internal/lsp/hover_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package lsp_test

import (
"fmt"
"testing"

"github.com/nokia/ntt/internal/fs"
"github.com/nokia/ntt/internal/lsp"
"github.com/nokia/ntt/internal/lsp/protocol"
"github.com/nokia/ntt/ttcn3"
"github.com/stretchr/testify/assert"
)

func TestPlainTextHoverForFunction(t *testing.T) {
actual := testHover(t, `
module Test {
function myfunc(integer x)
runs on Component
system System
return integer
{
return x;
}
}`,
protocol.Position{Line: 2, Character: 25},
&lsp.PlainTextHover{})

expected :=
"function myfunc(integer x)\n" +
" runs on Component\n" +
" system System\n" +
" return integer\n"

assert.Equal(t, expected, actual.Contents.Value)
}

func TestMarkdownHoverForFunction(t *testing.T) {
actual := testHover(t, `
module Test {
function myfunc(integer x)
runs on Component
system System
return integer
{
return x;
}
}`,
protocol.Position{Line: 2, Character: 25},
&lsp.MarkdownHover{})

expected :=
"```typescript\n" +
"function myfunc(integer x)\n" +
" runs on Component\n" +
" system System\n" +
" return integer\n" +
"```\n" +
" - - -\n" +
"\n" +
" - - -\n"

assert.Equal(t, expected, actual.Contents.Value)
}

func testHover(t *testing.T, text string, position protocol.Position, capability lsp.HoverContentProvider) *protocol.Hover {
t.Helper()

file := fmt.Sprintf("%s.ttcn3", t.Name())
fs.SetContent(file, []byte(text))

params := protocol.HoverParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
Position: position,
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.DocumentURI(file),
},
},
}

hover, err := lsp.ProcessHover(&params, &ttcn3.DB{}, capability)
assert.Equal(t, err, nil)

return hover
}

0 comments on commit bd93cad

Please sign in to comment.