breadcrumbs.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "packer",
  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. "lab",
  29. "Markdown",
  30. "notify",
  31. "noice",
  32. "",
  33. },
  34. options = {
  35. icons = {
  36. Array = icons.Array .. " ",
  37. Boolean = icons.Boolean,
  38. Class = icons.Class .. " ",
  39. Color = icons.Color .. " ",
  40. Constant = icons.Constant .. " ",
  41. Constructor = icons.Constructor .. " ",
  42. Enum = icons.Enum .. " ",
  43. EnumMember = icons.EnumMember .. " ",
  44. Event = icons.Event .. " ",
  45. Field = icons.Field .. " ",
  46. File = icons.File .. " ",
  47. Folder = icons.Folder .. " ",
  48. Function = icons.Function .. " ",
  49. Interface = icons.Interface .. " ",
  50. Key = icons.Key .. " ",
  51. Keyword = icons.Keyword .. " ",
  52. Method = icons.Method .. " ",
  53. Module = icons.Module .. " ",
  54. Namespace = icons.Namespace .. " ",
  55. Null = icons.Null .. " ",
  56. Number = icons.Number .. " ",
  57. Object = icons.Object .. " ",
  58. Operator = icons.Operator .. " ",
  59. Package = icons.Package .. " ",
  60. Property = icons.Property .. " ",
  61. Reference = icons.Reference .. " ",
  62. Snippet = icons.Snippet .. " ",
  63. String = icons.String .. " ",
  64. Struct = icons.Struct .. " ",
  65. Text = icons.Text .. " ",
  66. TypeParameter = icons.TypeParameter .. " ",
  67. Unit = icons.Unit .. " ",
  68. Value = icons.Value .. " ",
  69. Variable = icons.Variable .. " ",
  70. },
  71. highlight = true,
  72. separator = " " .. ">" .. " ",
  73. depth_limit = 0,
  74. depth_limit_indicator = "..",
  75. },
  76. }
  77. end
  78. M.setup = function()
  79. local status_ok, navic = pcall(require, "nvim-navic")
  80. if not status_ok then
  81. return
  82. end
  83. M.create_winbar()
  84. navic.setup(lvim.builtin.breadcrumbs.options)
  85. if lvim.builtin.breadcrumbs.on_config_done then
  86. lvim.builtin.breadcrumbs.on_config_done()
  87. end
  88. end
  89. M.get_filename = function()
  90. local filename = vim.fn.expand "%:t"
  91. local extension = vim.fn.expand "%:e"
  92. local f = require "lvim.utils.functions"
  93. if not f.isempty(filename) then
  94. local file_icon, hl_group = require("nvim-web-devicons").get_icon(filename, extension, { default = true })
  95. if f.isempty(file_icon) then
  96. file_icon = lvim.icons.kind.File
  97. end
  98. local buf_ft = vim.bo.filetype
  99. if buf_ft == "dapui_breakpoints" then
  100. file_icon = lvim.icons.ui.Bug
  101. end
  102. if buf_ft == "dapui_stacks" then
  103. file_icon = lvim.icons.ui.Stacks
  104. end
  105. if buf_ft == "dapui_scopes" then
  106. file_icon = lvim.icons.ui.Scopes
  107. end
  108. if buf_ft == "dapui_watches" then
  109. file_icon = lvim.icons.ui.Watches
  110. end
  111. -- if buf_ft == "dapui_console" then
  112. -- file_icon = lvim.icons.ui.DebugConsole
  113. -- end
  114. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  115. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  116. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  117. end
  118. end
  119. local get_gps = function()
  120. local status_gps_ok, gps = pcall(require, "nvim-navic")
  121. if not status_gps_ok then
  122. return ""
  123. end
  124. local status_ok, gps_location = pcall(gps.get_location, {})
  125. if not status_ok then
  126. return ""
  127. end
  128. if not gps.is_available() or gps_location == "error" then
  129. return ""
  130. end
  131. if not require("lvim.utils.functions").isempty(gps_location) then
  132. -- TODO: replace with chevron
  133. return ">" .. " " .. gps_location
  134. else
  135. return ""
  136. end
  137. end
  138. local excludes = function()
  139. return vim.tbl_contains(lvim.builtin.breadcrumbs.winbar_filetype_exclude or {}, vim.bo.filetype)
  140. end
  141. M.get_winbar = function()
  142. if excludes() then
  143. return
  144. end
  145. local f = require "lvim.utils.functions"
  146. local value = M.get_filename()
  147. local gps_added = false
  148. if not f.isempty(value) then
  149. local gps_value = get_gps()
  150. value = value .. " " .. gps_value
  151. if not f.isempty(gps_value) then
  152. gps_added = true
  153. end
  154. end
  155. if not f.isempty(value) and f.get_buf_option "mod" then
  156. -- TODO: replace with circle
  157. local mod = "%#LspCodeLens#" .. lvim.icons.ui.Circle .. "%*"
  158. if gps_added then
  159. value = value .. " " .. mod
  160. else
  161. value = value .. mod
  162. end
  163. end
  164. local num_tabs = #vim.api.nvim_list_tabpages()
  165. if num_tabs > 1 and not f.isempty(value) then
  166. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  167. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  168. end
  169. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  170. if not status_ok then
  171. return
  172. end
  173. end
  174. M.create_winbar = function()
  175. vim.api.nvim_create_augroup("_winbar", {})
  176. if vim.fn.has "nvim-0.8" == 1 then
  177. vim.api.nvim_create_autocmd(
  178. { "CursorMoved", "CursorHold", "BufWinEnter", "BufFilePost", "InsertEnter", "BufWritePost", "TabClosed" },
  179. {
  180. group = "_winbar",
  181. callback = function()
  182. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  183. if not status_ok then
  184. -- TODO:
  185. require("lvim.core.breadcrumbs").get_winbar()
  186. end
  187. end,
  188. }
  189. )
  190. end
  191. end
  192. return M