-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathevents.lua
More file actions
295 lines (251 loc) · 8.91 KB
/
events.lua
File metadata and controls
295 lines (251 loc) · 8.91 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")
local util = import("micro/util")
local buffer = import("micro/buffer")
local fmt = import("fmt")
local go_os = import("os")
local path = import("path")
local filepath = import("path/filepath")
local version = {}
function preRune(bp, r)
if splitBP ~= nil then
pcall(function() splitBP:Unsplit(); end)
splitBP = nil
local cur = bp.Buf:GetActiveCursor()
cur:Deselect(false);
cur:GotoLoc(buffer.Loc(cur.X + 1, cur.Y))
end
end
-- when a new character is types, the document changes
function onRune(bp, r)
local filetype = bp.Buf:FileType()
micro.Log("FILETYPE", filetype)
if cmd[filetype] == nil then
return
end
if splitBP ~= nil then
pcall(function() splitBP:Unsplit(); end)
splitBP = nil
end
local send = withSend(filetype)
local uri = getUriFromBuf(bp.Buf)
if r ~= nil then
lastCompletion = {}
end
-- allow the document contents to be escaped properly for the JSON string
local content = util.String(bp.Buf:Bytes()):gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\r", "\\r"):gsub('"', '\\"')
:gsub("\t", "\\t")
-- increase change version
version[uri] = (version[uri] or 0) + 1
send("textDocument/didChange",
fmt.Sprintf('{"textDocument": {"version": %.0f, "uri": "%s"}, "contentChanges": [{"text": "%s"}]}', version[uri],
uri, content), true)
local ignored = mysplit(config.GetGlobalOption("lsp.ignoreTriggerCharacters") or '', ",")
if r and capabilities[filetype] then
if not contains(ignored, "completion") and capabilities[filetype].completionProvider and capabilities[filetype].completionProvider.triggerCharacters and contains(capabilities[filetype].completionProvider.triggerCharacters, r) then
completionAction(bp)
elseif not contains(ignored, "signature") and capabilities[filetype].signatureHelpProvider and capabilities[filetype].signatureHelpProvider.triggerCharacters and contains(capabilities[filetype].signatureHelpProvider.triggerCharacters, r) then
hoverAction(bp)
end
end
end
function onBeforeTextEvent(bp, textEvent)
--micro.Log('onBeforeTextEvent', bp, textEvent)
return
--[[
local filetype = bp.Settings["filetype"]
if cmd[filetype] == nil then
return
end
if splitBP ~= nil then
pcall(function() splitBP:Unsplit(); end)
splitBP = nil
end
local send = withSend(filetype)
local uri = getUriFromBuf(bp)
version[uri] = (version[uri] or 0) + 1
local changes = ''
-- send the change message for every delta
for i = 1, #textEvent.Deltas do
local delta = textEvent.Deltas[i]
local startLine = 0 + delta.Start.Y - 1
local startChar = 0 + delta.Start.X - 1
local endLine = 0 + delta.End.Y - 1
local endChar = 0 + delta.End.X - 1
local change = util.String(delta.Text):gsub("\\", "\\\\"):gsub("\n", "\\n"):gsub("\r", "\\r"):gsub('"', '\\"')
:gsub("\t", "\\t")
if #changes > 0 then changes = changes .. ',' end
changes = changes .. fmt.Sprintf('{"range": {"start": {"line": %d, "character": %d}, "end": {"line": %d, "character": %d}}, "text": "%s"}', startLine, startChar, endLine, endChar, change)
end
if #changes > 0 then
micro.Log(changes)
send("textDocument/didChange", fmt.Sprintf('{"textDocument": {"version": "%.0f", "uri": "%s"}, "contentChanges": [%s]', version[uri], uri, changes))
end
--]]
end
-- alias functions for any kind of change to the document
function onMoveLinesUp(bp) onRune(bp) end
function onMoveLinesDown(bp) onRune(bp) end
function onDeleteWordRight(bp) onRune(bp) end
function onDeleteWordLeft(bp) onRune(bp) end
function onInsertNewline(bp) onRune(bp) end
function onInsertSpace(bp) onRune(bp) end
function onBackspace(bp) onRune(bp) end
function onDelete(bp) onRune(bp) end
function onInsertTab(bp) onRune(bp) end
function onUndo(bp) onRune(bp) end
function onRedo(bp) onRune(bp) end
function onCut(bp) onRune(bp) end
function onCutLine(bp) onRune(bp) end
function onDuplicateLine(bp) onRune(bp) end
function onDeleteLine(bp) onRune(bp) end
function onIndentSelection(bp) onRune(bp) end
function onOutdentSelection(bp) onRune(bp) end
function onOutdentLine(bp) onRune(bp) end
function onIndentLine(bp) onRune(bp) end
function onPaste(bp) onRune(bp) end
function onPlayMacro(bp) onRune(bp) end
function onAutocomplete(bp) onRune(bp) end
function onEscape(bp)
if splitBP ~= nil then
pcall(function() splitBP:Unsplit(); end)
splitBP = nil
end
end
function preInsertNewline(bp)
if bp.Buf.Path == "References found" then
local cur = bp.Buf:GetActiveCursor()
cur:SelectLine()
local data = util.String(cur:GetSelection())
local file, line, character = data:match("(./[^:]+):([^:]+):([^:]+)")
if not file then
end
local doc, _ = file:gsub("^file://", "")
buf, _ = buffer.NewBufferFromFile(doc)
bp:AddTab()
micro.CurPane():OpenBuffer(buf)
buf:GetActiveCursor():GotoLoc(buffer.Loc(character * 1, line * 1))
micro.CurPane():Center()
return false
end
end
function preSave(bp)
if config.GetGlobalOption("lsp.formatOnSave") then
onRune(bp)
formatAction(bp, function()
bp:Save()
end)
end
end
function onSave(bp)
local filetype = bp.Buf:FileType()
if cmd[filetype] == nil then
return
end
local send = withSend(filetype)
local uri = getUriFromBuf(bp.Buf)
send("textDocument/didSave", fmt.Sprintf('{"textDocument": {"uri": "%s"}}', uri), true)
end
function onBufferOpen(buf)
local filetype = buf:FileType()
micro.Log("ONBUFFEROPEN", filetype)
if filetype ~= "unknown" and not cmd[filetype] then return startServer(filetype, handleInitialized); end
if cmd[filetype] then
handleInitialized(buf, filetype)
end
end
function onStdout(filetype)
local nextMessage = ''
return function(text)
if text:starts("Content-Length:") then
message = text
else
message = message .. text
end
message = message:gsub('}Content%-Length:', '}\0Content-Length:')
local entries = mysplit(message, '\0')
if #entries > 1 then
micro.Log('Found break')
entries[1] = entries[1]
entries[2] = entries[2]
message = entries[1]
nextMessage = entries[2]
end
if not message:ends("}") then
micro.Log('Message incomplete, ignoring for now...')
return
end
local data = message:parse()
if data == false then
micro.Log('Parsing failed', message)
return
end
micro.Log(filetype .. " <<< " .. (data.method or 'no method'))
if data.method == "workspace/configuration" then
-- actually needs to respond with the same ID as the received JSON
local message = fmt.Sprintf('{"jsonrpc": "2.0", "id": %.0f, "result": [{"enable": true}]}', data.id)
shell.JobSend(cmd[filetype], fmt.Sprintf('Content-Length: %.0f\n\n%s', #message, message))
elseif data.method == "textDocument/publishDiagnostics" or data.method == "textDocument\\/publishDiagnostics" then
-- react to server-published event
local bp = micro.CurPane().Buf
bp:ClearMessages("lsp")
bp:AddMessage(buffer.NewMessage("lsp", "", buffer.Loc(0, 10000000), buffer.Loc(0, 10000000), buffer.MTInfo))
local uri = getUriFromBuf(bp)
if data.params.uri == uri then
for _, diagnostic in ipairs(data.params.diagnostics) do
local type = buffer.MTInfo
if diagnostic.severity == 1 then
type = buffer.MTError
elseif diagnostic.severity == 2 then
type = buffer.MTWarning
end
local mstart = buffer.Loc(diagnostic.range.start.character, diagnostic.range.start.line)
local mend = buffer.Loc(diagnostic.range["end"].character, diagnostic.range["end"].line)
if not isIgnoredMessage(diagnostic.message) then
msg = buffer.NewMessage("lsp", diagnostic.message, mstart, mend, type)
bp:AddMessage(msg)
end
end
end
elseif currentAction[filetype] and currentAction[filetype].method and not data.method and currentAction[filetype].response and data.jsonrpc then -- react to custom action event
local bp = micro.CurPane()
micro.Log("Received message for ", filetype, data)
currentAction[filetype].response(bp, data)
currentAction[filetype] = {}
elseif data.method == "window/showMessage" or data.method == "window\\/showMessage" then
if filetype == micro.CurPane().Buf:FileType() then
micro.InfoBar():Message(data.params.message)
else
micro.Log(filetype .. " message " .. data.params.message)
end
elseif data.method == "window/logMessage" or data.method == "window\\/logMessage" then
micro.Log(data.params.message)
elseif message:starts("Content-Length:") then
if message:find('"') and not message:find('"result":null') then
micro.Log("Unhandled message 1", filetype, message, currentAction[filetype])
end
else
-- enable for debugging purposes
micro.Log("Unhandled message 2", filetype, message)
end
if nextMessage then
local nm = nextMessage
nextMessage = nil
onStdout(filetype)(nm)
end
end
end
function onStderr(text)
micro.Log("ONSTDERR", text)
if not isIgnoredMessage(text) then
micro.InfoBar():Message(text)
end
end
function onExit(filetype)
return function(str)
currentAction[filetype] = nil
cmd[filetype] = nil
micro.Log("ONEXIT", filetype, str)
end
end