robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
4.7 KiB158 linesraw
1local store = require('vim.diagnostic._store')
2
3--- @class vim.diagnostic.OptsResolved
4--- @field float false|vim.diagnostic.Opts.Float
5--- @field update_in_insert boolean
6--- @field underline false|vim.diagnostic.Opts.Underline
7--- @field virtual_text false|vim.diagnostic.Opts.VirtualText
8--- @field virtual_lines false|vim.diagnostic.Opts.VirtualLines
9--- @field signs false|vim.diagnostic.Opts.Signs
10--- @field severity_sort false|{reverse?:boolean}
11
12--- @class (private) vim.diagnostic._config
13local M = {}
14
15--- @type vim.diagnostic.Opts
16local global_diagnostic_options = {
17  signs = true,
18  underline = true,
19  virtual_text = false,
20  virtual_lines = false,
21  float = true,
22  update_in_insert = false,
23  severity_sort = false,
24  jump = {
25    -- Wrap around buffer
26    wrap = true,
27  },
28}
29
30--- @param name 'signs'|'underline'|'virtual_text'|'virtual_lines'|'float'
31function M.enable_handler(name)
32  if global_diagnostic_options[name] == nil then
33    global_diagnostic_options[name] = true
34  end
35end
36
37--- @param option string
38--- @param namespace integer?
39--- @return table
40local function enabled_value(option, namespace)
41  local ns = namespace and vim.diagnostic.get_namespace(namespace) or {}
42  if ns.opts and type(ns.opts[option]) == 'table' then
43    return ns.opts[option]
44  end
45
46  local global_opt = global_diagnostic_options[option]
47  if type(global_opt) == 'table' then
48    return global_opt
49  end
50
51  return {}
52end
53
54--- @param option string
55--- @param value any?
56--- @param namespace integer?
57--- @param bufnr integer
58--- @return any
59local function resolve_optional_value(option, value, namespace, bufnr)
60  if not value then
61    return false
62  elseif value == true then
63    return enabled_value(option, namespace)
64  elseif type(value) == 'function' then
65    local val = value(namespace, bufnr) --- @type any
66    if val == true then
67      return enabled_value(option, namespace)
68    else
69      return val
70    end
71  elseif type(value) == 'table' then
72    return value
73  end
74  error('Unexpected option type: ' .. vim.inspect(value))
75end
76
77--- @param opts vim.diagnostic.Opts?
78--- @param namespace integer?
79--- @param bufnr integer
80--- @return vim.diagnostic.OptsResolved
81function M.get_resolved_options(opts, namespace, bufnr)
82  local ns = namespace and vim.diagnostic.get_namespace(namespace) or {}
83  -- Do not use tbl_deep_extend so that an empty table can be used to reset to default values
84  local resolved = vim.tbl_extend('keep', opts or {}, ns.opts or {}, global_diagnostic_options) --- @type table<string,any>
85  for k in pairs(global_diagnostic_options) do
86    if resolved[k] ~= nil then
87      resolved[k] = resolve_optional_value(k, resolved[k], namespace, bufnr)
88    end
89  end
90  return resolved --[[@as vim.diagnostic.OptsResolved]]
91end
92
93--- @param opts vim.diagnostic.Opts? When omitted or `nil`, retrieve the current
94---       configuration. Otherwise, a configuration table (see |vim.diagnostic.Opts|).
95--- @param namespace integer? Update the options for the given namespace.
96---                          When omitted, update the global diagnostic options.
97--- @return vim.diagnostic.Opts? : Current diagnostic config if {opts} is omitted.
98--- @overload fun(opts?: nil, namespace?: integer): vim.diagnostic.Opts
99function M.config(opts, namespace)
100  vim.validate('opts', opts, 'table', true)
101  vim.validate('namespace', namespace, 'number', true)
102
103  local t --- @type vim.diagnostic.Opts
104  if namespace then
105    local ns = vim.diagnostic.get_namespace(namespace)
106    t = ns.opts
107  else
108    t = global_diagnostic_options
109  end
110
111  if not opts then
112    -- Return current config
113    return vim.deepcopy(t, true)
114  end
115
116  local jump_opts = opts.jump --[[@as vim.diagnostic.JumpOpts1]]
117  if jump_opts and jump_opts.float ~= nil then --- @diagnostic disable-line
118    vim.deprecate('opts.jump.float', 'opts.jump.on_jump', '0.14')
119
120    local float_opts = jump_opts.float
121    if float_opts then
122      float_opts = type(float_opts) == 'table' and float_opts or {}
123
124      jump_opts.on_jump = function(_, bufnr)
125        vim.diagnostic.open_float(vim.tbl_extend('keep', float_opts, {
126          bufnr = bufnr,
127          scope = 'cursor',
128          focus = false,
129        }))
130      end
131    end
132
133    opts.jump.float = nil --- @diagnostic disable-line
134  end
135
136  for k, v in
137    pairs(opts --[[@as table<any,any>]])
138  do
139    t[k] = v
140  end
141
142  if namespace then
143    for _, bufnr in ipairs(store.get_bufnrs()) do
144      local namespaces = store.get_buf_namespaces(bufnr)
145      if vim.list_contains(namespaces, namespace) then
146        vim.diagnostic.show(namespace, bufnr)
147      end
148    end
149  else
150    for _, bufnr in ipairs(store.get_bufnrs()) do
151      for _, ns in ipairs(store.get_buf_namespaces(bufnr)) do
152        vim.diagnostic.show(ns, bufnr)
153      end
154    end
155  end
156end
157
158return M