]> git.madduck.net Git - etc/vim.git/blob - lua/ale/diagnostics.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / lua / ale / diagnostics.lua
1 local ale = require("ale")
2
3 local module = {}
4
5 local diagnostic_severity_map = {
6     E = vim.diagnostic.severity.ERROR,
7     W = vim.diagnostic.severity.WARN,
8     I = vim.diagnostic.severity.INFO
9 }
10
11 -- A map of all possible values that we can consider virtualtext enabled for
12 -- from ALE's setting.
13 local virtualtext_enabled_set = {
14     ["all"] = true,
15     ["2"] = true,
16     [2] = true,
17     ["current"] = true,
18     ["1"] = true,
19     [1] = true,
20     [true] = true,
21 }
22
23 ---Send diagnostics to the Neovim diagnostics API
24 ---@param buffer number The buffer number to retreive the variable for.
25 ---@param loclist table The loclist array to report as diagnostics.
26 ---@return nil
27 module.send = function(buffer, loclist)
28     local diagnostics = {}
29
30     -- Convert all the ALE loclist items to the shape that Neovim's diagnostic
31     -- API is expecting.
32     for _, location in ipairs(loclist) do
33         if location.bufnr == buffer then
34             table.insert(
35                 diagnostics,
36                 -- All line numbers from ALE are 1-indexed, but all line
37                 -- numbers in the diagnostics API are 0-indexed, so we have to
38                 -- subtract 1 to make this work.
39                 {
40                     lnum = location.lnum - 1,
41                     -- Ending line number, or if we don't have one, just make
42                     -- it the same as the starting line number
43                     end_lnum = (location.end_lnum or location.lnum) - 1,
44                     -- Which column does the error start on?
45                     col = math.max((location.col or 1) - 1, 0),
46                     -- end_col does not appear to need 1 subtracted.
47                     end_col = location.end_col,
48                     -- Which severity: error, warning, or info?
49                     severity = diagnostic_severity_map[location.type] or "E",
50                     -- An error code
51                     code = location.code,
52                     -- The error message
53                     message = location.text,
54                     -- e.g. "rubocop"
55                     source = location.linter_name,
56                 }
57             )
58         end
59     end
60
61     local set_signs = ale.var(buffer, "set_signs")
62     local sign_priority = ale.var(buffer, "sign_priority")
63     local signs
64
65     if (set_signs == 1 or set_signs == true) and sign_priority then
66         -- If signs are enabled, set the priority for them.
67         local local_cfg = { priority = sign_priority }
68         local global_cfg = vim.diagnostic.config().signs
69
70         if global_cfg == false or global_cfg == true or global_cfg == nil then
71             signs = local_cfg
72         elseif type(global_cfg) == "table" then
73             signs = vim.tbl_extend("force", global_cfg, local_cfg)
74         else
75             -- If a global function is defined, then define a function
76             -- that calls that function when Neovim calls our function.
77             signs = function(...)
78                 return vim.tbl_extend("force", global_cfg(...), local_cfg)
79             end
80         end
81     end
82
83     vim.diagnostic.set(
84         vim.api.nvim_create_namespace("ale"),
85         buffer,
86         diagnostics,
87         {
88             virtual_text =
89                 virtualtext_enabled_set[vim.g.ale_virtualtext_cursor] ~= nil,
90             signs = signs,
91         }
92     )
93 end
94
95 return module