feat: add sort_by "filetype" (#2302)

* feat: adds new type of sorting based on vim's filetype detection

* fix(ft/sorter): fallbacks to C.name when both ft's are nil or equal

* feat: adds new type of sorting based on vim's filetype detection

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Cyber Oliveira
2023-07-08 23:31:06 -03:00
committed by GitHub
parent 4af572246c
commit 3d2fd90b28
2 changed files with 30 additions and 2 deletions

View File

@@ -80,6 +80,7 @@ function M.sort(t)
absolute_path = n.absolute_path,
executable = n.executable,
extension = n.extension,
filetype = vim.filetype.match { filename = n.name },
link_to = n.link_to,
name = n.name,
type = n.type,
@@ -191,6 +192,32 @@ function C.extension(a, b)
return a.extension:lower() <= b.extension:lower()
end
function C.filetype(a, b)
local a_ft = vim.filetype.match { filename = a.name }
local b_ft = vim.filetype.match { filename = b.name }
-- directories first
if a.nodes and not b.nodes then
return true
elseif not a.nodes and b.nodes then
return false
end
-- one is nil, the other wins
if a_ft and not b_ft then
return true
elseif not a_ft and b_ft then
return false
end
-- same filetype or both nil, sort by name
if a_ft == b_ft then
return C.name(a, b)
end
return a_ft < b_ft
end
function M.setup(opts)
M.config = {}
M.config.sort_by = opts.sort_by