Skip to content

Commit 7fc048f

Browse files
committed
feat(render): artistic float and short inline renderers
Two renderers layered on top of the parser: - `artistic(diagnostic)` — multi-line boxed output for `vim.diagnostic.config.float.format`. Long type strings wrap at `;` boundaries inside objects or at soft whitespace; short types stay on one line. - `short(diagnostic)` — single-line output for inline virtual-text plugins (e.g. tiny-inline-diagnostic). Truncates long types with `…`. Effect mismatches get a compact single-channel box when only one of A/E/R diverges, with a context-aware hint (Effect.provide, catchTags, Effect.scoped, Layer.provide) and the full Got/Expected signature printed at the bottom for full context. When two or more channels diverge, falls back to a tri-channel table view.
1 parent d10d0ad commit 7fc048f

1 file changed

Lines changed: 352 additions & 0 deletions

File tree

‎lua/effect-error-pretty/render.lua‎

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
-- Renderers: take a parsed result and produce string output.
2+
-- * artistic(): multi-line boxed output for the float.
3+
-- * short(): one-line output for inline virtual-text plugins.
4+
5+
local parse = require("effect-error-pretty.parse")
6+
7+
local M = {}
8+
9+
-- ── helpers ────────────────────────────────────────────────────────────────
10+
11+
local function prettify_type(s)
12+
if not s then
13+
return s
14+
end
15+
s = s:gsub('import%("[^"]+/node_modules/[^"]+"%)%.', "")
16+
s = s:gsub('import%("[^"]+"%)%.([%w_]+)', "%1")
17+
s = s:gsub("ParseResult%.", "")
18+
return s
19+
end
20+
21+
-- Wrap long type strings onto multiple lines at semicolons (object members)
22+
-- and soft whitespace boundaries. Returns a list of display lines where the
23+
-- first line is bare and continuations carry `indent` as a prefix.
24+
local function format_type_multiline(type_str, indent)
25+
indent = indent or "│ "
26+
type_str = prettify_type(type_str)
27+
if not type_str then
28+
return {}
29+
end
30+
31+
local max_line_len = 70
32+
if #type_str <= max_line_len then
33+
return { type_str }
34+
end
35+
36+
local lines = {}
37+
local brace_depth = 0
38+
local current = ""
39+
local function trim(s)
40+
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
41+
end
42+
43+
for i = 1, #type_str do
44+
local char = type_str:sub(i, i)
45+
current = current .. char
46+
if char == "{" then
47+
brace_depth = brace_depth + 1
48+
elseif char == "}" then
49+
brace_depth = brace_depth - 1
50+
end
51+
if char == ";" and brace_depth == 1 then
52+
table.insert(lines, trim(current))
53+
current = ""
54+
elseif #current >= max_line_len and char == " " then
55+
table.insert(lines, trim(current))
56+
current = ""
57+
end
58+
end
59+
if #trim(current) > 0 then
60+
table.insert(lines, trim(current))
61+
end
62+
63+
if #lines <= 1 then
64+
return { type_str }
65+
end
66+
67+
local result = {}
68+
for idx, line in ipairs(lines) do
69+
if idx == 1 then
70+
table.insert(result, line)
71+
else
72+
table.insert(result, indent .. line)
73+
end
74+
end
75+
return result
76+
end
77+
M.format_type_multiline = format_type_multiline
78+
79+
local function display_name(tag)
80+
if tag == "layer" then
81+
return "Layer"
82+
elseif tag == "stream" then
83+
return "Stream"
84+
end
85+
return "Effect"
86+
end
87+
88+
local function strip_fences(s)
89+
return (s:gsub("```typescript\n", ""):gsub("```ts\n", ""):gsub("\n```", ""))
90+
end
91+
M.strip_fences = strip_fences
92+
93+
-- ── artistic (float) renderer ──────────────────────────────────────────────
94+
95+
local function render_effect_mismatch(parsed)
96+
local g, e = parsed.got, parsed.expected
97+
local name = display_name(parsed.tag)
98+
local labels = parsed.labels
99+
local is_layer = parsed.tag == "layer"
100+
local lines = {}
101+
102+
local function push_multiline(indent_prefix, first_prefix, value)
103+
local chunks = format_type_multiline(value, indent_prefix)
104+
table.insert(lines, first_prefix .. (chunks[1] or ""))
105+
for i = 2, #chunks do
106+
table.insert(lines, chunks[i])
107+
end
108+
end
109+
110+
local function signature(shape)
111+
return name .. "<" .. shape.A .. ", " .. shape.E .. ", " .. shape.R .. ">"
112+
end
113+
114+
local function push_signatures()
115+
table.insert(lines, "│")
116+
push_multiline("│ ", "│ Got: ", signature(g))
117+
push_multiline("│ ", "│ Expected: ", signature(e))
118+
end
119+
120+
-- Compact single-channel-diff mode.
121+
if parsed.diff_count == 1 then
122+
if #parsed.missing_services > 0 then
123+
local only_scope = #parsed.missing_services == 1 and parsed.missing_services[1]:match("^Scope")
124+
local title
125+
if only_scope then
126+
title = name .. " — Scope Required"
127+
elseif is_layer then
128+
title = name .. " — Missing RIn"
129+
else
130+
title = name .. " — Missing Services"
131+
end
132+
table.insert(lines, "╭─ ◈ " .. title)
133+
table.insert(lines, "│")
134+
table.insert(lines, "│ ◈ Forgot to provide: " .. table.concat(parsed.missing_services, " | "))
135+
if parsed.scope_required then
136+
table.insert(lines, "│ ⚡ Hint: wrap in Effect.scoped(...) — Scope is required")
137+
elseif is_layer then
138+
table.insert(lines, "│ ⚡ Hint: compose with Layer.provide(...) or Layer.merge(...)")
139+
else
140+
table.insert(lines, "│ ⚡ Hint: .pipe(Effect.provide(SomeLayer))")
141+
end
142+
push_signatures()
143+
table.insert(lines, "╰─")
144+
return table.concat(lines, "\n")
145+
end
146+
147+
if #parsed.unhandled_errors > 0 then
148+
table.insert(lines, "╭─ ⚠ " .. name .. " — Unhandled Errors")
149+
table.insert(lines, "│")
150+
table.insert(lines, "│ ⚠ Not in E channel: " .. table.concat(parsed.unhandled_errors, " | "))
151+
table.insert(lines, "│ ⚡ Hint: .pipe(Effect.catchTags({...})) or Effect.orDie")
152+
push_signatures()
153+
table.insert(lines, "╰─")
154+
return table.concat(lines, "\n")
155+
end
156+
157+
if parsed.success_differs then
158+
local a_label = labels[1]
159+
table.insert(lines, "╭─ ⊘ " .. name .. " — " .. a_label .. " Mismatch")
160+
table.insert(lines, "│")
161+
push_multiline("│ ", "│ ✗ Got " .. a_label .. ": ", g.A)
162+
push_multiline("│ ", "│ ✓ Expected: ", e.A)
163+
push_signatures()
164+
table.insert(lines, "╰─")
165+
return table.concat(lines, "\n")
166+
end
167+
end
168+
169+
-- Multi-channel diff: full tri-channel view.
170+
table.insert(lines, "╭─ ⊘ " .. name .. " Mismatch")
171+
table.insert(lines, "│")
172+
table.insert(lines, "│ ✗ Got:")
173+
push_multiline("│ ", "│ " .. labels[1] .. ": ", g.A)
174+
push_multiline("│ ", "│ " .. labels[2] .. ": ", g.E)
175+
push_multiline("│ ", "│ " .. labels[3] .. ": ", g.R)
176+
table.insert(lines, "│")
177+
table.insert(lines, "│ ✓ Expected:")
178+
push_multiline("│ ", "│ " .. labels[1] .. ": ", e.A)
179+
push_multiline("│ ", "│ " .. labels[2] .. ": ", e.E)
180+
push_multiline("│ ", "│ " .. labels[3] .. ": ", e.R)
181+
if #parsed.missing_services > 0 then
182+
table.insert(lines, "│")
183+
table.insert(lines, "│ ◈ Forgot to provide: " .. table.concat(parsed.missing_services, " | "))
184+
end
185+
if #parsed.unhandled_errors > 0 then
186+
table.insert(lines, "│ ⚠ Unhandled errors: " .. table.concat(parsed.unhandled_errors, " | "))
187+
end
188+
if parsed.scope_required then
189+
table.insert(lines, "│ ⚡ Hint: wrap in Effect.scoped(...)")
190+
end
191+
table.insert(lines, "╰─")
192+
return table.concat(lines, "\n")
193+
end
194+
195+
function M.artistic(diagnostic, opts)
196+
opts = opts or {}
197+
local parsed = parse.parse(diagnostic.message, opts)
198+
if not parsed then
199+
return nil
200+
end
201+
202+
local lines = {}
203+
204+
if parsed.kind == "effect_mismatch" then
205+
return render_effect_mismatch(parsed)
206+
elseif parsed.kind == "type_mismatch" then
207+
local got_lines = format_type_multiline(parsed.got, "│ ")
208+
local expected_lines = format_type_multiline(parsed.expected, "│ ")
209+
table.insert(lines, "╭─ ⊘ Type Mismatch")
210+
table.insert(lines, "│")
211+
table.insert(lines, "│ ✗ Got: " .. (got_lines[1] or ""))
212+
for i = 2, #got_lines do
213+
table.insert(lines, got_lines[i])
214+
end
215+
table.insert(lines, "│ ✓ Expected: " .. (expected_lines[1] or ""))
216+
for i = 2, #expected_lines do
217+
table.insert(lines, expected_lines[i])
218+
end
219+
if parsed.missing then
220+
table.insert(lines, "│")
221+
table.insert(lines, "│ ◈ Missing: '" .. parsed.missing .. "'")
222+
end
223+
table.insert(lines, "╰─")
224+
return table.concat(lines, "\n")
225+
elseif parsed.kind == "missing_property" then
226+
local in_lines = format_type_multiline(parsed.in_type, "│ ")
227+
local req_lines = format_type_multiline(parsed.required, "│ ")
228+
table.insert(lines, "╭─ ◈ Missing Property")
229+
table.insert(lines, "│")
230+
table.insert(lines, "│ ◈ Property: '" .. parsed.prop .. "'")
231+
table.insert(lines, "│ ◇ In: " .. (in_lines[1] or ""))
232+
for i = 2, #in_lines do
233+
table.insert(lines, in_lines[i])
234+
end
235+
table.insert(lines, "│ ◆ Required: " .. (req_lines[1] or ""))
236+
for i = 2, #req_lines do
237+
table.insert(lines, req_lines[i])
238+
end
239+
table.insert(lines, "╰─")
240+
return table.concat(lines, "\n")
241+
elseif parsed.kind == "unknown_property" then
242+
local on_lines = format_type_multiline(parsed.on_type, "│ ")
243+
table.insert(lines, "╭─ ❓ Unknown Property")
244+
table.insert(lines, "│")
245+
table.insert(lines, "│ ✗ '" .. parsed.prop .. "' not found")
246+
table.insert(lines, "│ ◇ on type: " .. (on_lines[1] or ""))
247+
for i = 2, #on_lines do
248+
table.insert(lines, on_lines[i])
249+
end
250+
table.insert(lines, "╰─")
251+
return table.concat(lines, "\n")
252+
elseif parsed.kind == "undefined" then
253+
return "╭─ ❓ Undefined Reference\n│\n│ ✗ '" .. parsed.name .. "' is not defined\n╰─"
254+
elseif parsed.kind == "module_not_found" then
255+
return "╭─ 🔗 Module Not Found\n│\n│ ✗ '" .. parsed.path .. "'\n│ ⚡ Check path or install types\n╰─"
256+
elseif parsed.kind == "export_not_found" then
257+
return ("╭─ 🔗 Export Not Found\n│\n│ ✗ '%s'\n│ ◇ not exported from '%s'\n╰─"):format(
258+
parsed.member,
259+
parsed.module:gsub(".*/", "")
260+
)
261+
elseif parsed.kind == "implicit_any" then
262+
return "╭─ 📝 Implicit Any\n│\n│ ⚠ '" .. parsed.name .. "' needs type annotation\n╰─"
263+
elseif parsed.kind == "used_before_assigned" then
264+
return "╭─ ⚠ Uninitialized Variable\n│\n│ ✗ '" .. parsed.name .. "' used before assignment\n╰─"
265+
elseif parsed.kind == "nullish" then
266+
return "╭─ ❓ Nullish Reference\n│\n│ ⚠ Object may be "
267+
.. parsed.value
268+
.. "\n│ ⚡ Add optional chaining (?.) or null check\n╰─"
269+
elseif parsed.kind == "arg_count" then
270+
return ("╭─ 🔢 Argument Count\n│\n│ ✗ Got %s args, expected %s\n╰─"):format(parsed.got, parsed.expected)
271+
elseif parsed.kind == "const_assign" then
272+
return "╭─ 🔒 Constant Assignment\n│\n│ ✗ '" .. parsed.name .. "' is readonly\n╰─"
273+
elseif parsed.kind == "deprecated" then
274+
return "╭─ ⚠ Deprecated\n│\n│ ⚠ '" .. parsed.name .. "' is deprecated\n╰─"
275+
elseif parsed.kind == "not_callable" then
276+
local type_lines = parsed.type and format_type_multiline(parsed.type, "│ ") or { "Expression" }
277+
table.insert(lines, "╭─ ⊘ Not Callable")
278+
table.insert(lines, "│")
279+
table.insert(lines, "│ ✗ " .. (type_lines[1] or ""))
280+
for i = 2, #type_lines do
281+
table.insert(lines, type_lines[i])
282+
end
283+
table.insert(lines, "│ is not a function")
284+
table.insert(lines, "╰─")
285+
return table.concat(lines, "\n")
286+
end
287+
288+
return nil
289+
end
290+
291+
-- ── short (inline) renderer ───────────────────────────────────────────────
292+
293+
function M.short(diagnostic, opts)
294+
opts = opts or {}
295+
local parsed = parse.parse(diagnostic.message, opts)
296+
if not parsed then
297+
return nil
298+
end
299+
300+
if parsed.kind == "effect_mismatch" then
301+
if parsed.scope_required and #parsed.missing_services == 1 and parsed.missing_services[1]:match("^Scope") then
302+
return "◈ Needs Effect.scoped"
303+
end
304+
if #parsed.missing_services > 0 then
305+
return "◈ Missing provide: " .. table.concat(parsed.missing_services, " | ")
306+
end
307+
if #parsed.unhandled_errors > 0 then
308+
return "⚠ Unhandled: " .. table.concat(parsed.unhandled_errors, " | ")
309+
end
310+
if parsed.success_differs then
311+
local label = parsed.labels[1]
312+
local g = parsed.got.A:sub(1, 30)
313+
local e = parsed.expected.A:sub(1, 30)
314+
if #parsed.got.A > 30 then
315+
g = g .. "…"
316+
end
317+
if #parsed.expected.A > 30 then
318+
e = e .. "…"
319+
end
320+
return "✗ " .. label .. ": " .. g .. " → ✓ " .. e
321+
end
322+
return "⊘ " .. display_name(parsed.tag) .. " mismatch"
323+
elseif parsed.kind == "type_mismatch" then
324+
local got = parsed.got:gsub("import%([^)]+%)%.", ""):sub(1, 50)
325+
local expected = parsed.expected:gsub("import%([^)]+%)%.", ""):sub(1, 50)
326+
if #parsed.got > 50 then
327+
got = got .. "…"
328+
end
329+
if #parsed.expected > 50 then
330+
expected = expected .. "…"
331+
end
332+
return "✗ " .. got .. " → ✓ " .. expected
333+
elseif parsed.kind == "missing_property" then
334+
return "◈ Missing: '" .. parsed.prop .. "'"
335+
elseif parsed.kind == "unknown_property" then
336+
return "✗ Unknown: '" .. parsed.prop .. "'"
337+
elseif parsed.kind == "undefined" then
338+
return "✗ Undefined: '" .. parsed.name .. "'"
339+
elseif parsed.kind == "module_not_found" then
340+
return "✗ Module: '" .. parsed.path:gsub(".*/", "") .. "'"
341+
elseif parsed.kind == "implicit_any" then
342+
return "⚠ Needs type: '" .. parsed.name .. "'"
343+
elseif parsed.kind == "nullish" then
344+
return "⚠ Possibly nullish"
345+
elseif parsed.kind == "deprecated" then
346+
return "⚠ Deprecated: '" .. parsed.name .. "'"
347+
end
348+
349+
return nil
350+
end
351+
352+
return M

0 commit comments

Comments
 (0)