From 62b9c09f44dd2e0b4b44bdbe7e4f7347c935589d Mon Sep 17 00:00:00 2001 From: siduck Date: Sun, 26 Jun 2022 06:24:52 +0530 Subject: [PATCH] fix tabufline crash when formatting buf | (#1265) --- init.lua | 6 +----- lua/core/init.lua | 4 ++-- lua/core/lazy_load.lua | 2 +- lua/core/utils.lua | 16 ++++++++++++++-- lua/plugins/configs/nvimtree.lua | 1 + lua/ui/tabline.lua | 28 ++++++++++++++++------------ 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/init.lua b/init.lua index 467fb0f..2237c5c 100644 --- a/init.lua +++ b/init.lua @@ -9,8 +9,4 @@ end, 0) require("core.packer").bootstrap() require "plugins" -local user_conf, _ = pcall(require, "custom") - -if user_conf then - require "custom" -end +pcall(require, "custom") diff --git a/lua/core/init.lua b/lua/core/init.lua index 7fdf605..d731be5 100644 --- a/lua/core/init.lua +++ b/lua/core/init.lua @@ -39,7 +39,7 @@ autocmd("BufEnter", { vim.t.bufs = vim.api.nvim_list_bufs() -- thx to https://github.com/ii14 & stores buffer per tab -> table -autocmd({ "BufAdd" }, { +autocmd("BufAdd", { callback = function(args) if vim.t.bufs == nil then vim.t.bufs = { args.buf } @@ -76,5 +76,5 @@ if require("core.utils").load_config().ui.tabufline_lazyloaded then require("core.lazy_load").tabufline() else vim.opt.showtabline = 2 - vim.opt.tabline = "%!v:lua.require('ui.tabline').run()" + vim.opt.tabline = "%!v:lua.require'ui.tabline'.run()" end diff --git a/lua/core/lazy_load.lua b/lua/core/lazy_load.lua index 873b593..036be76 100644 --- a/lua/core/lazy_load.lua +++ b/lua/core/lazy_load.lua @@ -114,7 +114,7 @@ M.tabufline = function() callback = function() if #vim.fn.getbufinfo { buflisted = 1 } >= 2 then vim.opt.showtabline = 2 - vim.opt.tabline = "%!v:lua.require('ui.tabline').run()" + vim.opt.tabline = "%!v:lua.require'ui.tabline'.run()" vim.api.nvim_del_augroup_by_name "TabuflineLazyLoad" end end, diff --git a/lua/core/utils.lua b/lua/core/utils.lua index b1c09c8..59d0ec5 100644 --- a/lua/core/utils.lua +++ b/lua/core/utils.lua @@ -195,8 +195,20 @@ M.packer_sync = function(...) end end +M.bufilter = function() + local bufs = vim.t.bufs + + for i = #bufs, 1, -1 do + if not vim.api.nvim_buf_is_loaded(bufs[i]) then + table.remove(bufs, i) + end + end + + return bufs +end + M.tabuflineNext = function() - local bufs = vim.t.bufs or {} + local bufs = M.bufilter() or {} for i, v in ipairs(bufs) do if api.nvim_get_current_buf() == v then @@ -207,7 +219,7 @@ M.tabuflineNext = function() end M.tabuflinePrev = function() - local bufs = vim.t.bufs or {} + local bufs = M.bufilter() or {} for i, v in ipairs(bufs) do if api.nvim_get_current_buf() == v then diff --git a/lua/plugins/configs/nvimtree.lua b/lua/plugins/configs/nvimtree.lua index 1bea58a..fdfb636 100644 --- a/lua/plugins/configs/nvimtree.lua +++ b/lua/plugins/configs/nvimtree.lua @@ -9,6 +9,7 @@ require("base46").load_highlight "nvimtree" local options = { filters = { dotfiles = false, + exclude = { vim.fn.stdpath "config" .. "/lua/custom" }, }, disable_netrw = true, hijack_netrw = true, diff --git a/lua/ui/tabline.lua b/lua/ui/tabline.lua index 09112a2..900b692 100644 --- a/lua/ui/tabline.lua +++ b/lua/ui/tabline.lua @@ -74,20 +74,24 @@ local function bufferlist() local buffers = "" for _, nr in ipairs(vim.t.bufs or {}) do -- buf = bufnr - local name = (#api.nvim_buf_get_name(nr) ~= 0) and fn.fnamemodify(api.nvim_buf_get_name(nr), ":t") or " No Name " - local close_btn = "%" .. nr .. "@TbKillBuf@ %X" - name = "%" .. nr .. "@TbGoToBuf@" .. add_fileInfo(name, nr) .. "%X" + if api.nvim_buf_is_loaded(nr) then + local name = (#api.nvim_buf_get_name(nr) ~= 0) and fn.fnamemodify(api.nvim_buf_get_name(nr), ":t") + or " No Name " + local close_btn = "%" .. nr .. "@TbKillBuf@ %X" + name = "%" .. nr .. "@TbGoToBuf@" .. add_fileInfo(name, nr) .. "%X" - -- color close btn for focused / hidden buffers - if nr == api.nvim_get_current_buf() then - close_btn = (vim.bo[0].modified and "%#TbLineBufOnModified# ") or ("%#TbLineBufOnClose#" .. close_btn) - name = "%#TbLineBufOn#" .. name .. close_btn - else - close_btn = (vim.bo[nr].modified and "%#TbBufLineBufOffModified# ") or ("%#TbLineBufOffClose#" .. close_btn) - name = "%#TbLineBufOff#" .. name .. close_btn + -- color close btn for focused / hidden buffers + if nr == api.nvim_get_current_buf() then + close_btn = (vim.bo[0].modified and "%#TbLineBufOnModified# ") or ("%#TbLineBufOnClose#" .. close_btn) + name = "%#TbLineBufOn#" .. name .. close_btn + else + close_btn = (vim.bo[nr].modified and "%#TbBufLineBufOffModified# ") + or ("%#TbLineBufOffClose#" .. close_btn) + name = "%#TbLineBufOff#" .. name .. close_btn + end + + buffers = buffers .. name end - - buffers = buffers .. name end return buffers .. "%#TblineFill#" .. "%=" -- buffers + empty space