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 pathserver.go
130 lines (114 loc) · 3.32 KB
/
server.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package lsp
import (
"context"
"encoding/json"
"log/slog"
"os"
"path/filepath"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
"github.com/harry-hov/gnopls/internal/env"
"github.com/harry-hov/gnopls/internal/tools"
"github.com/harry-hov/gnopls/internal/version"
)
type server struct {
conn jsonrpc2.Conn
env *env.Env
snapshot *Snapshot
completionStore *CompletionStore
cache *Cache
formatOpt tools.FormattingOption
}
func BuildServerHandler(conn jsonrpc2.Conn, e *env.Env) jsonrpc2.Handler {
dirs := []string{}
if e.GNOROOT != "" {
dirs = append(dirs, filepath.Join(e.GNOROOT, "examples"))
dirs = append(dirs, filepath.Join(e.GNOROOT, "gnovm/stdlibs"))
}
server := &server{
conn: conn,
env: e,
snapshot: NewSnapshot(),
completionStore: InitCompletionStore(dirs),
cache: NewCache(),
formatOpt: tools.Gofumpt,
}
env.GlobalEnv = e
return jsonrpc2.ReplyHandler(server.ServerHandler)
}
func (s *server) ServerHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
switch req.Method() {
case "exit":
return s.Exit(ctx, reply, req)
case "initialize":
return s.Initialize(ctx, reply, req)
case "initialized":
return s.Initialized(ctx, reply, req)
case "shutdown":
return s.Shutdown(ctx, reply, req)
case "textDocument/didChange":
return s.DidChange(ctx, reply, req)
case "textDocument/didClose":
return s.DidClose(ctx, reply, req)
case "textDocument/didOpen":
return s.DidOpen(ctx, reply, req)
case "textDocument/didSave":
return s.DidSave(ctx, reply, req)
case "textDocument/formatting":
return s.Formatting(ctx, reply, req)
case "textDocument/hover":
return s.Hover(ctx, reply, req)
case "textDocument/completion":
return s.Completion(ctx, reply, req)
case "textDocument/definition":
return s.Definition(ctx, reply, req)
default:
return jsonrpc2.MethodNotFoundHandler(ctx, reply, req)
}
}
func (s *server) Initialize(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params protocol.InitializeParams
if err := json.Unmarshal(req.Params(), ¶ms); err != nil {
return sendParseError(ctx, reply, err)
}
return reply(ctx, protocol.InitializeResult{
ServerInfo: &protocol.ServerInfo{
Name: "gnopls",
Version: version.GetVersion(ctx),
},
Capabilities: protocol.ServerCapabilities{
TextDocumentSync: protocol.TextDocumentSyncOptions{
Change: protocol.TextDocumentSyncKindFull,
OpenClose: true,
Save: &protocol.SaveOptions{
IncludeText: true,
},
},
CompletionProvider: &protocol.CompletionOptions{
TriggerCharacters: []string{"."},
ResolveProvider: false,
},
HoverProvider: true,
ExecuteCommandProvider: &protocol.ExecuteCommandOptions{
Commands: []string{
"gnopls.version",
},
},
DefinitionProvider: true,
DocumentFormattingProvider: true,
},
}, nil)
}
func (s *server) Initialized(ctx context.Context, reply jsonrpc2.Replier, _ jsonrpc2.Request) error {
slog.Info("initialized")
return reply(ctx, nil, nil)
}
func (s *server) Shutdown(ctx context.Context, reply jsonrpc2.Replier, _ jsonrpc2.Request) error {
slog.Info("shutdown")
return reply(ctx, nil, s.conn.Close())
}
func (s *server) Exit(ctx context.Context, reply jsonrpc2.Replier, _ jsonrpc2.Request) error {
slog.Info("exit")
os.Exit(1)
return nil
}