robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. Keyesrefactor: introduce nvim_on internally #398839aa4608401

master
6.4 KiB192 linesraw
1local api = vim.api
2local nvim_on = require('vim._core.util').nvim_on
3
4local diagnostic_modules = vim._defer_require('vim.diagnostic', {
5  _config = ..., --- @module 'vim.diagnostic._config'
6  _severity = ..., --- @module 'vim.diagnostic._severity'
7  _shared = ..., --- @module 'vim.diagnostic._shared'
8  _store = ..., --- @module 'vim.diagnostic._store'
9})
10
11--- @class (private) vim.diagnostic._display
12local M = {}
13
14-- Metatable that automatically creates an empty table when assigning to a missing key
15local bufnr_and_namespace_cacher_mt = {
16  --- @param t table<integer,table>
17  --- @param bufnr integer
18  --- @return table
19  __index = function(t, bufnr)
20    assert(bufnr > 0, 'Invalid buffer number')
21    t[bufnr] = {}
22    return t[bufnr]
23  end,
24}
25
26--- @type table<integer,table<integer,table>>
27local bufs_waiting_to_update = setmetatable({}, bufnr_and_namespace_cacher_mt)
28
29--- @type table<string,true>
30local registered_autocmds = {}
31
32--- Table of autocmd events to fire the update for displaying new diagnostic information
33local insert_leave_auto_cmds = { 'InsertLeave', 'CursorHoldI' }
34
35--- @param namespace integer
36--- @param bufnr integer
37--- @return string
38local function make_augroup_key(namespace, bufnr)
39  local ns = vim.diagnostic.get_namespace(namespace)
40  return string.format('nvim.diagnostic.insertleave.%s.%s', bufnr, ns.name)
41end
42
43--- @param namespace integer
44--- @param bufnr integer
45local function execute_scheduled_display(namespace, bufnr)
46  local args = bufs_waiting_to_update[bufnr][namespace]
47  if not args then
48    return
49  end
50
51  -- Clear the args so we don't display unnecessarily.
52  bufs_waiting_to_update[bufnr][namespace] = nil
53
54  M.show(namespace, bufnr, nil, args)
55end
56
57--- @param namespace integer
58--- @param bufnr integer
59--- @param args vim.diagnostic.OptsResolved
60local function schedule_display(namespace, bufnr, args)
61  bufs_waiting_to_update[bufnr][namespace] = args
62
63  local key = make_augroup_key(namespace, bufnr)
64  if not registered_autocmds[key] then
65    local group = api.nvim_create_augroup(key, { clear = true })
66    nvim_on(insert_leave_auto_cmds, group, {
67      buf = bufnr,
68      desc = 'vim.diagnostic: display diagnostics',
69    }, function()
70      execute_scheduled_display(namespace, bufnr)
71    end)
72    registered_autocmds[key] = true
73  end
74end
75
76--- @param namespace integer
77--- @param bufnr integer
78local function clear_scheduled_display(namespace, bufnr)
79  local key = make_augroup_key(namespace, bufnr)
80
81  if registered_autocmds[key] then
82    api.nvim_del_augroup_by_name(key)
83    registered_autocmds[key] = nil
84  end
85end
86
87--- @param namespace integer? Diagnostic namespace. When omitted, hide
88---                          diagnostics from all namespaces.
89--- @param bufnr integer? Buffer number, or 0 for current buffer. When
90---                      omitted, hide diagnostics in all buffers.
91function M.hide(namespace, bufnr)
92  vim.validate('namespace', namespace, 'number', true)
93  vim.validate('bufnr', bufnr, 'number', true)
94
95  local buffers = bufnr and { vim._resolve_bufnr(bufnr) } or diagnostic_modules._store.get_bufnrs()
96  for _, iter_bufnr in ipairs(buffers) do
97    local namespaces = namespace and { namespace }
98      or diagnostic_modules._store.get_buf_namespaces(iter_bufnr)
99    for _, iter_namespace in ipairs(namespaces) do
100      for _, handler in pairs(vim.diagnostic.handlers) do
101        if handler.hide then
102          handler.hide(iter_namespace, iter_bufnr)
103        end
104      end
105    end
106  end
107end
108
109--- @param namespace integer? Diagnostic namespace. When omitted, show
110---                          diagnostics from all namespaces.
111--- @param bufnr integer? Buffer number, or 0 for current buffer. When omitted, show
112---                      diagnostics in all buffers.
113--- @param diagnostics vim.Diagnostic[]? The diagnostics to display. When omitted, use the
114---                             saved diagnostics for the given namespace and
115---                             buffer. This can be used to display a list of diagnostics
116---                             without saving them or to display only a subset of
117---                             diagnostics. May not be used when {namespace}
118---                             or {bufnr} is nil.
119--- @param opts? vim.diagnostic.Opts Display options.
120function M.show(namespace, bufnr, diagnostics, opts)
121  vim.validate('namespace', namespace, 'number', true)
122  vim.validate('bufnr', bufnr, 'number', true)
123  vim.validate('diagnostics', diagnostics, vim.islist, true, 'a list of diagnostics')
124  vim.validate('opts', opts, 'table', true)
125
126  if not bufnr or not namespace then
127    assert(not diagnostics, 'Cannot show diagnostics without a buffer and namespace')
128    if not bufnr then
129      for _, iter_bufnr in ipairs(diagnostic_modules._store.get_bufnrs()) do
130        M.show(namespace, iter_bufnr, nil, opts)
131      end
132    else
133      -- namespace is nil
134      bufnr = vim._resolve_bufnr(bufnr)
135      for _, iter_namespace in ipairs(diagnostic_modules._store.get_buf_namespaces(bufnr)) do
136        M.show(iter_namespace, bufnr, nil, opts)
137      end
138    end
139    return
140  end
141
142  if not vim.diagnostic.is_enabled({ bufnr = bufnr or 0, ns_id = namespace }) then
143    return
144  end
145
146  M.hide(namespace, bufnr)
147
148  diagnostics = diagnostics
149    or diagnostic_modules._store.get_diagnostics(bufnr, {
150      namespace = namespace,
151    }, true)
152
153  if vim.tbl_isempty(diagnostics) then
154    return
155  end
156
157  local opts_res = diagnostic_modules._config.get_resolved_options(opts, namespace, bufnr)
158
159  if opts_res.update_in_insert then
160    clear_scheduled_display(namespace, bufnr)
161  else
162    local mode = api.nvim_get_mode()
163    if mode.mode:sub(1, 1) == 'i' then
164      schedule_display(namespace, bufnr, opts_res)
165      return
166    end
167  end
168
169  if opts_res.severity_sort then
170    if type(opts_res.severity_sort) == 'table' and opts_res.severity_sort.reverse then
171      table.sort(diagnostics, function(a, b)
172        return diagnostic_modules._shared.diagnostic_cmp(a, b, 'severity', false)
173      end)
174    else
175      table.sort(diagnostics, function(a, b)
176        return diagnostic_modules._shared.diagnostic_cmp(a, b, 'severity', true)
177      end)
178    end
179  end
180
181  for handler_name, handler in pairs(vim.diagnostic.handlers) do
182    if handler.show and opts_res[handler_name] then
183      local filtered = diagnostic_modules._severity.filter_by_severity(
184        opts_res[handler_name].severity,
185        diagnostics
186      )
187      handler.show(namespace, bufnr, filtered, opts_res)
188    end
189  end
190end
191
192return M