Repository navigation
Expand file tree
/
Copy pathproof.lua
More file actions
84 lines (77 loc) · 2.66 KB
/
Copy pathproof.lua
File metadata and controls
84 lines (77 loc) · 2.66 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
-- Code generated by litgo from litgo.lit.md; DO NOT EDIT.
-- Diagnostics from the verifier, drawn apart from the compiler's.
local M = {}
M.handler = "litgo/proof"
local drawn = {} -- diagnostic namespace -> extmark namespace
function M.is_proof(d)
return type(d.source) == "string" and d.source:find("vego", 1, true) ~= nil
end
local function draw(buf, ns, d)
local line = vim.api.nvim_buf_get_lines(buf, d.lnum, d.lnum + 1, false)[1] or ""
local warn = d.severity ~= vim.diagnostic.severity.ERROR
local hl = warn and "LitgoProofWarn" or "LitgoProofError"
local pad = string.rep(" ", vim.fn.strdisplaywidth(line:match("^%s*")))
local lines = {}
for i, text in ipairs(vim.split(d.message, "\n")) do
lines[i] = { { pad .. (i == 1 and "└─ ∴ " or " ") .. text, hl } }
end
local end_col = math.min(d.end_col or #line, #line)
pcall(vim.api.nvim_buf_set_extmark, buf, ns, d.lnum, math.min(d.col, #line), {
end_col = math.max(end_col, math.min(d.col, #line)),
hl_group = not warn and "LitgoProofUnderline" or nil,
sign_text = "∴",
sign_hl_group = hl,
virt_lines = lines,
priority = 220,
strict = false,
})
end
--- Install the handler, and keep the built-in ones away from proofs.
function M.setup()
if vim.diagnostic.handlers[M.handler] then
return
end
local function is_lit(buf)
return require("litgo").is_lit(buf)
end
vim.diagnostic.handlers[M.handler] = {
show = function(namespace, buf, diagnostics)
drawn[namespace] = drawn[namespace] or vim.api.nvim_create_namespace("litgo-proof-" .. namespace)
vim.api.nvim_buf_clear_namespace(buf, drawn[namespace], 0, -1)
if not is_lit(buf) then
return
end
for _, d in ipairs(diagnostics) do
if M.is_proof(d) then
draw(buf, drawn[namespace], d)
end
end
end,
hide = function(namespace, buf)
if drawn[namespace] and vim.api.nvim_buf_is_valid(buf) then
vim.api.nvim_buf_clear_namespace(buf, drawn[namespace], 0, -1)
end
end,
}
for _, name in ipairs({ "signs", "underline", "virtual_text", "virtual_lines" }) do
local builtin = vim.diagnostic.handlers[name]
if builtin then
vim.diagnostic.handlers[name] = {
show = function(namespace, buf, diagnostics, opts)
if is_lit(buf) then
diagnostics = vim.tbl_filter(function(d)
return not M.is_proof(d)
end, diagnostics)
end
builtin.show(namespace, buf, diagnostics, opts)
end,
hide = function(namespace, buf)
if builtin.hide then
builtin.hide(namespace, buf)
end
end,
}
end
end
end
return M