Repository navigation
Expand file tree
/
Copy pathrun.lua
More file actions
398 lines (374 loc) · 12.2 KB
/
Copy pathrun.lua
File metadata and controls
398 lines (374 loc) · 12.2 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
10BC8
div>
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
-- Code generated by litgo from litgo.lit.md; DO NOT EDIT.
-- :Tangle, :Weave and :TangleCompileAndRun. The run loop streams JSON events
-- from `litgo run --json`; diagnostics arrive already translated to .lit.md
-- positions, so they can be placed in the buffer the programmer is editing.
local M = {}
local run_ns = vim.api.nvim_create_namespace("litgo-run")
M.ns = run_ns
local job -- the running vim.system object
local panel = { buf = nil }
local function litgo()
return require("litgo")
end
local function panel_buf()
if panel.buf and vim.api.nvim_buf_is_valid(panel.buf) then
return panel.buf
end
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(buf, "litgo://output")
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "hide"
vim.bo[buf].swapfile = false
vim.bo[buf].filetype = "litgo-output"
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = buf, silent = true })
-- <CR> on a `file.lit.md:12:3` reference jumps there.
vim.keymap.set("n", "<cr>", function()
local file, lnum, col = vim.api.nvim_get_current_line():match("([^%s:]+%.lit%.md):(%d+):?(%d*)")
if not file then
return
end
if vim.fn.filereadable(file) == 0 and panel.dir then
file = panel.dir .. "/" .. file
end
local target = vim.fn.bufnr(file)
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == target then
vim.api.nvim_set_current_win(win)
vim.api.nvim_win_set_cursor(win, { tonumber(lnum), math.max((tonumber(col) or 1) - 1, 0) })
return
end
end
vim.cmd("wincmd p | edit +" .. lnum .. " " .. vim.fn.fnameescape(file))
end, { buffer = buf, silent = true })
panel.buf = buf
return buf
end
local function panel_win()
local buf = panel_buf()
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == buf then
return win
end
end
local current = vim.api.nvim_get_current_win()
vim.cmd("botright " .. litgo().config.run.panel_height .. "split")
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, buf)
vim.wo[win].number = false
vim.wo[win].relativenumber = false
vim.wo[win].signcolumn = "no"
vim.wo[win].winfixheight = true
vim.wo[win].wrap = true
vim.api.nvim_set_current_win(current)
return win
end
--- Put the cursor in the panel, if it is on screen.
local function panel_focus()
local buf = panel.buf
if not (buf and vim.api.nvim_buf_is_valid(buf)) then
return
end
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(win) == buf then
vim.api.nvim_set_current_win(win)
return
end
end
end
local function panel_clear()
local buf = panel_buf()
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
vim.api.nvim_buf_clear_namespace(buf, run_ns, 0, -1)
end
local function panel_add(text, hl)
local buf = panel_buf()
if panel.dir then
text = text:gsub(vim.pesc(panel.dir .. "/"), "") -- positions read better without the directory
end
local lines = vim.split(text, "\n", { plain = true })
local count = vim.api.nvim_buf_line_count(buf)
local empty = count == 1 and vim.api.nvim_buf_get_lines(buf, 0, 1, false)[1] == ""
local first = empty and 0 or count
vim.api.nvim_buf_set_lines(buf, first, -1, false, lines)
if hl then
for i = 0, #lines - 1 do
vim.api.nvim_buf_set_extmark(buf, run_ns, first + i, 0, { line_hl_group = hl })
end
end
local win = panel_win()
vim.api.nvim_win_set_cursor(win, { vim.api.nvim_buf_line_count(buf), 0 })
end
--- Place diagnostics in every file they mention and mirror them in quickfix.
local function publish(diags, origin)
local by_file, qf = {}, {}
for _, d in ipairs(diags) do
local file = (d.file and d.file ~= "") and d.file or vim.api.nvim_buf_get_name(origin)
by_file[file] = by_file[file] or {}
table.insert(by_file[file], d)
qf[#qf + 1] = {
filename = file,
lnum = d.line + 1,
col = d.col + 1,
text = d.message,
type = d.severity == 1 and "E" or "W",
}
end
for file, list in pairs(by_file) do
local buf = vim.fn.bufadd(file)
vim.fn.bufload(buf)
-- What the compiler and the verifier say, the language server has usually
-- said already.
if require("litgo.lsp").reporting(buf) then
list = vim.tbl_filter(function(d)
return d.source ~= "go build" and d.source ~= "vego"
end, list)
end
vim.diagnostic.set(run_ns, buf, require("litgo.render").to_diagnostics(list, buf))
end
vim.fn.setqflist({}, " ", { title = "litgo", items = qf })
end
function M.clear(buf)
vim.diagnostic.reset(run_ns)
if buf then
vim.diagnostic.reset(run_ns, buf)
end
end
--- Keep run diagnostics accurate while editing: fixing a line dismisses its
--- error, and errors below an edit move with their text.
function M.attach(buf)
vim.diagnostic.config({
virtual_lines = true, -- the message sits under the offending line
virtual_text = false,
signs = true,
underline = true,
severity_sort = true,
[require("litgo.proof").handler] = true,
}, run_ns)
-- Several edits can arrive before the scheduled update runs (a macro, a
-- :global command), so each works on the result of the one before.
local pending
vim.api.nvim_buf_attach(buf, false, {
on_lines = function(_, _, _, first, last_old, last_new)
local diags = pending or vim.diagnostic.get(buf, { namespace = run_ns })
if #diags == 0 then
return
end
local kept, changed = {}, false
for _, d in ipairs(diags) do
if d.lnum >= first and d.lnum < math.max(last_old, first + 1) then
changed = true
else
if d.lnum >= last_old and last_new ~= last_old then
d.lnum = d.lnum + last_new - last_old
d.end_lnum = d.end_lnum and d.end_lnum + last_new - last_old
changed = true
end
kept[#kept + 1] = d
end
end
if changed and not pending then
vim.schedule(function()
if vim.api.nvim_buf_is_valid(buf) then
vim.diagnostic.set(run_ns, buf, pending)
end
pending = nil
end)
end
if changed then
pending = kept
end
end,
})
end
local function save(buf)
if vim.bo[buf].modified then
vim.api.nvim_buf_call(buf, function()
vim.cmd("silent update")
end)
end
end
function M.tangle(buf, force)
local bin = litgo().bin()
if not bin then
return
end
save(buf)
local cmd = { bin, "tangle" }
if force then
cmd[#cmd + 1] = "--force"
end
cmd[#cmd + 1] = vim.api.nvim_buf_get_name(buf)
vim.system(cmd, { text = true }, function(res)
vim.schedule(function()
local msg = vim.trim(res.stderr or "")
vim.notify(msg ~= "" and msg or "litgo: tangled", res.code == 0 and vim.log.levels.INFO or vim.log.levels.ERROR)
end)
end)
end
function M.weave(buf, format)
local bin = litgo().bin()
if not bin then
return
end
save(buf)
local cmd = { bin, "weave" }
if format and format ~= "" then
vim.list_extend(cmd, { "--to", format })
end
cmd[#cmd + 1] = vim.api.nvim_buf_get_name(buf)
vim.system(cmd, { text = true }, function(res)
vim.schedule(function()
if res.code ~= 0 then
vim.notify(vim.trim(res.stderr or "litgo: weave failed"), vim.log.levels.ERROR)
return
end
local out = vim.trim(res.stdout)
vim.notify("litgo: wove " .. vim.fn.fnamemodify(out, ":~:."))
if litgo().config.weave.open then
vim.ui.open(out)
end
end)
end)
end
--- Check the //@ annotations without compiling or running anything.
function M.prove(buf)
local bin = litgo().bin()
if not bin then
return
end
save(buf)
M.clear(buf)
vim.system({ bin, "prove", "--json", vim.api.nvim_buf_get_name(buf) }, { text = true }, function(res)
vim.schedule(function()
local ok, decoded = pcall(vim.json.decode, res.stdout or "")
if not ok or type(decoded) ~= "table" then
vim.notify("litgo: prove failed\n" .. (res.stderr or ""), vim.log.levels.ERROR)
return
end
publish(decoded.diagnostics or {}, buf)
vim.g.litgo_last_proof = ("%d/%d"):format(decoded.proved, decoded.functions)
local level = decoded.proved == decoded.functions and vim.log.levels.INFO or vim.log.levels.WARN
if decoded.functions == 0 then
vim.notify("litgo: no //@ annotations, so nothing to prove")
else
vim.notify(("litgo: ∴ proved %d of %d annotated functions (%d obligations)"):format(decoded.proved, decoded.functions, decoded.obligations), level)
end
end)
end)
end
function M.stop()
if job then
job:kill("sigterm")
job = nil
panel_add("■ stopped", "LitgoError")
end
end
function M.run(buf, args)
local bin = litgo().bin()
if not bin then
return
end
if job then
job:kill("sigterm")
job = nil
end
save(buf)
M.clear(buf)
panel_clear()
panel.dir = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ":p:h")
local cfg = litgo().config.run
local cmd = { bin, "run", "--json" }
if cfg.vet then
cmd[#cmd + 1] = "--vet"
end
if cfg.timeout and cfg.timeout ~= "" then
vim.list_extend(cmd, { "--timeout", cfg.timeout })
end
cmd[#cmd + 1] = vim.api.nvim_buf_get_name(buf)
if args and #args > 0 then
cmd[#cmd + 1] = "--"
vim.list_extend(cmd, args)
end
local diags, pending = {}, ""
local function handle(ev)
if ev.event == "tangle" then
panel_add("⟫ tangled → " .. ev.out, "LitgoInfo")
elseif ev.event == "diagnostic" then
diags[#diags + 1] = ev
local where = ev.file and (ev.file .. ":" .. (ev.line + 1) .. ":" .. (ev.col + 1) .. ": ") or ""
local hl = ev.severity == 1 and "LitgoError" or "DiagnosticWarn"
if require("litgo.proof").is_proof(ev) then
hl = ev.severity == 1 and "LitgoProofError" or "LitgoProofWarn"
end
panel_add(where .. ev.message, hl)
elseif ev.event == "prove" then
local all = ev.proved == ev.functions
panel_add(("∴ proved %d of %d annotated functions (%d obligations, %d ms)"):format(ev.proved, ev.functions, ev.obligations, ev.ms), all and "LitgoProofKeyword" or "LitgoProofError")
elseif ev.event == "build" then
if ev.ok then
panel_add(("✓ built in %d ms"):format(ev.ms), "LitgoOk")
else
local what = { tangle = "tangle failed", prove = "not proved, so not run" }
panel_add("✗ " .. (what[ev.stage] or "build failed"), ev.stage == "prove" and "LitgoProofError" or "LitgoError")
end
elseif ev.event == "output" then
panel_add(ev.text, ev.stream == "stderr" and "DiagnosticWarn" or nil)
elseif ev.event == "exit" and ev.ran then
local note = ev.timed_out and " (timed out)" or ""
panel_add(("%s exit %d in %d ms%s"):format(ev.code == 0 and "✓" or "✗", ev.code, ev.ms, note), ev.code == 0 and "LitgoOk" or "LitgoError")
end
end
local this
this = vim.system(cmd, {
text = true,
stdout = function(_, data)
if not data then
return
end
vim.schedule(function()
if job ~= this then
return -- a newer run replaced this one
end
pending = pending .. data
while true do
local nl = pending:find("\n", 1, true)
if not nl then
break
end
local line = pending:sub(1, nl - 1)
pending = pending:sub(nl + 1)
local ok, ev = pcall(vim.json.decode, line)
if ok and type(ev) == "table" then
handle(ev)
end
end
end)
end,
}, function()
vim.schedule(function()
if job ~= this then
return
end
job = nil
publish(diags, buf)
local jumped = false
if cfg.jump and vim.api.nvim_get_current_buf() == buf then
local name = vim.api.nvim_buf_get_name(buf)
for _, d in ipairs(diags) do
if d.severity == 1 and (not d.file or d.file == name) then
local row = math.min(d.line + 1, vim.api.nvim_buf_line_count(buf))
vim.api.nvim_win_set_cursor(0, { row, d.col })
vim.cmd("normal! zv")
jumped = true
break
end
end
end
if cfg.focus and not jumped and vim.api.nvim_get_current_buf() == buf then
panel_focus()
end
end)
end)
job = this
end
return M