diff options
author | Ben Boeckel <mathstuf@users.noreply.github.com> | 2023-01-29 17:25:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-29 17:25:09 +0000 |
commit | 116d713f63c7a81663fa53efa10e34649c9479e3 (patch) | |
tree | 0ca5ed37b4320e70901de6b9bcafee53c259a2aa /lua | |
parent | 0af4899605af26dd8ea7fe79acff6ab99f6532b2 (diff) | |
download | ale-116d713f63c7a81663fa53efa10e34649c9479e3.zip |
diagnostics: support sending ALE output to Neovim's diagnostics API (#4345)
Support replacing ALE's display of problems with sending problems to the Neovim diagnostics API.
:help g:ale_use_neovim_diagnostics_api
Co-authored-by: David Balatero <dbalatero@users.noreply.github.com>
Co-authored-by: Georgi Angelchev <angelchev@live.co.uk>
Co-authored-by: w0rp <devw0rp@gmail.com>
Diffstat (limited to 'lua')
-rw-r--r-- | lua/diagnostics.lua | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lua/diagnostics.lua b/lua/diagnostics.lua new file mode 100644 index 00000000..1ac6eb57 --- /dev/null +++ b/lua/diagnostics.lua @@ -0,0 +1,49 @@ +local module = {} + +local ale_type_to_diagnostic_severity = { + E = vim.diagnostic.severity.ERROR, + W = vim.diagnostic.severity.WARN, + I = vim.diagnostic.severity.INFO +} + +module.sendAleResultsToDiagnostics = function(buffer, loclist) + local diagnostics = {} + + -- Convert all the ALE loclist items to the shape that Neovim's diagnostic + -- API is expecting. + for _, location in ipairs(loclist) do + table.insert( + diagnostics, + -- All line numbers from ALE are 1-indexed, but all line numbers + -- in the diagnostics API are 0-indexed, so we have to subtract 1 + -- to make this work. + { + lnum = location.lnum - 1, + -- Ending line number, or if we don't have one, just make it the same + -- as the starting line number + end_lnum = (location.end_lnum or location.lnum) - 1, + -- Which column does the error start on? + col = math.max((location.col or 1) - 1, 0), + -- end_col does *not* appear to need 1 subtracted, so we don't. + end_col = location.end_col, + -- Which severity: error, warning, or info? + severity = ale_type_to_diagnostic_severity[location.type] or "E", + -- The error message + message = location.text, + -- e.g. "rubocop" + source = location.linter_name, + } + ) + end + + local virtualtext_enabled_set = {['all'] = true, ['2'] = true, ['current'] = true, ['1'] = true} + + vim.diagnostic.set( + vim.api.nvim_create_namespace('ale'), + buffer, + diagnostics, + { virtual_text = virtualtext_enabled_set[vim.g.ale_virtualtext_cursor] ~= nil} + ) +end + +return module |