feat: add <leader>bp to switch to previous buffer in tabline

This commit is contained in:
Micah Halter
2023-06-23 07:56:01 -04:00
parent 3908a51a11
commit 52fcb15cb6
4 changed files with 46 additions and 8 deletions
+10 -2
View File
@@ -19,13 +19,19 @@ autocmd({ "BufAdd", "BufEnter", "TabNewEntered" }, {
desc = "Update buffers when adding new buffers",
group = bufferline_group,
callback = function(args)
local buf_utils = require "astronvim.utils.buffer"
if not buf_utils.is_valid(args.buf) then return end
if args.buf ~= buf_utils.current_buf then
buf_utils.last_buf = buf_utils.current_buf
buf_utils.current_buf = args.buf
end
if not vim.t.bufs then vim.t.bufs = {} end
local bufs = vim.t.bufs
if not vim.tbl_contains(bufs, args.buf) then
table.insert(bufs, args.buf)
vim.t.bufs = bufs
end
vim.t.bufs = vim.tbl_filter(require("astronvim.utils.buffer").is_valid, vim.t.bufs)
vim.t.bufs = vim.tbl_filter(buf_utils.is_valid, vim.t.bufs)
astroevent "BufsUpdated"
end,
})
@@ -33,11 +39,13 @@ autocmd("BufDelete", {
desc = "Update buffers when deleting buffers",
group = bufferline_group,
callback = function(args)
local removed
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
local bufs = vim.t[tab].bufs
if bufs then
for i, bufnr in ipairs(bufs) do
if bufnr == args.buf then
removed = true
table.remove(bufs, i)
vim.t[tab].bufs = bufs
break
@@ -46,7 +54,7 @@ autocmd("BufDelete", {
end
end
vim.t.bufs = vim.tbl_filter(require("astronvim.utils.buffer").is_valid, vim.t.bufs)
astroevent "BufsUpdated"
if removed then astroevent "BufsUpdated" end
vim.cmd.redrawtabline()
end,
})
+1
View File
@@ -84,6 +84,7 @@ maps.n["<leader>bd"] = {
}
maps.n["<leader>bl"] =
{ function() require("astronvim.utils.buffer").close_left() end, desc = "Close all buffers to the left" }
maps.n["<leader>bp"] = { function() require("astronvim.utils.buffer").prev() end, desc = "Previous buffer" }
maps.n["<leader>br"] =
{ function() require("astronvim.utils.buffer").close_right() end, desc = "Close all buffers to the right" }
maps.n["<leader>bs"] = sections.bs
+22 -4
View File
@@ -10,6 +10,11 @@
local M = {}
local utils = require "astronvim.utils"
--- Placeholders for keeping track of most recent and previous buffer
M.current_buf, M.last_buf = nil, nil
-- TODO: Add user configuration table for this once resession is default
--- Configuration table for controlling session options
M.sessions = {
@@ -91,7 +96,7 @@ function M.move(n)
end
end
vim.t.bufs = bufs -- set buffers
require("astronvim.utils").event "BufsUpdated"
utils.event "BufsUpdated"
vim.cmd.redrawtabline() -- redraw tabline
end
@@ -111,12 +116,25 @@ end
---@param tabnr number The position of the buffer to navigate to
function M.nav_to(tabnr) vim.cmd.b(vim.t.bufs[tabnr]) end
--- Navigate to the previously used buffer
function M.prev()
if vim.fn.bufnr() == M.current_buf then
if M.last_buf then
vim.cmd.b(M.last_buf)
else
utils.notify "No previous buffer found"
end
else
utils.notify "Must be in a main editor window to switch the window buffer"
end
end
--- Close a given buffer
---@param bufnr? number The buffer to close or the current buffer if not provided
---@param force? boolean Whether or not to foce close the buffers or confirm changes (default: false)
function M.close(bufnr, force)
if force == nil then force = false end
if require("astronvim.utils").is_available "bufdelete.nvim" then
if utils.is_available "bufdelete.nvim" then
require("bufdelete").bufdelete(bufnr, force)
else
vim.cmd((force and "bd!" or "confirm bd") .. (bufnr == nil and "" or bufnr))
@@ -165,7 +183,7 @@ function M.sort(compare_func, skip_autocmd)
local bufs = vim.t.bufs
table.sort(bufs, compare_func)
vim.t.bufs = bufs
if not skip_autocmd then require("astronvim.utils").event "BufsUpdated" end
if not skip_autocmd then utils.event "BufsUpdated" end
vim.cmd.redrawtabline()
return true
end
@@ -176,7 +194,7 @@ end
function M.close_tab()
if #vim.api.nvim_list_tabpages() > 1 then
vim.t.bufs = nil
require("astronvim.utils").event "BufsUpdated"
utils.event "BufsUpdated"
vim.cmd.tabclose()
end
end
+13 -2
View File
@@ -2,7 +2,12 @@ local M = {}
M.on_save = function()
-- initiate astronvim data
local data = { last_bufnr = vim.fn.bufnr(), bufnrs = {}, tabs = {} }
local data = { bufnrs = {}, tabs = {} }
local buf_utils = require "astronvim.utils.buffer"
data.current_buf = buf_utils.current_buf
data.last_buf = buf_utils.last_buf
-- save tab scoped buffers and buffer numbers from buffer name
for new_tabpage, tabpage in ipairs(vim.api.nvim_list_tabpages()) do
@@ -26,8 +31,14 @@ M.on_load = function(data)
for tabpage, tabs in pairs(data.tabs) do
vim.t[tabpage].bufs = vim.tbl_map(function(bufnr) return new_bufnrs[bufnr] end, tabs)
end
local buf_utils = require "astronvim.utils.buffer"
buf_utils.current_buf = new_bufnrs[data.current_buf]
buf_utils.last_buf = new_bufnrs[data.last_buf]
require("astronvim.utils").event "BufsUpdated"
if vim.fn.bufnr() ~= new_bufnrs[data.last_bufnr] then vim.cmd.b(new_bufnrs[data.last_bufnr]) end
if vim.fn.bufnr() ~= buf_utils.current_buf then vim.cmd.b(buf_utils.current_buf) end
end
return M