-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
173 lines (144 loc) · 4.8 KB
/
handler.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"io"
"log"
"os"
"os/exec"
"sync"
lsp "github.com/tectiv3/go-lsp"
"github.com/tectiv3/go-lsp/jsonrpc"
"go.bug.st/json"
)
type handler struct {
lsc *lsp.Client
Diagnostics chan *lsp.PublishDiagnosticsParams
waitingForDiagnostics bool
config KeyValue
sync.Mutex
}
func (h handler) SetConfig(config KeyValue) {
h.config = config
}
func (h handler) GetDiagnosticChannel() chan *lsp.PublishDiagnosticsParams {
return h.Diagnostics
}
func (h handler) ClientRegisterCapability(context.Context, jsonrpc.FunctionLogger, *lsp.RegistrationParams) *jsonrpc.ResponseError {
//Log("ClientRegisterCapability")
//h.client.GetConnection().SendNotification("client/registerCapability", lsp.EncodeMessage(KeyValue{}))
return nil
}
// ClientUnregisterCapability
func (h handler) ClientUnregisterCapability(context.Context, jsonrpc.FunctionLogger, *lsp.UnregistrationParams) *jsonrpc.ResponseError {
return nil
}
// LogTrace
func (h handler) LogTrace(logger jsonrpc.FunctionLogger, params *lsp.LogTraceParams) {
log.Printf("LogTrace: %v", params)
}
// Progress
func (h handler) Progress(logger jsonrpc.FunctionLogger, params *lsp.ProgressParams) {
log.Printf("Progress: %v", params)
}
// WindowShowMessage
func (h handler) WindowShowMessage(logger jsonrpc.FunctionLogger, params *lsp.ShowMessageParams) {
log.Printf("WindowShowMessage: %v", params)
}
// WindowLogMessage
func (h handler) WindowLogMessage(logger jsonrpc.FunctionLogger, params *lsp.LogMessageParams) {
logger.Logf("%v", params)
}
// TelemetryEvent
func (h handler) TelemetryEvent(logger jsonrpc.FunctionLogger, msg json.RawMessage) {
log.Printf("TelemetryEvent: %v", msg)
}
// TextDocumentPublishDiagnostics
func (h handler) TextDocumentPublishDiagnostics(logger jsonrpc.FunctionLogger, params *lsp.PublishDiagnosticsParams) {
go func() {
h.Lock()
defer h.Unlock()
if !h.waitingForDiagnostics {
return
}
logger.Logf("TextDocumentPublishDiagnostics: %v", params)
if params.IsClear {
logger.Logf("Clearing diagnostics for %s", params.URI)
return
}
h.Diagnostics <- params
}()
}
// WindowShowMessageRequest
func (h handler) WindowShowMessageRequest(context.Context, jsonrpc.FunctionLogger, *lsp.ShowMessageRequestParams) (*lsp.MessageActionItem, *jsonrpc.ResponseError) {
return nil, nil
}
// WindowShowDocument
func (h handler) WindowShowDocument(context.Context, jsonrpc.FunctionLogger, *lsp.ShowDocumentParams) (*lsp.ShowDocumentResult, *jsonrpc.ResponseError) {
return nil, nil
}
// WindowWorkDoneProgressCreate
func (h handler) WindowWorkDoneProgressCreate(context.Context, jsonrpc.FunctionLogger, *lsp.WorkDoneProgressCreateParams) *jsonrpc.ResponseError {
return nil
}
// WorkspaceWorkspaceFolders
func (h handler) WorkspaceWorkspaceFolders(context.Context, jsonrpc.FunctionLogger) ([]lsp.WorkspaceFolder, *jsonrpc.ResponseError) {
folders := []lsp.WorkspaceFolder{}
// go over server openFolders and append to folders
for name, folder := range server.openFolders {
folders = append(folders, lsp.WorkspaceFolder{
URI: folder,
Name: name,
})
}
return folders, nil
}
// WorkspaceConfiguration
func (h handler) WorkspaceConfiguration(context.Context, jsonrpc.FunctionLogger, *lsp.ConfigurationParams) ([]json.RawMessage, *jsonrpc.ResponseError) {
body, _ := json.Marshal(h.config)
return []json.RawMessage{body, body}, nil
}
// WorkspaceApplyEdit
func (h handler) WorkspaceApplyEdit(context.Context, jsonrpc.FunctionLogger, *lsp.ApplyWorkspaceEditParams) (*lsp.ApplyWorkspaceEditResult, *jsonrpc.ResponseError) {
return nil, nil
}
// WorkspaceCodeLensRefresh
func (h handler) WorkspaceCodeLensRefresh(context.Context, jsonrpc.FunctionLogger) *jsonrpc.ResponseError {
return nil
}
func startRPCServer(app, name string, args ...string) *handler {
var stdin io.WriteCloser
var stdout, stderr io.ReadCloser
cmd := exec.Command(name, args...)
if cin, err := cmd.StdinPipe(); err != nil {
panic("getting clangd stdin: " + err.Error())
} else if cout, err := cmd.StdoutPipe(); err != nil {
panic("getting clangd stdout: " + err.Error())
} else if cerr, err := cmd.StderrPipe(); err != nil {
panic("getting clangd stderr: " + err.Error())
} else if err := cmd.Start(); err != nil {
panic("running clangd: " + err.Error())
} else {
stdin = cin
stdout = cout
stderr = cerr
}
stdio := NewReadWriteCloser(stdout, stdin)
if config.EnableLogging {
stdio = LogReadWriteCloserAs(stdio, app+".log")
go io.Copy(openLogFileAs(app+"-err.log"), stderr)
} else {
go io.Copy(os.Stderr, stderr)
}
handler := &handler{
Diagnostics: make(chan *lsp.PublishDiagnosticsParams),
}
lsc := lsp.NewClient(stdio, stdio, handler, func(err error) {
log.Println(errorString("Error: %v", err))
})
handler.lsc = lsc
go func() {
defer stdin.Close()
cmd.Wait()
}()
return handler
}