This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (128 loc) · 4.18 KB
/
main.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
package main
import (
"bufio"
"educationalsp/analysis"
"educationalsp/lsp"
"educationalsp/rpc"
"encoding/json"
"fmt"
"io"
"log"
"os"
)
func main() {
logger := getLogger("./log.txt")
logger.Println("Hey, I Started!")
fmt.Println("hi")
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(rpc.Split)
state := analysis.NewState()
writer := os.Stdout
for scanner.Scan() {
msg := scanner.Bytes()
method, contents, err := rpc.DecodeMessage(msg)
if err != nil {
logger.Printf("Got an error: %s", err)
continue
}
handleMessage(logger, writer, state, method, contents)
}
}
func handleMessage(logger *log.Logger, writer io.Writer, state analysis.State, method string, contents []byte) {
logger.Printf("Received msg with method: %s", method)
switch method {
case "initialize":
var request lsp.InitializeRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("We couldn't parse this: %s", err)
}
logger.Printf("Connected to: %s %s",
request.Params.ClientInfo.Name,
request.Params.ClientInfo.Version)
// Replying client
msg := lsp.NewInitializeResponse(request.ID)
writeResponse(writer, msg)
logger.Print("Sent the reply")
case "textDocument/didOpen":
var request lsp.DidOpenTextDocumentNotification
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/didOpen: %s", err)
return
}
logger.Printf("Opened: %s", request.Params.TextDocument.URI)
diagnostics := state.OpenDocument(request.Params.TextDocument.URI, request.Params.TextDocument.Text)
writeResponse(writer, lsp.PublishDiagnosticsNotification{
Notification: lsp.Notification{
RPC: "2.0",
Method: "textDocument/publishDiagnostics",
},
Params: lsp.PublishDiagnosticsParams{
URI: request.Params.TextDocument.URI,
Diagnostics: diagnostics,
},
})
case "textDocument/didChange":
var request lsp.TextDocumentDidChangeNotification
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/didChange: %s", err)
return
}
logger.Printf("Changed: %s", request.Params.TextDocument.URI)
for _, change := range request.Params.ContentChanges {
diagnostics := state.UpdateDocument(request.Params.TextDocument.URI, change.Text)
writeResponse(writer, lsp.PublishDiagnosticsNotification{
Notification: lsp.Notification{
RPC: "2.0",
Method: "textDocument/publishDiagnostics",
},
Params: lsp.PublishDiagnosticsParams{
URI: request.Params.TextDocument.URI,
Diagnostics: diagnostics,
},
})
}
case "textDocument/hover":
var request lsp.HoverRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/hover: %s", err)
return
}
response := state.Hover(request.ID, request.Params.TextDocument.URI, request.Params.Position)
writeResponse(writer, response)
case "textDocument/definition":
var request lsp.DefinitionRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/definition: %s", err)
return
}
response := state.Definition(request.ID, request.Params.TextDocument.URI, request.Params.Position)
writeResponse(writer, response)
case "textDocument/codeAction":
var request lsp.CodeActionRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/codeAction: %s", err)
return
}
response := state.CodeAction(request.ID, request.Params.TextDocument.URI)
writeResponse(writer, response)
case "textDocument/completion":
var request lsp.CompletionRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/completion: %s", err)
return
}
response := state.TextDocumentCompletion(request.ID, request.Params.TextDocument.URI)
writeResponse(writer, response)
}
}
func writeResponse(writer io.Writer, msg any) {
reply := rpc.EncodeMessage(msg)
writer.Write([]byte(reply))
}
func getLogger(filename string) *log.Logger {
logfile, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
panic("Hey, you didn't give me a good file")
}
return log.New(logfile, "[educationalsp]", log.Ldate|log.Ltime|log.Lshortfile)
}