breadcrumbs.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, file_icon_color =
  95. require("nvim-web-devicons").get_icon_color(filename, extension, { default = true })
  96. local hl_group = "FileIconColor" .. extension
  97. vim.api.nvim_set_hl(0, hl_group, { fg = file_icon_color })
  98. if f.isempty(file_icon) then
  99. file_icon = lvim.icons.kind.File
  100. end
  101. local buf_ft = vim.bo.filetype
  102. if buf_ft == "dapui_breakpoints" then
  103. file_icon = lvim.icons.ui.Bug
  104. end
  105. if buf_ft == "dapui_stacks" then
  106. file_icon = lvim.icons.ui.Stacks
  107. end
  108. if buf_ft == "dapui_scopes" then
  109. file_icon = lvim.icons.ui.Scopes
  110. end
  111. if buf_ft == "dapui_watches" then
  112. file_icon = lvim.icons.ui.Watches
  113. end
  114. -- if buf_ft == "dapui_console" then
  115. -- file_icon = lvim.icons.ui.DebugConsole
  116. -- end
  117. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  118. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  119. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  120. end
  121. end
  122. local get_gps = function()
  123. local status_gps_ok, gps = pcall(require, "nvim-navic")
  124. if not status_gps_ok then
  125. return ""
  126. end
  127. local status_ok, gps_location = pcall(gps.get_location, {})
  128. if not status_ok then
  129. return ""
  130. end
  131. if not gps.is_available() or gps_location == "error" then
  132. return ""
  133. end
  134. if not require("lvim.utils.functions").isempty(gps_location) then
  135. -- TODO: replace with chevron
  136. return ">" .. " " .. gps_location
  137. else
  138. return ""
  139. end
  140. end
  141. local excludes = function()
  142. return vim.tbl_contains(lvim.builtin.breadcrumbs.winbar_filetype_exclude or {}, vim.bo.filetype)
  143. end
  144. M.get_winbar = function()
  145. if excludes() then
  146. return
  147. end
  148. local f = require "lvim.utils.functions"
  149. local value = M.get_filename()
  150. local gps_added = false
  151. if not f.isempty(value) then
  152. local gps_value = get_gps()
  153. value = value .. " " .. gps_value
  154. if not f.isempty(gps_value) then
  155. gps_added = true
  156. end
  157. end
  158. if not f.isempty(value) and f.get_buf_option "mod" then
  159. -- TODO: replace with circle
  160. local mod = "%#LspCodeLens#" .. lvim.icons.ui.Circle .. "%*"
  161. if gps_added then
  162. value = value .. " " .. mod
  163. else
  164. value = value .. mod
  165. end
  166. end
  167. local num_tabs = #vim.api.nvim_list_tabpages()
  168. if num_tabs > 1 and not f.isempty(value) then
  169. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  170. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  171. end
  172. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  173. if not status_ok then
  174. return
  175. end
  176. end
  177. M.create_winbar = function()
  178. vim.api.nvim_create_augroup("_winbar", {})
  179. if vim.fn.has "nvim-0.8" == 1 then
  180. vim.api.nvim_create_autocmd(
  181. { "CursorMoved", "CursorHold", "BufWinEnter", "BufFilePost", "InsertEnter", "BufWritePost", "TabClosed" },
  182. {
  183. group = "_winbar",
  184. callback = function()
  185. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  186. if not status_ok then
  187. -- TODO:
  188. require("lvim.core.breadcrumbs").get_winbar()
  189. end
  190. end,
  191. }
  192. )
  193. end
  194. end
  195. return M