breadcrumbs.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. local M = {}
  2. -- local Log = require "lvim.core.log"
  3. local icons = lvim.icons.kind
  4. M.config = function()
  5. lvim.builtin.breadcrumbs = {
  6. active = true,
  7. on_config_done = nil,
  8. winbar_filetype_exclude = {
  9. "help",
  10. "startify",
  11. "dashboard",
  12. "lazy",
  13. "neo-tree",
  14. "neogitstatus",
  15. "NvimTree",
  16. "Trouble",
  17. "alpha",
  18. "lir",
  19. "Outline",
  20. "spectre_panel",
  21. "toggleterm",
  22. "DressingSelect",
  23. "Jaq",
  24. "harpoon",
  25. "dap-repl",
  26. "dap-terminal",
  27. "dapui_console",
  28. "dapui_hover",
  29. "lab",
  30. "notify",
  31. "noice",
  32. "neotest-summary",
  33. "",
  34. },
  35. options = {
  36. icons = {
  37. Array = icons.Array .. " ",
  38. Boolean = icons.Boolean,
  39. Class = icons.Class .. " ",
  40. Color = icons.Color .. " ",
  41. Constant = icons.Constant .. " ",
  42. Constructor = icons.Constructor .. " ",
  43. Enum = icons.Enum .. " ",
  44. EnumMember = icons.EnumMember .. " ",
  45. Event = icons.Event .. " ",
  46. Field = icons.Field .. " ",
  47. File = icons.File .. " ",
  48. Folder = icons.Folder .. " ",
  49. Function = icons.Function .. " ",
  50. Interface = icons.Interface .. " ",
  51. Key = icons.Key .. " ",
  52. Keyword = icons.Keyword .. " ",
  53. Method = icons.Method .. " ",
  54. Module = icons.Module .. " ",
  55. Namespace = icons.Namespace .. " ",
  56. Null = icons.Null .. " ",
  57. Number = icons.Number .. " ",
  58. Object = icons.Object .. " ",
  59. Operator = icons.Operator .. " ",
  60. Package = icons.Package .. " ",
  61. Property = icons.Property .. " ",
  62. Reference = icons.Reference .. " ",
  63. Snippet = icons.Snippet .. " ",
  64. String = icons.String .. " ",
  65. Struct = icons.Struct .. " ",
  66. Text = icons.Text .. " ",
  67. TypeParameter = icons.TypeParameter .. " ",
  68. Unit = icons.Unit .. " ",
  69. Value = icons.Value .. " ",
  70. Variable = icons.Variable .. " ",
  71. },
  72. highlight = true,
  73. separator = " " .. lvim.icons.ui.ChevronRight .. " ",
  74. depth_limit = 0,
  75. depth_limit_indicator = "..",
  76. },
  77. }
  78. end
  79. M.setup = function()
  80. local status_ok, navic = pcall(require, "nvim-navic")
  81. if not status_ok then
  82. return
  83. end
  84. M.create_winbar()
  85. navic.setup(lvim.builtin.breadcrumbs.options)
  86. if lvim.builtin.breadcrumbs.on_config_done then
  87. lvim.builtin.breadcrumbs.on_config_done()
  88. end
  89. end
  90. M.get_filename = function()
  91. local filename = vim.fn.expand "%:t"
  92. local extension = vim.fn.expand "%:e"
  93. local f = require "lvim.utils.functions"
  94. if not f.isempty(filename) then
  95. local file_icon, hl_group
  96. local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
  97. if lvim.use_icons and devicons_ok then
  98. file_icon, hl_group = devicons.get_icon(filename, extension, { default = true })
  99. if f.isempty(file_icon) then
  100. file_icon = lvim.icons.kind.File
  101. end
  102. else
  103. file_icon = ""
  104. hl_group = "Normal"
  105. end
  106. local buf_ft = vim.bo.filetype
  107. if buf_ft == "dapui_breakpoints" then
  108. file_icon = lvim.icons.ui.Bug
  109. end
  110. if buf_ft == "dapui_stacks" then
  111. file_icon = lvim.icons.ui.Stacks
  112. end
  113. if buf_ft == "dapui_scopes" then
  114. file_icon = lvim.icons.ui.Scopes
  115. end
  116. if buf_ft == "dapui_watches" then
  117. file_icon = lvim.icons.ui.Watches
  118. end
  119. -- if buf_ft == "dapui_console" then
  120. -- file_icon = lvim.icons.ui.DebugConsole
  121. -- end
  122. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  123. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  124. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  125. end
  126. end
  127. local get_gps = function()
  128. local status_gps_ok, gps = pcall(require, "nvim-navic")
  129. if not status_gps_ok then
  130. return ""
  131. end
  132. local status_ok, gps_location = pcall(gps.get_location, {})
  133. if not status_ok then
  134. return ""
  135. end
  136. if not gps.is_available() or gps_location == "error" then
  137. return ""
  138. end
  139. if not require("lvim.utils.functions").isempty(gps_location) then
  140. return "%#NavicSeparator#" .. lvim.icons.ui.ChevronRight .. "%* " .. gps_location
  141. else
  142. return ""
  143. end
  144. end
  145. local excludes = function()
  146. return vim.tbl_contains(lvim.builtin.breadcrumbs.winbar_filetype_exclude or {}, vim.bo.filetype)
  147. end
  148. M.get_winbar = function()
  149. if excludes() then
  150. return
  151. end
  152. local f = require "lvim.utils.functions"
  153. local value = M.get_filename()
  154. local gps_added = false
  155. if not f.isempty(value) then
  156. local gps_value = get_gps()
  157. value = value .. " " .. gps_value
  158. if not f.isempty(gps_value) then
  159. gps_added = true
  160. end
  161. end
  162. if not f.isempty(value) and f.get_buf_option "mod" then
  163. -- TODO: replace with circle
  164. local mod = "%#LspCodeLens#" .. lvim.icons.ui.Circle .. "%*"
  165. if gps_added then
  166. value = value .. " " .. mod
  167. else
  168. value = value .. mod
  169. end
  170. end
  171. local num_tabs = #vim.api.nvim_list_tabpages()
  172. if num_tabs > 1 and not f.isempty(value) then
  173. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  174. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  175. end
  176. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  177. if not status_ok then
  178. return
  179. end
  180. end
  181. M.create_winbar = function()
  182. vim.api.nvim_create_augroup("_winbar", {})
  183. if vim.fn.has "nvim-0.8" == 1 then
  184. vim.api.nvim_create_autocmd({
  185. "CursorHoldI",
  186. "CursorHold",
  187. "BufWinEnter",
  188. "BufFilePost",
  189. "InsertEnter",
  190. "BufWritePost",
  191. "TabClosed",
  192. "TabEnter",
  193. }, {
  194. group = "_winbar",
  195. callback = function()
  196. if lvim.builtin.breadcrumbs.active then
  197. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  198. if not status_ok then
  199. -- TODO:
  200. require("lvim.core.breadcrumbs").get_winbar()
  201. end
  202. end
  203. end,
  204. })
  205. end
  206. end
  207. return M