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

Implement show hover #577

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion internal/lsp/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
DocumentSymbolProvider: true,
WorkspaceSymbolProvider: false,
FoldingRangeProvider: false,
HoverProvider: false,
HoverProvider: true,
DocumentHighlightProvider: false,
DocumentLinkProvider: protocol.DocumentLinkOptions{},
ReferencesProvider: true,
Expand Down
31 changes: 31 additions & 0 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lsp

import (
"context"
"fmt"
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/lsp/protocol"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/ast"
)

func (s *Server) hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
var (
file = string(params.TextDocument.URI.SpanURI())
line = int(params.Position.Line) + 1
col = int(params.Position.Character) + 1
)

tree := ttcn3.ParseFile(file)
x := tree.ExprAt(tree.Pos(line, col))
if x == nil {
log.Debug(fmt.Sprintf("No expression at %s:%d:%d\n", file, line, col))
}

comment := ast.FirstToken(x).Comments()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the first token of the cursor position is sufficient for an initial hover implementation.

A nice followup PR would be using the first token of the parent ast.Stmt, *ast.ModuleDef, *ast.Module.


hoverContents := protocol.MarkupContent{Kind: "markdown", Value: comment}
hover := &protocol.Hover{Contents: hoverContents}

return hover, nil
}
4 changes: 2 additions & 2 deletions internal/lsp/server_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (s *Server) Formatting(context.Context, *protocol.DocumentFormattingParams)
return nil, notImplemented("Formatting")
}

func (s *Server) Hover(context.Context, *protocol.HoverParams) (*protocol.Hover, error) {
return nil, notImplemented("Hover")
func (s *Server) Hover(ctx context.Context, params *protocol.HoverParams) (*protocol.Hover, error) {
return s.hover(ctx, params)
}

func (s *Server) Implementation(context.Context, *protocol.ImplementationParams) (interface{}, error) {
Expand Down