Skip to content

Commit

Permalink
feat: sort files by current directory and prefer non-hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 10, 2021
1 parent e44db4c commit ea9a5e3
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lua/trouble/providers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,30 @@ function M.get(win, buf, cb, options)
return {}
end

local sort_keys = { "severity", "filename", "lnum", "col" }
local sort_keys = {
function(item)
local cwd = vim.loop.fs_realpath(vim.fn.getcwd())
local path = vim.loop.fs_realpath(item.filename)
local ret = string.find(path, cwd, 1, true) == 1 and 10 or 100
-- prefer non-hidden files
if string.find(path, ".") then
ret = ret + 1
end
return ret
end,
"severity",
"filename",
"lnum",
"col",
}

provider(win, buf, function(items)
table.sort(items, function(a, b)
for _, key in ipairs(sort_keys) do
if a[key] ~= b[key] then
return a[key] < b[key]
local ak = type(key) == "string" and a[key] or key(a)
local bk = type(key) == "string" and b[key] or key(b)
if ak ~= bk then
return ak < bk
end
end
end)
Expand Down

0 comments on commit ea9a5e3

Please sign in to comment.