diff options
author | w0rp <devw0rp@gmail.com> | 2023-09-14 00:38:12 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2023-09-14 00:40:56 +0100 |
commit | be69af270547b0d1dd5fe947efb3f61455f709ed (patch) | |
tree | 3d72ab429ed3d6025fb88accbd2be753fb78f26a /lua/ale/diagnostics.lua | |
parent | 9092af9ad6a5c93a759be571323ea1d0cafa8d6e (diff) | |
download | ale-be69af270547b0d1dd5fe947efb3f61455f709ed.zip |
#4607 No conflicts with nvim-lspconfig by default
Default `g:ale_disable_lsp` to a new mode `'auto'` by default. With this
setting applied, ALE will now check for the presence of nvim-lspconfig
and automatically turn off particular LSP linters if already configured
via nvim-lspconfig.
For users that do not use `nvim-lspconfig`, everything should work as
before.
Diffstat (limited to 'lua/ale/diagnostics.lua')
-rw-r--r-- | lua/ale/diagnostics.lua | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lua/ale/diagnostics.lua b/lua/ale/diagnostics.lua new file mode 100644 index 00000000..21f81e2a --- /dev/null +++ b/lua/ale/diagnostics.lua @@ -0,0 +1,82 @@ +local module = {} + +local ale_type_to_diagnostic_severity = { + E = vim.diagnostic.severity.ERROR, + W = vim.diagnostic.severity.WARN, + I = vim.diagnostic.severity.INFO +} + +-- Equivalent to ale#Var, only we can't error on missing global keys. +module.aleVar = function(buffer, key) + key = "ale_" .. key + local exists, value = pcall(vim.api.nvim_buf_get_var, buffer, key) + + if exists then + return value + end + + return vim.g[key] +end + +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 + if location.bufnr == buffer then + 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", + -- An error code + code = location.code, + -- The error message + message = location.text, + -- e.g. "rubocop" + source = location.linter_name, + } + ) + end + end + + local virtualtext_enabled_set = { + ['all'] = true, + ['2'] = true, + [2] = true, + ['current'] = true, + ['1'] = true, + [1] = true, + } + + local signs = module.aleVar(buffer, 'set_signs') == 1 + + if signs then + -- If signs are enabled, set the priority for them. + signs = {priority = vim.g.ale_sign_priority } + end + + vim.diagnostic.set( + vim.api.nvim_create_namespace('ale'), + buffer, + diagnostics, + { + virtual_text = virtualtext_enabled_set[vim.g.ale_virtualtext_cursor] ~= nil, + signs = signs, + } + ) +end + +return module |