-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (92 loc) · 3.06 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
package main
import (
"bufio"
"github.com/dhananjaylatkar/cscope_lsp/lsp"
"github.com/dhananjaylatkar/cscope_lsp/rpc"
"github.com/dhananjaylatkar/cscope_lsp/state"
"encoding/json"
"io"
"log"
"os"
)
func main() {
logger := getLogger("/tmp/cscope_lsp.log")
logger.Println("Started!")
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(rpc.Split)
writer := os.Stdout
state := state.New()
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 state.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("Hey, we couldn't parse this: %s", err)
}
logger.Printf("Connected to: %s %s",
request.Params.ClientInfo.Name,
request.Params.ClientInfo.Version)
// sent initialize response
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)
state.Update(request.Params.TextDocument.URI, request.Params.TextDocument.Text)
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 {
state.Update(request.Params.TextDocument.URI, change.Text)
}
case "textDocument/definition":
var request lsp.DefinitionRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/definition: %s", err)
return
}
// Create a response
response := state.Definition(request.ID, request.Params.TextDocument.URI, logger, request.Params.Position)
writeResponse(writer, response)
case "textDocument/references":
var request lsp.ReferencesRequest
if err := json.Unmarshal(contents, &request); err != nil {
logger.Printf("textDocument/references: %s", err)
return
}
// Create a response
response := state.References(request.ID, request.Params.TextDocument.URI, logger, request.Params.Position)
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 didnt give me a good file")
}
return log.New(logfile, "[cscope_lsp]", log.Ldate|log.Ltime|log.Lshortfile)
}