Skip to content

Commit

Permalink
feat: sort items by severity / filename / lnum / col
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 6, 2021
1 parent 5897b09 commit 4a45782
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
31 changes: 20 additions & 11 deletions lua/trouble/providers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ function M.get(win, buf, cb, options)
return {}
end

local sort_keys = { "severity", "filename", "lnum", "col" }
provider(win, buf, function(items)
table.sort(items, function(a, b)
if a.severity == b.severity then
if a.lnum == b.lnum then
return a.col < b.col
else
return a.lnum < b.lnum
for _, key in ipairs(sort_keys) do
if a[key] ~= b[key] then
return a[key] < b[key]
end
else
return a.severity < b.severity
end
end)
cb(items)
Expand All @@ -54,13 +51,25 @@ end
---@param items Item[]
---@return table<string, Item[]>
function M.group(items)
local ret = {}
local keys = {}
local keyid = 0
local groups = {}
for _, item in ipairs(items) do
if ret[item.filename] == nil then
ret[item.filename] = {}
if groups[item.filename] == nil then
groups[item.filename] = { filename = item.filename, items = {} }
keys[item.filename] = keyid
keyid = keyid + 1
end
table.insert(ret[item.filename], item)
table.insert(groups[item.filename].items, item)
end

local ret = {}
for _, group in pairs(groups) do
table.insert(ret, group)
end
table.sort(ret, function(a, b)
return keys[a.filename] < keys[b.filename]
end)
return ret
end

Expand Down
8 changes: 4 additions & 4 deletions lua/trouble/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ function renderer.render(view, opts)
end

-- render file groups
for filename, group_items in pairs(grouped) do
for _, group in ipairs(grouped) do
if opts.open_folds then
folds.open(filename)
folds.open(group.filename)
end
if opts.close_folds then
folds.close(filename)
folds.close(group.filename)
end
renderer.render_file(view, text, filename, group_items)
renderer.render_file(view, text, group.filename, group.items)
end

view:render(text)
Expand Down

0 comments on commit 4a45782

Please sign in to comment.