LSP-server-example
40 строк · 963.0 Байт
1package handlers
2
3import (
4"encoding/json"
5"log/slog"
6
7"lsp-server/internal/protocol"
8)
9
10const DidOpenTextDocumentNotification = "textDocument/didOpen"
11
12type DidOpen struct {
13documentUpdates chan protocol.TextDocumentItem
14}
15
16func NewDidOpen(documentUpdates chan protocol.TextDocumentItem) *DidOpen {
17return &DidOpen{documentUpdates: documentUpdates}
18}
19
20func (d DidOpen) Call(params json.RawMessage) error {
21slog.Info("received didOpen notification")
22openParams, err := d.parseParams(params)
23if err != nil {
24slog.Error("Error to parse didOpen params")
25return err
26}
27slog.Debug("didOpen params", slog.Any("params", *openParams))
28d.documentUpdates <- openParams.TextDocument
29
30return nil
31}
32
33func (d DidOpen) parseParams(params json.RawMessage) (*protocol.DidOpenTextDocumentParams, error) {
34var openParams protocol.DidOpenTextDocumentParams
35if err := json.Unmarshal(params, &openParams); err != nil {
36return nil, err
37}
38
39return &openParams, nil
40}
41