LSP-server-example
61 строка · 1.3 Кб
1package handlers
2
3import (
4"encoding/json"
5"log/slog"
6
7"lsp-server/internal/protocol"
8)
9
10const HoverMethod = "textDocument/hover"
11
12type Hover struct {
13fileURIToContents *map[string][]string
14}
15
16func NewHover(fileURIToContents *map[string][]string) *Hover {
17return &Hover{fileURIToContents: fileURIToContents}
18}
19
20func (h Hover) Call(params json.RawMessage) (any, error) {
21slog.Info("received hover method")
22hParams, err := h.parseParams(params)
23if err != nil {
24slog.Error("Error to parse hover params")
25return nil, err
26}
27slog.Debug("hover params", slog.Any("params", *hParams))
28
29hoverItem := h.createHoverItem(hParams)
30
31return hoverItem, nil
32}
33
34func (h Hover) createHoverItem(hParams *protocol.HoverParams) *protocol.Hover {
35hoverItem := &protocol.Hover{
36Contents: protocol.MarkupContent{
37Kind: protocol.Markdown,
38Value: "some hover",
39},
40Range: protocol.Range{
41Start: protocol.Position{
42Line: hParams.Position.Line,
43Character: hParams.Position.Character,
44},
45End: protocol.Position{
46Line: hParams.Position.Line,
47Character: hParams.Position.Character,
48},
49},
50}
51return hoverItem
52}
53
54func (h Hover) parseParams(params json.RawMessage) (*protocol.HoverParams, error) {
55var hoverParams protocol.HoverParams
56if err := json.Unmarshal(params, &hoverParams); err != nil {
57return nil, err
58}
59
60return &hoverParams, nil
61}
62