write own lightweight tabufline | remove bufferline

This commit is contained in:
siduck 2022-06-24 19:24:59 +05:30
parent 5e4b2e6a11
commit 845d5b4866
11 changed files with 269 additions and 123 deletions

View file

@ -4,7 +4,6 @@
local M = {}
M.options = {
-- load your options here or load module with options
user = function() end,
@ -25,9 +24,11 @@ M.ui = {
statusline = {
separator_style = "default", -- default/round/block/arrow
config = "%!v:lua.require'ui.statusline'.run()",
config = "%!v:lua.require('ui.statusline').run()",
override = {},
},
tabufline_enabled = true,
}
M.plugins = {

View file

@ -4,9 +4,9 @@ vim.cmd "silent! command! NvChadSnapshotCreate lua require('nvchad').snap_create
vim.cmd "silent! command! NvChadSnapshotDelete lua require('nvchad').snap_delete()"
vim.cmd "silent! command! NvChadSnapshotCheckout lua require('nvchad').snap_checkout()"
-- autocmds
local autocmd = vim.api.nvim_create_autocmd
local api = vim.api
-- wrap the PackerSync command to warn people before using it in NvChadSnapshots
autocmd("VimEnter", {
@ -36,12 +36,45 @@ autocmd("BufEnter", {
command = "set fo-=c fo-=r fo-=o",
})
autocmd("InsertLeave", {
callback = function()
if require("luasnip").session.current_nodes[vim.api.nvim_get_current_buf()]
and not require("luasnip").session.jump_active
then
require("luasnip").unlink_current()
vim.t.bufs = vim.api.nvim_list_bufs()
-- thx to https://github.com/ii14 && stores buffer per tab -> table
autocmd({ "BufAdd" }, {
callback = function(args)
if vim.t.bufs == nil then
vim.t.bufs = { args.buf }
else
local bufs = vim.t.bufs
-- check for duplicates
if not vim.tbl_contains(bufs, args.buf) then
table.insert(bufs, args.buf)
vim.t.bufs = bufs
end
end
end,
})
autocmd("BufDelete", {
callback = function(args)
for _, tab in ipairs(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
table.remove(bufs, i)
vim.t[tab].bufs = bufs
break
end
end
end
end
end,
})
if require("core.utils").load_config().ui.tabufline_enabled then
require("core.lazy_load").tabufline()
else
vim.opt.showtabline = 2
vim.opt.tabline = "%!v:lua.require('ui.tabline').run()"
end

View file

@ -1,8 +1,8 @@
-- thx to https://github.com/max397574/omega-nvim/blob/master/lua/omega/modules/ui/bufferline.lua
local M = {}
local autocmd = vim.api.nvim_create_autocmd
M.lazy_load = function(tb)
vim.api.nvim_create_autocmd(tb.events, {
autocmd(tb.events, {
pattern = "*",
group = vim.api.nvim_create_augroup(tb.augroup_name, {}),
callback = function()
@ -23,18 +23,6 @@ M.lazy_load = function(tb)
})
end
M.bufferline = function()
M.lazy_load {
events = { "BufNewFile", "BufRead", "TabEnter" },
augroup_name = "BufferLineLazy",
plugins = "bufferline.nvim",
condition = function()
return #vim.fn.getbufinfo { buflisted = 1 } >= 2
end,
}
end
M.colorizer = function()
M.lazy_load {
events = { "BufRead", "BufNewFile" },
@ -96,7 +84,7 @@ M.treesitter_cmds = {
M.gitsigns = function()
-- taken from https://github.com/max397574
vim.api.nvim_create_autocmd({ "BufRead" }, {
autocmd({ "BufRead" }, {
callback = function()
local function onexit(code, _)
if code == 0 then
@ -119,4 +107,20 @@ M.gitsigns = function()
})
end
M.tabufline = function()
autocmd({ "BufNewFile", "BufRead", "TabEnter" }, {
pattern = "*",
group = vim.api.nvim_create_augroup("TabuflineLazyLoad", {}),
callback = function()
if #vim.fn.getbufinfo { buflisted = 1 } >= 2 then
vim.opt.showtabline = 2
vim.opt.tabline = "%!v:lua.require('ui.tabline').run()"
vim.api.nvim_del_augroup_by_name "TabuflineLazyLoad"
else
return ""
end
end,
})
end
return M

View file

@ -58,15 +58,19 @@ M.general = {
},
}
M.bufferline = {
M.tabufline = {
n = {
-- new buffer
["<S-b>"] = { "<cmd> enew <CR>", "烙 new buffer" },
-- cycle through buffers
["<TAB>"] = { "<cmd> BufferLineCycleNext <CR>", " cycle next buffer" },
["<S-Tab>"] = { "<cmd> BufferLineCyclePrev <CR>", " cycle prev buffer" },
["<TAB>"] = { "<cmd> Tbufnext <CR>", " goto next buffer" },
["<S-Tab>"] = { "<cmd> Tbufprev <CR> ", " goto prev buffer" },
-- cycle through tabs
["<leader>tp"] = { "<cmd> tabprevious <CR>", " goto next tab" },
["<leader>tn"] = { "<cmd> tabnext <CR> ", " goto prev tab" },
-- close buffer + hide terminal buffer
["<leader>x"] = {

View file

@ -1,30 +1,28 @@
local M = {}
local api = vim.api
local fn = vim.fn
local merge_tb = vim.tbl_deep_extend
M.close_buffer = function(force)
M.close_buffer = function(bufnr)
if vim.bo.buftype == "terminal" then
force = force or #api.nvim_list_wins() < 2 and ":bd!"
local swap = force and #api.nvim_list_bufs() > 1 and ":bp | bd!" .. fn.bufnr()
return vim.cmd(swap or force or "hide")
end
local fileExists = fn.filereadable(fn.expand "%p")
local modified = api.nvim_buf_get_option(fn.bufnr(), "modified")
-- if file doesnt exist & its modified
if fileExists == 0 and modified then
print "no file name? add it now!"
if vim.bo.buflisted then
vim.bo.buflisted = false
vim.cmd "enew"
else
vim.cmd "hide"
end
return
end
force = force or not vim.bo.buflisted or vim.bo.buftype == "nofile"
-- if file doesnt exist & its modified
if vim.bo.modified then
print "save the file!"
return
end
-- if not force, change to prev buf and then close current
local close_cmd = force and ":bd!" or ":bp | bd" .. fn.bufnr()
vim.cmd(close_cmd)
bufnr = bufnr or api.nvim_get_current_buf()
require("core.utils").tabuflinePrev()
vim.cmd("bd" .. bufnr)
end
M.load_config = function()
@ -151,9 +149,7 @@ M.load_override = function(default_table, plugin_name)
if type(user_table) == "function" then
user_table = user_table()
end
if type(user_table) == "table" then
elseif type(user_table) == "table" then
default_table = merge_tb("force", default_table, user_table)
else
default_table = default_table
@ -175,13 +171,16 @@ M.packer_sync = function(...)
vim.api.nvim_echo({
{ "WARNING: You are trying to use ", "WarningMsg" },
{ "PackerSync" },
{ " on a NvChadSnapshot. This will cause issues if NvChad dependencies contain "
.. "any breaking changes! Plugin updates will not be included in this "
.. "snapshot, so they will be lost after switching between snapshots! Would "
.. "you still like to continue? [y/N]\n", "WarningMsg" }
{
" on a NvChadSnapshot. This will cause issues if NvChad dependencies contain "
.. "any breaking changes! Plugin updates will not be included in this "
.. "snapshot, so they will be lost after switching between snapshots! Would "
.. "you still like to continue? [y/N]\n",
"WarningMsg",
},
}, false, {})
local ans = vim.trim(string.lower(vim.fn.input("-> ")))
local ans = vim.trim(string.lower(vim.fn.input "-> "))
if ans ~= "y" then
return
@ -192,7 +191,39 @@ M.packer_sync = function(...)
if packer_exists then
packer.sync(...)
else
error("Packer could not be loaded!")
error "Packer could not be loaded!"
end
end
M.tabuflineNext = function()
local bufs = vim.t.bufs or {}
for i, v in ipairs(bufs) do
if api.nvim_get_current_buf() == v then
vim.cmd(i == #bufs and "b" .. bufs[1] or "b" .. bufs[i + 1])
break
end
end
end
M.tabuflinePrev = function()
local bufs = vim.t.bufs or {}
for i, v in ipairs(bufs) do
if api.nvim_get_current_buf() == v then
vim.cmd(i == 1 and "b" .. bufs[#bufs] or "b" .. bufs[i - 1])
break
end
end
end
-- closes tab + all of its buffers
M.tabuflineCloseTab = function()
local bufs = vim.t.bufs or {}
vim.cmd "tabclose"
for _, buf in ipairs(bufs) do
vim.cmd("bd" .. buf)
end
end