breadcrumbs.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. local function isempty(s)
  91. return s == nil or s == ""
  92. end
  93. M.get_filename = function()
  94. local filename = vim.fn.expand "%:t"
  95. local extension = vim.fn.expand "%:e"
  96. if not isempty(filename) then
  97. local file_icon, hl_group
  98. local devicons_ok, devicons = pcall(require, "nvim-web-devicons")
  99. if lvim.use_icons and devicons_ok then
  100. file_icon, hl_group = devicons.get_icon(filename, extension, { default = true })
  101. if isempty(file_icon) then
  102. file_icon = lvim.icons.kind.File
  103. end
  104. else
  105. file_icon = ""
  106. hl_group = "Normal"
  107. end
  108. local buf_ft = vim.bo.filetype
  109. if buf_ft == "dapui_breakpoints" then
  110. file_icon = lvim.icons.ui.Bug
  111. end
  112. if buf_ft == "dapui_stacks" then
  113. file_icon = lvim.icons.ui.Stacks
  114. end
  115. if buf_ft == "dapui_scopes" then
  116. file_icon = lvim.icons.ui.Scopes
  117. end
  118. if buf_ft == "dapui_watches" then
  119. file_icon = lvim.icons.ui.Watches
  120. end
  121. -- if buf_ft == "dapui_console" then
  122. -- file_icon = lvim.icons.ui.DebugConsole
  123. -- end
  124. local navic_text_hl = vim.api.nvim_get_hl(0, { name = "Normal" })
  125. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text_hl.fg })
  126. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  127. end
  128. end
  129. local get_gps = function()
  130. local status_gps_ok, gps = pcall(require, "nvim-navic")
  131. if not status_gps_ok then
  132. return ""
  133. end
  134. local status_ok, gps_location = pcall(gps.get_location, {})
  135. if not status_ok then
  136. return ""
  137. end
  138. if not gps.is_available() or gps_location == "error" then
  139. return ""
  140. end
  141. if not isempty(gps_location) then
  142. return "%#NavicSeparator#" .. lvim.icons.ui.ChevronRight .. "%* " .. gps_location
  143. else
  144. return ""
  145. end
  146. end
  147. local excludes = function()
  148. return vim.tbl_contains(lvim.builtin.breadcrumbs.winbar_filetype_exclude or {}, vim.bo.filetype)
  149. end
  150. M.get_winbar = function()
  151. if excludes() then
  152. return
  153. end
  154. local value = M.get_filename()
  155. local gps_added = false
  156. if not isempty(value) then
  157. local gps_value = get_gps()
  158. value = value .. " " .. gps_value
  159. if not isempty(gps_value) then
  160. gps_added = true
  161. end
  162. end
  163. if not isempty(value) and vim.api.nvim_get_option_value("mod", { buf = 0 }) then
  164. -- TODO: replace with circle
  165. local mod = "%#LspCodeLens#" .. lvim.icons.ui.Circle .. "%*"
  166. if gps_added then
  167. value = value .. " " .. mod
  168. else
  169. value = value .. mod
  170. end
  171. end
  172. local num_tabs = #vim.api.nvim_list_tabpages()
  173. if num_tabs > 1 and not isempty(value) then
  174. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  175. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  176. end
  177. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  178. if not status_ok then
  179. return
  180. end
  181. end
  182. M.create_winbar = function()
  183. vim.api.nvim_create_augroup("_winbar", {})
  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. return M