-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
241 lines (197 loc) · 5.55 KB
/
main.go
File metadata and controls
241 lines (197 loc) · 5.55 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bufio"
"encoding/json"
"io"
"os"
"terragrunt-ls/internal/config"
"terragrunt-ls/internal/logger"
"terragrunt-ls/internal/lsp"
"terragrunt-ls/internal/rpc"
"terragrunt-ls/internal/tg"
"go.lsp.dev/protocol"
)
func main() {
cfg := config.Load()
l := logger.NewLogger(cfg.LogFile, cfg.LogLevel)
defer func() {
if err := l.Close(); err != nil {
panic(err)
}
}()
l.Info("Initializing terragrunt-ls")
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(rpc.Split)
state := tg.NewState()
writer := os.Stdout
for scanner.Scan() {
msg := scanner.Bytes()
method, contents, err := rpc.DecodeMessage(msg)
if err != nil {
l.Error("Got an error decoding message from client", "err", err)
continue
}
handleMessage(l, writer, state, method, contents)
}
}
func handleMessage(l logger.Logger, writer io.Writer, state tg.State, method string, contents []byte) {
l.Debug("Received msg", "method", method, "contents", string(contents))
switch method {
case protocol.MethodInitialize:
var request lsp.InitializeRequest
if err := json.Unmarshal(contents, &request); err != nil {
l.Error("Failed to parse initialize request", "err", err)
}
l.Debug("Connected",
"Name", request.Params.ClientInfo.Name,
"Version", request.Params.ClientInfo.Version)
msg := lsp.NewInitializeResponse(request.ID)
writeResponse(l, writer, msg)
l.Debug("Initialized")
case protocol.MethodTextDocumentDidOpen:
var notification lsp.DidOpenTextDocumentNotification
if err := json.Unmarshal(contents, ¬ification); err != nil {
l.Error(
"Failed to parse didOpen request",
"error",
err,
)
}
l.Debug(
"Opened",
"URI", notification.Params.TextDocument.URI,
"LanguageID", notification.Params.TextDocument.LanguageID,
"Version", notification.Params.TextDocument.Version,
"Text", notification.Params.TextDocument.Text,
)
diagnostics := state.OpenDocument(l, notification.Params.TextDocument.URI, notification.Params.TextDocument.Text)
writeResponse(l, writer, lsp.PublishDiagnosticsNotification{
Notification: lsp.Notification{
RPC: lsp.RPCVersion,
Method: protocol.MethodTextDocumentPublishDiagnostics,
},
Params: protocol.PublishDiagnosticsParams{
URI: notification.Params.TextDocument.URI,
Diagnostics: diagnostics,
},
})
l.Debug(
"Document opened",
"URI", notification.Params.TextDocument.URI,
)
case protocol.MethodTextDocumentDidChange:
var notification lsp.DidChangeTextDocumentNotification
if err := json.Unmarshal(contents, ¬ification); err != nil {
l.Error(
"Failed to parse didChange request",
"error",
err,
)
}
l.Debug(
"Changed",
"URI", notification.Params.TextDocument.URI,
"Changes", notification.Params.ContentChanges,
)
for _, change := range notification.Params.ContentChanges {
l.Debug(
"Change",
"Range", change.Range,
"Text", change.Text,
)
diagnostics := state.UpdateDocument(l, notification.Params.TextDocument.URI, change.Text)
writeResponse(l, writer, lsp.PublishDiagnosticsNotification{
Notification: lsp.Notification{
RPC: lsp.RPCVersion,
Method: protocol.MethodTextDocumentPublishDiagnostics,
},
Params: protocol.PublishDiagnosticsParams{
URI: notification.Params.TextDocument.URI,
Diagnostics: diagnostics,
},
})
}
l.Debug(
"Document changed",
"URI", notification.Params.TextDocument.URI,
)
case protocol.MethodTextDocumentHover:
var request lsp.HoverRequest
if err := json.Unmarshal(contents, &request); err != nil {
l.Debug(
"Failed to parse hover request",
"error",
err,
)
}
l.Debug(
"Hover",
"URI", request.Params.TextDocument.URI,
"Position", request.Params.Position,
)
response := state.Hover(l, request.ID, request.Params.TextDocument.URI, request.Params.Position)
writeResponse(l, writer, response)
case protocol.MethodTextDocumentDefinition:
var request lsp.DefinitionRequest
if err := json.Unmarshal(contents, &request); err != nil {
l.Error(
"Failed to parse definition request",
"error",
err,
)
}
l.Debug(
"Definition",
"URI", request.Params.TextDocument.URI,
"Position", request.Params.Position,
)
response := state.Definition(l, request.ID, request.Params.TextDocument.URI, request.Params.Position)
writeResponse(l, writer, response)
case protocol.MethodTextDocumentCompletion:
var request lsp.CompletionRequest
if err := json.Unmarshal(contents, &request); err != nil {
l.Error(
"Failed to parse completion request",
"error",
err,
)
}
l.Debug(
"Completion",
"URI", request.Params.TextDocument.URI,
"Position", request.Params.Position,
)
response := state.TextDocumentCompletion(l, request.ID, request.Params.TextDocument.URI, request.Params.Position)
l.Debug(
"Completion response",
"Response", response,
)
writeResponse(l, writer, response)
case protocol.MethodTextDocumentFormatting:
var request lsp.FormatRequest
if err := json.Unmarshal(contents, &request); err != nil {
l.Error(
"Failed to parse format request",
"error",
err,
)
}
l.Debug(
"Formatting",
"URI", request.Params.TextDocument.URI,
)
response := state.TextDocumentFormatting(l, request.ID, request.Params.TextDocument.URI)
writeResponse(l, writer, response)
}
}
func writeResponse(l logger.Logger, writer io.Writer, msg any) {
reply := rpc.EncodeMessage(msg)
_, err := writer.Write([]byte(reply))
if err != nil {
l.Error(
"Failed to write response",
"error",
err,
)
}
}