Skip to content

Commit

Permalink
refactor(yamlls-hover): split large method
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Dec 8, 2023
1 parent 57020da commit beed62f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
53 changes: 32 additions & 21 deletions internal/adapter/yamlls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,49 @@ import (
lsp "go.lsp.dev/protocol"
)

// Calls the Completion method of yamlls to get a fitting hover response
// TODO: clarify why the hover method of yamlls can't be used
func (yamllsConnector Connector) CallHover(params lsp.HoverParams, word string) *lsp.Hover {
// Calls the Hover method of yamlls to get a fitting hover response
// If hover returns nothing appropriate, calls yamlls for completions
func (yamllsConnector Connector) CallHover(ctx context.Context, params lsp.HoverParams, word string) (*lsp.Hover, error) {
if yamllsConnector.Conn == nil {
return &lsp.Hover{}
return &lsp.Hover{}, nil
}

hoverResponse, err := (yamllsConnector).getHoverFromHover(ctx, params)
if err != nil {
return hoverResponse, err
}

if hoverResponse.Contents.Value != "" {
return hoverResponse, nil
}
return (yamllsConnector).getHoverFromCompletion(ctx, params, word)
}

func (yamllsConnector Connector) getHoverFromHover(ctx context.Context, params lsp.HoverParams) (*lsp.Hover, error) {

var hoverResponse = reflect.New(reflect.TypeOf(lsp.Hover{})).Interface()
_, err := (*yamllsConnector.Conn).Call(ctx, lsp.MethodTextDocumentHover, params, hoverResponse)
if err != nil {
logger.Error("Error calling yamlls for hover", err)
return &lsp.Hover{}, err
}
logger.Debug("Got hover from yamlls", hoverResponse.(*lsp.Hover).Contents.Value)
return hoverResponse.(*lsp.Hover), nil
}

func (yamllsConnector Connector) getHoverFromCompletion(ctx context.Context, params lsp.HoverParams, word string) (*lsp.Hover, error) {
var (
err error
documentation string
hoverResponse = reflect.New(reflect.TypeOf(lsp.Hover{})).Interface()
completionResponse = reflect.New(reflect.TypeOf(lsp.CompletionList{})).Interface()
completionParams = lsp.CompletionParams{
TextDocumentPositionParams: params.TextDocumentPositionParams,
}
)

_, err := (*yamllsConnector.Conn).Call(context.Background(), lsp.MethodTextDocumentHover, completionParams, hoverResponse)
if err != nil {
logger.Error("Error calling yamlls for hover", err)
return &lsp.Hover{}
}

logger.Debug("Got hover from yamlls", hoverResponse.(*lsp.Hover).Contents.Value)

if hoverResponse.(*lsp.Hover).Contents.Value != "" {
return hoverResponse.(*lsp.Hover)
}

_, err = (*yamllsConnector.Conn).Call(context.Background(), lsp.MethodTextDocumentCompletion, completionParams, completionResponse)
_, err = (*yamllsConnector.Conn).Call(ctx, lsp.MethodTextDocumentCompletion, completionParams, completionResponse)
if err != nil {
logger.Error("Error calling yamlls for Completion", err)
return &lsp.Hover{}
return &lsp.Hover{}, err
}

for _, completionItem := range completionResponse.(*lsp.CompletionList).Items {
Expand All @@ -50,5 +61,5 @@ func (yamllsConnector Connector) CallHover(params lsp.HoverParams, word string)
}

response := util.BuildHoverResponse(documentation, lsp.Range{})
return &response
return &response, nil
}
2 changes: 1 addition & 1 deletion internal/handler/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (h *langHandler) handleHover(ctx context.Context, reply jsonrpc2.Replier, r
if len(word) > 2 && string(word[len(word)-1]) == ":" {
word = word[0 : len(word)-1]
}
var response = *h.yamllsConnector.CallHover(params, word)
var response, err = h.yamllsConnector.CallHover(ctx, params, word)
return reply(ctx, response, err)
}
if pt == "function_call" && ct == "identifier" {
Expand Down

0 comments on commit beed62f

Please sign in to comment.