bufferline.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. local M = {}
  2. local function is_ft(b, ft)
  3. return vim.bo[b].filetype == ft
  4. end
  5. local function diagnostics_indicator(num, _, diagnostics, _)
  6. local result = {}
  7. local symbols = { error = "", warning = "", info = "" }
  8. if not lvim.use_icons then
  9. return "(" .. num .. ")"
  10. end
  11. for name, count in pairs(diagnostics) do
  12. if symbols[name] and count > 0 then
  13. table.insert(result, symbols[name] .. " " .. count)
  14. end
  15. end
  16. result = table.concat(result, " ")
  17. return #result > 0 and result or ""
  18. end
  19. local function custom_filter(buf, buf_nums)
  20. local logs = vim.tbl_filter(function(b)
  21. return is_ft(b, "log")
  22. end, buf_nums)
  23. if vim.tbl_isempty(logs) then
  24. return true
  25. end
  26. local tab_num = vim.fn.tabpagenr()
  27. local last_tab = vim.fn.tabpagenr "$"
  28. local is_log = is_ft(buf, "log")
  29. if last_tab == 1 then
  30. return true
  31. end
  32. -- only show log buffers in secondary tabs
  33. return (tab_num == last_tab and is_log) or (tab_num ~= last_tab and not is_log)
  34. end
  35. M.config = function()
  36. lvim.builtin.bufferline = {
  37. active = true,
  38. on_config_done = nil,
  39. keymap = {
  40. normal_mode = {},
  41. },
  42. highlights = {
  43. background = {
  44. italic = true,
  45. },
  46. buffer_selected = {
  47. bold = true,
  48. },
  49. },
  50. options = {
  51. mode = "buffers", -- set to "tabs" to only show tabpages instead
  52. numbers = "none", -- can be "none" | "ordinal" | "buffer_id" | "both" | function
  53. close_command = "bdelete! %d", -- can be a string | function, see "Mouse actions"
  54. right_mouse_command = "vert sbuffer %d", -- can be a string | function, see "Mouse actions"
  55. left_mouse_command = "buffer %d", -- can be a string | function, see "Mouse actions"
  56. middle_mouse_command = nil, -- can be a string | function, see "Mouse actions"
  57. indicator = {
  58. icon = "▎", -- this should be omitted if indicator style is not 'icon'
  59. style = "icon", -- can also be 'underline'|'none',
  60. },
  61. buffer_close_icon = "",
  62. modified_icon = "●",
  63. close_icon = "",
  64. left_trunc_marker = "",
  65. right_trunc_marker = "",
  66. --- name_formatter can be used to change the buffer's label in the bufferline.
  67. --- Please note some names can/will break the
  68. --- bufferline so use this at your discretion knowing that it has
  69. --- some limitations that will *NOT* be fixed.
  70. name_formatter = function(buf) -- buf contains a "name", "path" and "bufnr"
  71. -- remove extension from markdown files for example
  72. if buf.name:match "%.md" then
  73. return vim.fn.fnamemodify(buf.name, ":t:r")
  74. end
  75. end,
  76. max_name_length = 18,
  77. max_prefix_length = 15, -- prefix used when a buffer is de-duplicated
  78. truncate_names = true, -- whether or not tab names should be truncated
  79. tab_size = 18,
  80. diagnostics = "nvim_lsp",
  81. diagnostics_update_in_insert = false,
  82. diagnostics_indicator = diagnostics_indicator,
  83. -- NOTE: this will be called a lot so don't do any heavy processing here
  84. custom_filter = custom_filter,
  85. offsets = {
  86. {
  87. filetype = "undotree",
  88. text = "Undotree",
  89. highlight = "PanelHeading",
  90. padding = 1,
  91. },
  92. {
  93. filetype = "NvimTree",
  94. text = "Explorer",
  95. highlight = "PanelHeading",
  96. padding = 1,
  97. },
  98. {
  99. filetype = "DiffviewFiles",
  100. text = "Diff View",
  101. highlight = "PanelHeading",
  102. padding = 1,
  103. },
  104. {
  105. filetype = "flutterToolsOutline",
  106. text = "Flutter Outline",
  107. highlight = "PanelHeading",
  108. },
  109. {
  110. filetype = "packer",
  111. text = "Packer",
  112. highlight = "PanelHeading",
  113. padding = 1,
  114. },
  115. },
  116. color_icons = true, -- whether or not to add the filetype icon highlights
  117. show_buffer_icons = lvim.use_icons, -- disable filetype icons for buffers
  118. show_buffer_close_icons = lvim.use_icons,
  119. show_close_icon = false,
  120. show_tab_indicators = true,
  121. persist_buffer_sort = true, -- whether or not custom sorted buffers should persist
  122. -- can also be a table containing 2 custom separators
  123. -- [focused and unfocused]. eg: { '|', '|' }
  124. separator_style = "thin",
  125. enforce_regular_tabs = false,
  126. always_show_bufferline = false,
  127. hover = {
  128. enabled = false, -- requires nvim 0.8+
  129. delay = 200,
  130. reveal = { "close" },
  131. },
  132. sort_by = "id",
  133. },
  134. }
  135. end
  136. M.setup = function()
  137. require("lvim.keymappings").load(lvim.builtin.bufferline.keymap)
  138. local status_ok, bufferline = pcall(require, "bufferline")
  139. if not status_ok then
  140. return
  141. end
  142. bufferline.setup {
  143. options = lvim.builtin.bufferline.options,
  144. highlights = lvim.builtin.bufferline.highlights,
  145. }
  146. if lvim.builtin.bufferline.on_config_done then
  147. lvim.builtin.bufferline.on_config_done()
  148. end
  149. end
  150. --stylua: ignore
  151. -- Common kill function for bdelete and bwipeout
  152. -- credits: based on bbye and nvim-bufdel
  153. ---@param kill_command? string defaults to "bd"
  154. ---@param bufnr? number defaults to the current buffer
  155. ---@param force? boolean defaults to false
  156. function M.buf_kill(kill_command, bufnr, force)
  157. kill_command = kill_command or "bd"
  158. local bo = vim.bo
  159. local api = vim.api
  160. local fmt = string.format
  161. local fnamemodify = vim.fn.fnamemodify
  162. if bufnr == 0 or bufnr == nil then
  163. bufnr = api.nvim_get_current_buf()
  164. end
  165. local bufname = api.nvim_buf_get_name(bufnr)
  166. if not force then
  167. local warning
  168. if bo[bufnr].modified then
  169. warning = fmt([[No write since last change for (%s)]], fnamemodify(bufname, ":t"))
  170. elseif api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then
  171. warning = fmt([[Terminal %s will be killed]], bufname)
  172. end
  173. if warning then
  174. vim.ui.input({
  175. prompt = string.format([[%s. Close it anyway? [y]es or [n]o (default: no): ]], warning),
  176. }, function(choice)
  177. if choice:match "ye?s?" then force = true end
  178. end)
  179. if not force then return end
  180. end
  181. end
  182. -- Get list of windows IDs with the buffer to close
  183. local windows = vim.tbl_filter(function(win)
  184. return api.nvim_win_get_buf(win) == bufnr
  185. end, api.nvim_list_wins())
  186. if #windows == 0 then return end
  187. if force then
  188. kill_command = kill_command .. "!"
  189. end
  190. -- Get list of active buffers
  191. local buffers = vim.tbl_filter(function(buf)
  192. return api.nvim_buf_is_valid(buf) and bo[buf].buflisted
  193. end, api.nvim_list_bufs())
  194. -- If there is only one buffer (which has to be the current one), vim will
  195. -- create a new buffer on :bd.
  196. -- For more than one buffer, pick the previous buffer (wrapping around if necessary)
  197. if #buffers > 1 then
  198. for i, v in ipairs(buffers) do
  199. if v == bufnr then
  200. local prev_buf_idx = i == 1 and (#buffers - 1) or (i - 1)
  201. local prev_buffer = buffers[prev_buf_idx]
  202. for _, win in ipairs(windows) do
  203. api.nvim_win_set_buf(win, prev_buffer)
  204. end
  205. end
  206. end
  207. end
  208. -- Check if buffer still exists, to ensure the target buffer wasn't killed
  209. -- due to options like bufhidden=wipe.
  210. if api.nvim_buf_is_valid(bufnr) and bo[bufnr].buflisted then
  211. vim.cmd(string.format("%s %d", kill_command, bufnr))
  212. end
  213. end
  214. return M