From beed62f825ee9c8a5a762f0cc9da7450461b2903 Mon Sep 17 00:00:00 2001 From: qvalentin Date: Fri, 8 Dec 2023 20:21:57 +0100 Subject: [PATCH] refactor(yamlls-hover): split large method --- internal/adapter/yamlls/hover.go | 53 +++++++++++++++++++------------- internal/handler/hover.go | 2 +- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/internal/adapter/yamlls/hover.go b/internal/adapter/yamlls/hover.go index 561c93f6..41627990 100644 --- a/internal/adapter/yamlls/hover.go +++ b/internal/adapter/yamlls/hover.go @@ -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 { @@ -50,5 +61,5 @@ func (yamllsConnector Connector) CallHover(params lsp.HoverParams, word string) } response := util.BuildHoverResponse(documentation, lsp.Range{}) - return &response + return &response, nil } diff --git a/internal/handler/hover.go b/internal/handler/hover.go index 6adf9f11..b0664b42 100644 --- a/internal/handler/hover.go +++ b/internal/handler/hover.go @@ -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" {