bufferline.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. return M