|
| 1 | +-- effect-error-pretty.nvim |
| 2 | +-- Effect-first TypeScript diagnostic formatter for Neovim. |
| 3 | + |
| 4 | +local parse = require("effect-error-pretty.parse") |
| 5 | +local render = require("effect-error-pretty.render") |
| 6 | + |
| 7 | +local M = {} |
| 8 | + |
| 9 | +M.parse = parse.parse |
| 10 | +M.is_ts_source = parse.is_ts_source |
| 11 | + |
| 12 | +---@class EffectErrorPretty.Config |
| 13 | +---@field effect? boolean Enable Effect<A,E,R> / Stream / Layer parsing. Default: true. |
| 14 | +---@field sources? table<string, boolean> Diagnostic sources to format. Default: { typescript, ts, vtsls }. |
| 15 | +---@field format_ts_errors_fallback? boolean Use format-ts-errors when no pattern matches. Default: true. |
| 16 | +---@field extra_patterns? fun(msg: string): table|nil[] User-defined parsers, run after builtins. |
| 17 | +---@field float? boolean Patch vim.diagnostic.config({ float = { format } }) in setup. Default: false. |
| 18 | + |
| 19 | +---@type EffectErrorPretty.Config |
| 20 | +local defaults = { |
| 21 | + effect = true, |
| 22 | + sources = nil, -- nil -> use parse.default_sources |
| 23 | + format_ts_errors_fallback = true, |
| 24 | + extra_patterns = nil, |
| 25 | + float = false, |
| 26 | +} |
| 27 | + |
| 28 | +local state = { opts = vim.deepcopy(defaults) } |
| 29 | + |
| 30 | +local function parse_opts() |
| 31 | + return { |
| 32 | + effect = state.opts.effect, |
| 33 | + extra_patterns = state.opts.extra_patterns, |
| 34 | + } |
| 35 | +end |
| 36 | + |
| 37 | +local function source_allowed(source) |
| 38 | + local allowed = state.opts.sources or parse.default_sources |
| 39 | + return parse.is_ts_source(source, allowed) |
| 40 | +end |
| 41 | + |
| 42 | +local function ts_errors_fallback(diagnostic) |
| 43 | + if not state.opts.format_ts_errors_fallback then |
| 44 | + return nil |
| 45 | + end |
| 46 | + local ok, formatter = pcall(require, "format-ts-errors") |
| 47 | + if not ok or not diagnostic.code then |
| 48 | + return nil |
| 49 | + end |
| 50 | + local fn = formatter[diagnostic.code] |
| 51 | + if type(fn) ~= "function" then |
| 52 | + return nil |
| 53 | + end |
| 54 | + local msg = fn(diagnostic.message) |
| 55 | + if not msg or msg == "" then |
| 56 | + return nil |
| 57 | + end |
| 58 | + return render.strip_fences(msg) |
| 59 | +end |
| 60 | + |
| 61 | +-- Returns the multi-line "artistic" box for `vim.diagnostic.config.float.format`. |
| 62 | +-- If no pattern matches and no fallback applies, returns nil so the caller can |
| 63 | +-- use the diagnostic's original message. |
| 64 | +---@param diagnostic vim.Diagnostic |
| 65 | +---@return string|nil |
| 66 | +function M.float_format(diagnostic) |
| 67 | + if not source_allowed(diagnostic.source) then |
| 68 | + return nil |
| 69 | + end |
| 70 | + local artistic = render.artistic(diagnostic, parse_opts()) |
| 71 | + if artistic then |
| 72 | + return artistic |
| 73 | + end |
| 74 | + return ts_errors_fallback(diagnostic) |
| 75 | +end |
| 76 | + |
| 77 | +-- Returns the one-line formatted message for inline-virtual-text plugins |
| 78 | +-- (e.g. tiny-inline-diagnostic's `options.format`). nil -> caller falls back |
| 79 | +-- to the raw message. |
| 80 | +---@param diagnostic vim.Diagnostic |
| 81 | +---@return string|nil |
| 82 | +function M.inline_format(diagnostic) |
| 83 | + if not source_allowed(diagnostic.source) then |
| 84 | + return nil |
| 85 | + end |
| 86 | + local short = render.short(diagnostic, parse_opts()) |
| 87 | + if short then |
| 88 | + return short |
| 89 | + end |
| 90 | + local fallback = ts_errors_fallback(diagnostic) |
| 91 | + if fallback then |
| 92 | + fallback = fallback:gsub("\n.*", "") |
| 93 | + if #fallback > 80 then |
| 94 | + fallback = fallback:sub(1, 77) .. "…" |
| 95 | + end |
| 96 | + return fallback |
| 97 | + end |
| 98 | + return nil |
| 99 | +end |
| 100 | + |
| 101 | +---@param opts? EffectErrorPretty.Config |
| 102 | +function M.setup(opts) |
| 103 | + state.opts = vim.tbl_deep_extend("force", vim.deepcopy(defaults), opts or {}) |
| 104 | + |
| 105 | + if state.opts.float then |
| 106 | + local prev = (vim.diagnostic.config() or {}).float or {} |
| 107 | + local prev_format = type(prev) == "table" and prev.format or nil |
| 108 | + vim.diagnostic.config({ |
| 109 | + float = vim.tbl_deep_extend("force", type(prev) == "table" and prev or {}, { |
| 110 | + format = function(diagnostic) |
| 111 | + local rendered = M.float_format(diagnostic) |
| 112 | + if rendered then |
| 113 | + return rendered |
| 114 | + end |
| 115 | + if prev_format then |
| 116 | + return prev_format(diagnostic) |
| 117 | + end |
| 118 | + return diagnostic.message |
| 119 | + end, |
| 120 | + }), |
| 121 | + }) |
| 122 | + end |
| 123 | +end |
| 124 | + |
| 125 | +return M |
0 commit comments