Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sort by MRU #149 #535

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/bufferline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ local function setup_commands()
cmd("BufferLineMovePrev", function() M.move(-1) end, {})
cmd("BufferLineSortByExtension", function() M.sort_buffers_by("extension") end, {})
cmd("BufferLineSortByDirectory", function() M.sort_buffers_by("directory") end, {})
cmd("BufferLineSortByMru", function() M.sort_buffers_by("mru") end, {})
cmd(
"BufferLineSortByRelativeDirectory",
function() M.sort_buffers_by("relative_directory") end,
Expand Down
6 changes: 6 additions & 0 deletions lua/bufferline/sorters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ local function sort_by_id(buf_a, buf_b)
return buf_a.id < buf_b.id
end

local function sort_by_mru(buf_a, buf_b)
return vim.fn.getbufinfo(buf_a.id)[1].lastused > vim.fn.getbufinfo(buf_b.id)[1].lastused
end

--- @param buf NvimBuffer
local function init_buffer_tabnr(buf)
local maxinteger = 1000000000
Expand Down Expand Up @@ -140,6 +144,8 @@ function M.sort(elements, sort_by, state)
table.sort(elements, sort_by_id)
elseif sort_by == "tabs" then
table.sort(elements, config:is_tabline() and sort_by_id or sort_by_tabs)
elseif sort_by == "mru" then
table.sort(elements, sort_by_mru)
elseif type(sort_by) == "function" then
table.sort(elements, sort_by)
end
Expand Down