-
Notifications
You must be signed in to change notification settings - Fork 0
/
copilot.go
152 lines (142 loc) · 5.09 KB
/
copilot.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
package main
import (
"context"
"github.com/tectiv3/go-lsp"
"github.com/tectiv3/go-lsp/jsonrpc"
"go.bug.st/json"
"log"
"path/filepath"
)
var cClient *handler
func startCopilot(in mrChan) {
cClient = startRPCServer("copilot", config.NodePath, config.CopilotPath, "--stdio")
cClient.lsc.SetLogger(&Logger{
IncomingPrefix: "LSC <-- Copilot", OutgoingPrefix: "LSC --> Copilot",
HiColor: hiGreenString, LoColor: greenString, ErrorColor: errorString,
})
cClient.lsc.RegisterCustomNotification("statusNotification", func(logger jsonrpc.FunctionLogger, params json.RawMessage) {
logger.Logf("statusNotification %s", string(params))
})
go cClient.lsc.Run()
go cClient.processCopilotRequests(in)
}
func (c *handler) processCopilotRequests(in mrChan) {
defer catchAndLogPanic(func() {
c.processCopilotRequests(in)
})
Log("Copilot is waiting for input")
ctx := context.Background()
lsc := c.lsc
conn := lsc.GetConnection()
for {
request := <-in
Log("LSC <-- IDE %s %s %db", "request", request.Method, len(string(request.Body)))
switch request.Method {
case "initialize":
sendRequest("initialize", KeyValue{
"capabilities": KeyValue{"workspace": KeyValue{"workspaceFolders": true}},
}, conn, ctx)
//log.Println("After initialize")
lsc.Initialized(&lsp.InitializedParams{})
sendRequest("setEditorInfo", KeyValue{
"editorInfo": KeyValue{"name": "Textmate", "version": "2.0.23"},
"editorPluginInfo": KeyValue{"name": "lsp-client", "version": "0.1.0"},
}, conn, ctx)
request.CB <- &KeyValue{"status": "ok"}
case "signIn":
resp := sendRequest("signInInitiate", KeyValue{}, conn, ctx)
var res signInResponse
json.Unmarshal(resp, &res)
// eval_in_emacs("browse-url", result['verificationUri'])
// message_emacs(f'Please enter user-code {result["userCode"]}')
//log.Println(res.Status)
request.CB <- &KeyValue{"status": res.Status}
case "getCompletions":
go func() {
textDocument := &KeyValue{}
if err := json.Unmarshal(request.Body, textDocument); err != nil {
request.CB <- &KeyValue{"result": "error", "message": err.Error()}
return
}
path := textDocument.string("uri", "")
position := textDocument.keyValue("position", KeyValue{})
resp := sendRequest("getCompletions", KeyValue{
"doc": KeyValue{
"source": textDocument.string("text", ""),
"tabSize": textDocument.int("tabSize", 4),
"indentSize": 4,
"insertSpaces": true,
"version": 0,
"path": path,
"uri": "file://" + path,
"relativePath": filepath.Base(path),
"languageId": textDocument.string("languageId", ""),
"position": position,
},
}, conn, ctx)
if string(resp) == "null" {
Log("Empty response")
request.CB <- &KeyValue{"status": "ok", "result": "No completions"}
return
}
result := CompletionsResponse{}
if err := json.Unmarshal(resp, &result); err != nil {
request.CB <- &KeyValue{"result": "error", "message": err.Error()}
return
}
// check that slice of completions is not empty
if len(result.Completions) == 0 {
request.CB <- &KeyValue{"status": "ok", "result": "No completions"}
return
}
completion := result.Completions[0]
conn.SendNotification("notifyShown", lsp.EncodeMessage(KeyValue{
"uuids": []string{completion.UUID},
}))
request.CB <- &KeyValue{"status": "ok", "result": lsp.EncodeMessage(completion.DisplayText)}
}()
case "getCompletionsCycling":
//
case "notifyCompletionAccepted":
//h.client.GetConnection().SendNotification("notifyAccepted", lsp.EncodeMessage(KeyValue{}))
case "notifyCompletionRejected":
//h.client.GetConnection().SendNotification("notifyRejected", lsp.EncodeMessage(KeyValue{}))
case "textDocument/didOpen":
textDocument := &KeyValue{}
if err := json.Unmarshal(request.Body, textDocument); err != nil {
request.CB <- &KeyValue{"result": "error", "message": err.Error()}
return
}
uri, _ := lsp.NewDocumentURIFromURL(textDocument.string("uri", ""))
go lsc.TextDocumentDidOpen(&lsp.DidOpenTextDocumentParams{TextDocument: lsp.TextDocumentItem{
URI: uri,
LanguageID: textDocument.string("languageId", ""),
Version: textDocument.int("version", 0),
Text: textDocument.string("text", ""),
}})
request.CB <- &KeyValue{"status": "ok"}
case "textDocument/didClose":
textDocument := lsp.TextDocumentIdentifier{}
if err := json.Unmarshal(request.Body, &textDocument); err != nil {
request.CB <- &KeyValue{"result": "error", "message": err.Error()}
return
}
go lsc.TextDocumentDidClose(&lsp.DidCloseTextDocumentParams{TextDocument: textDocument})
request.CB <- &KeyValue{"status": "ok"}
}
}
}
func sendRequest(method string, request KeyValue, conn *jsonrpc.Connection, ctx context.Context) json.RawMessage {
body, err := json.Marshal(request)
if err != nil {
LogError(err)
return []byte{}
}
resp, respErr, err := conn.SendRequest(ctx, method, body)
if err != nil || respErr != nil {
log.Println("respErr: ", respErr)
LogError(err)
return []byte{}
}
return resp
}