From ea9a5e331b70cf4011081c951015033f0079a0cc Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 10 Dec 2021 10:48:54 +0100 Subject: [PATCH] feat: sort files by current directory and prefer non-hidden --- lua/trouble/providers/init.lua | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lua/trouble/providers/init.lua b/lua/trouble/providers/init.lua index 0e7c07a8..c8ee9aca 100644 --- a/lua/trouble/providers/init.lua +++ b/lua/trouble/providers/init.lua @@ -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)