This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneral.go
81 lines (67 loc) · 2.38 KB
/
general.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package lsp
import (
"context"
"encoding/json"
"errors"
"log/slog"
"path/filepath"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
)
func (s *server) DidOpen(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params protocol.DidOpenTextDocumentParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return sendParseError(ctx, reply, err)
}
uri := params.TextDocument.URI
file := &GnoFile{
URI: uri,
Src: []byte(params.TextDocument.Text),
}
s.snapshot.file.Set(uri.Filename(), file)
slog.Info("open " + string(params.TextDocument.URI.Filename()))
s.UpdateCache(filepath.Dir(string(params.TextDocument.URI.Filename())))
notification := s.publishDiagnostics(ctx, s.conn, file)
return reply(ctx, notification, nil)
}
func (s *server) DidClose(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params protocol.DidChangeTextDocumentParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return sendParseError(ctx, reply, err)
}
slog.Info("close" + string(params.TextDocument.URI.Filename()))
return reply(ctx, s.conn.Notify(ctx, protocol.MethodTextDocumentDidClose, nil), nil)
}
func (s *server) DidChange(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params protocol.DidChangeTextDocumentParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return sendParseError(ctx, reply, err)
}
uri := params.TextDocument.URI
_, ok := s.snapshot.Get(uri.Filename())
if !ok {
return reply(ctx, nil, errors.New("snapshot not found"))
}
file := &GnoFile{
URI: uri,
Src: []byte(params.ContentChanges[0].Text),
}
s.snapshot.file.Set(uri.Filename(), file)
slog.Info("change " + string(params.TextDocument.URI.Filename()))
return reply(ctx, nil, nil)
}
func (s *server) DidSave(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params protocol.DidSaveTextDocumentParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return sendParseError(ctx, reply, err)
}
uri := params.TextDocument.URI
file, ok := s.snapshot.Get(uri.Filename())
if !ok {
return reply(ctx, nil, errors.New("snapshot not found"))
}
slog.Info("save " + string(uri.Filename()))
s.UpdateCache(filepath.Dir(string(params.TextDocument.URI.Filename())))
notification := s.publishDiagnostics(ctx, s.conn, file)
return reply(ctx, notification, nil)
}