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