From 08f772b7d023e1542ee5d3c8503dc7c394e00624 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 12 Aug 2020 01:15:37 -0400 Subject: [PATCH] Better hover parsing --- internal/lsp/requests.go | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/lsp/requests.go b/internal/lsp/requests.go index b30f0a30..faf29a79 100644 --- a/internal/lsp/requests.go +++ b/internal/lsp/requests.go @@ -19,6 +19,21 @@ type RPCHover struct { Result lsp.Hover `json:"result"` } +type hoverAlternate struct { + // Contents is the hover's content + Contents []interface{} `json:"contents"` + + // Range an optional range is a range inside a text document + // that is used to visualize a hover, e.g. by changing the background color. + Range lsp.Range `json:"range,omitempty"` +} + +type RPCHoverAlternate struct { + RPCVersion string `json:"jsonrpc"` + ID int `json:"id"` + Result hoverAlternate `json:"result"` +} + func Position(x, y int) lsp.Position { return lsp.Position{ Line: float64(y), @@ -83,9 +98,23 @@ func (s *Server) Hover(filename string, pos lsp.Position) (string, error) { var r RPCHover err = json.Unmarshal(resp, &r) + if err == nil { + return r.Result.Contents.Value, nil + } + + var ra RPCHoverAlternate + err = json.Unmarshal(resp, &ra) if err != nil { return "", err } - return r.Result.Contents.Value, nil + for _, c := range ra.Result.Contents { + switch t := c.(type) { + case string: + return t, nil + case map[string]string: + return t["value"], nil + } + } + return "", nil }