bufferline.lua 6.3 KB

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