diff options
author | Mathias Fussenegger <f.mathias@zignar.net> | 2021-06-06 12:55:41 +0200 |
---|---|---|
committer | Mathias Fussenegger <f.mathias@zignar.net> | 2021-06-07 18:24:32 +0200 |
commit | f03a4d616b2eb22064719a654892cb45d584fe03 (patch) | |
tree | fc79e7838810cdc625281afb0aa42f29d85b9bc9 /runtime/lua/vim/lsp/util.lua | |
parent | f2906a4669a2eef6d7bf86a29648793d63c98949 (diff) | |
download | rneovim-f03a4d616b2eb22064719a654892cb45d584fe03.tar.gz rneovim-f03a4d616b2eb22064719a654892cb45d584fe03.tar.bz2 rneovim-f03a4d616b2eb22064719a654892cb45d584fe03.zip |
feat(lsp): Split out a `diagnostics_to_items` function from set_loclist
Makes it easier to re-use the logic to populate the quickfix list
instead of the location list.
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r-- | runtime/lua/vim/lsp/util.lua | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index d421a10011..cb9a7cbed5 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -30,6 +30,16 @@ local default_border = { {" ", "NormalFloat"}, } + +local DiagnosticSeverity = protocol.DiagnosticSeverity +local loclist_type_map = { + [DiagnosticSeverity.Error] = 'E', + [DiagnosticSeverity.Warning] = 'W', + [DiagnosticSeverity.Information] = 'I', + [DiagnosticSeverity.Hint] = 'I', +} + + --@private -- Check the border given by opts or the default border for the additional -- size it adds to a float. @@ -1878,6 +1888,40 @@ function M.lookup_section(settings, section) return settings end + +--- Convert diagnostics grouped by bufnr to a list of items for use in the +--- quickfix or location list. +--- +--@param diagnostics_by_bufnr table bufnr -> Diagnostic[] +--@param predicate an optional function to filter the diagnostics. +-- If present, only diagnostic items matching will be included. +--@return table (A list of items) +function M.diagnostics_to_items(diagnostics_by_bufnr, predicate) + local items = {} + for bufnr, diagnostics in pairs(diagnostics_by_bufnr or {}) do + for _, d in pairs(diagnostics) do + if not predicate or predicate(d) then + table.insert(items, { + bufnr = bufnr, + lnum = d.range.start.line + 1, + col = d.range.start.character + 1, + text = d.message, + type = loclist_type_map[d.severity or DiagnosticSeverity.Error] or 'E' + }) + end + end + end + table.sort(items, function(a, b) + if a.bufnr == b.bufnr then + return a.lnum < b.lnum + else + return a.bufnr < b.bufnr + end + end) + return items +end + + M._get_line_byte_from_position = get_line_byte_from_position M._warn_once = warn_once |