LSP-server-example
45 строк · 1.1 Кб
1package handlers
2
3import (
4"encoding/json"
5"log/slog"
6
7"lsp-server/internal/protocol"
8)
9
10const DidChangeTextDocumentNotification = "textDocument/didChange"
11
12type DidChange struct {
13documentUpdates chan protocol.TextDocumentItem
14}
15
16func NewDidChange(documentUpdates chan protocol.TextDocumentItem) *DidChange {
17return &DidChange{documentUpdates: documentUpdates}
18}
19
20func (d DidChange) Call(params json.RawMessage) error {
21slog.Info("received didChange notification")
22changeParams, err := d.parseParams(params)
23if err != nil {
24slog.Error("Error to parse didChange params")
25return err
26}
27slog.Debug("didChange params", slog.Any("params", *changeParams))
28
29d.documentUpdates <- protocol.TextDocumentItem{
30URI: changeParams.TextDocument.URI,
31Version: changeParams.TextDocument.Version,
32Text: changeParams.ContentChanges[0].Text,
33}
34
35return nil
36}
37
38func (d DidChange) parseParams(params json.RawMessage) (*protocol.DidChangeTextDocumentParams, error) {
39var changeParams protocol.DidChangeTextDocumentParams
40if err := json.Unmarshal(params, &changeParams); err != nil {
41return nil, err
42}
43
44return &changeParams, nil
45}
46