feat(diagnostics): add sort_by option (#2632)

* feat(diagnostics): add `sort_by` option

* [docgen] Update doc/telescope.txt
skip-checks: true

---------

Co-authored-by: Github Actions <actions@github>
This commit is contained in:
James Trew
2023-08-06 16:46:34 -04:00
committed by GitHub
parent 31b05ad3c3
commit f5363d3c2a
3 changed files with 55 additions and 20 deletions

View File

@@ -6,6 +6,50 @@ local utils = require "telescope.utils"
local diagnostics = {}
local sorting_comparator = function(opts)
local current_buf = vim.api.nvim_get_current_buf()
local comparators = {
-- sort results by bufnr (prioritize cur buf), severity, lnum
buffer = function(a, b)
if a.bufnr == b.bufnr then
if a.type == b.type then
return a.lnum < b.lnum
else
return a.type < b.type
end
else
if a.bufnr == current_buf then
return true
end
if b.bufnr == current_buf then
return false
end
return a.bufnr < b.bufnr
end
end,
severity = function(a, b)
if a.type < b.type then
return true
elseif a.type > b.type then
return false
end
if a.bufnr == b.bufnr then
return a.lnum < b.lnum
elseif a.bufnr == current_buf then
return true
elseif b.bufnr == current_buf then
return false
else
return a.bufnr < b.bufnr
end
end,
}
local sort_by = vim.F.if_nil(opts.sort_by, "buffer")
return comparators[sort_by]
end
local convert_diagnostic_type = function(severities, severity)
-- convert from string to int
if type(severity) == "string" then
@@ -20,7 +64,6 @@ local diagnostics_to_tbl = function(opts)
opts = vim.F.if_nil(opts, {})
local items = {}
local severities = vim.diagnostic.severity
local current_buf = vim.api.nvim_get_current_buf()
opts.severity = convert_diagnostic_type(severities, opts.severity)
opts.severity_limit = convert_diagnostic_type(severities, opts.severity_limit)
@@ -77,25 +120,7 @@ local diagnostics_to_tbl = function(opts)
end
end
-- sort results by bufnr (prioritize cur buf), severity, lnum
table.sort(items, function(a, b)
if a.bufnr == b.bufnr then
if a.type == b.type then
return a.lnum < b.lnum
else
return a.type < b.type
end
else
-- prioritize for current bufnr
if a.bufnr == current_buf then
return true
end
if b.bufnr == current_buf then
return false
end
return a.bufnr < b.bufnr
end
end)
table.sort(items, sorting_comparator(opts))
return items
end