breadcrumbs.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. local M = {}
  2. -- local Log = require "lvim.core.log"
  3. M.config = function()
  4. lvim.builtin.breadcrumbs = {
  5. active = false,
  6. on_config_done = nil,
  7. options = {
  8. icons = {
  9. Text = " ",
  10. Method = " ",
  11. Function = " ",
  12. Constructor = " ",
  13. Field = " ",
  14. Variable = " ",
  15. Class = " ",
  16. Interface = " ",
  17. Module = " ",
  18. Property = " ",
  19. Unit = " ",
  20. Value = " ",
  21. Enum = " ",
  22. Keyword = " ",
  23. Snippet = " ",
  24. Color = " ",
  25. File = " ",
  26. Reference = " ",
  27. Folder = " ",
  28. EnumMember = " ",
  29. Constant = " ",
  30. Struct = " ",
  31. Event = " ",
  32. Operator = " ",
  33. TypeParameter = " ",
  34. Array = " ",
  35. Number = " ",
  36. String = " ",
  37. Boolean = "蘒",
  38. Object = " ",
  39. Package = " ",
  40. Namespace = "",
  41. Key = "",
  42. Null = "ﳠ",
  43. },
  44. highlight = true,
  45. separator = " " .. ">" .. " ",
  46. depth_limit = 0,
  47. depth_limit_indicator = "..",
  48. },
  49. }
  50. end
  51. M.setup = function()
  52. local status_ok, navic = pcall(require, "nvim-navic")
  53. if not status_ok then
  54. return
  55. end
  56. M.create_winbar()
  57. navic.setup(lvim.builtin.breadcrumbs.options)
  58. if lvim.builtin.breadcrumbs.on_config_done then
  59. lvim.builtin.breadcrumbs.on_config_done()
  60. end
  61. end
  62. M.winbar_filetype_exclude = {
  63. "help",
  64. "startify",
  65. "dashboard",
  66. "packer",
  67. "neogitstatus",
  68. "NvimTree",
  69. "Trouble",
  70. "alpha",
  71. "lir",
  72. "Outline",
  73. "spectre_panel",
  74. "toggleterm",
  75. "DressingSelect",
  76. "Jaq",
  77. "harpoon",
  78. "dapui_scopes",
  79. "dapui_breakpoints",
  80. "dapui_stacks",
  81. "dapui_watches",
  82. "dap-repl",
  83. "dap-terminal",
  84. "dapui_console",
  85. "lab",
  86. "Markdown",
  87. "",
  88. }
  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 = ""
  100. end
  101. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  102. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  103. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  104. end
  105. end
  106. local get_gps = function()
  107. local status_gps_ok, gps = pcall(require, "nvim-navic")
  108. if not status_gps_ok then
  109. return ""
  110. end
  111. local status_ok, gps_location = pcall(gps.get_location, {})
  112. if not status_ok then
  113. return ""
  114. end
  115. if not gps.is_available() or gps_location == "error" then
  116. return ""
  117. end
  118. if not require("lvim.utils.functions").isempty(gps_location) then
  119. -- TODO: replace with chevron
  120. return ">" .. " " .. gps_location
  121. else
  122. return ""
  123. end
  124. end
  125. local excludes = function()
  126. if vim.tbl_contains(M.winbar_filetype_exclude, vim.bo.filetype) then
  127. vim.opt_local.winbar = nil
  128. return true
  129. end
  130. return false
  131. end
  132. M.get_winbar = function()
  133. if excludes() then
  134. return
  135. end
  136. local f = require "lvim.utils.functions"
  137. local value = M.get_filename()
  138. local gps_added = false
  139. if not f.isempty(value) then
  140. local gps_value = get_gps()
  141. value = value .. " " .. gps_value
  142. if not f.isempty(gps_value) then
  143. gps_added = true
  144. end
  145. end
  146. if not f.isempty(value) and f.get_buf_option "mod" then
  147. -- TODO: replace with circle
  148. local mod = "%#LspCodeLens#" .. "" .. "%*"
  149. if gps_added then
  150. value = value .. " " .. mod
  151. else
  152. value = value .. mod
  153. end
  154. end
  155. local num_tabs = #vim.api.nvim_list_tabpages()
  156. if num_tabs > 1 and not f.isempty(value) then
  157. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  158. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  159. end
  160. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  161. if not status_ok then
  162. return
  163. end
  164. end
  165. M.create_winbar = function()
  166. vim.api.nvim_create_augroup("_winbar", {})
  167. if vim.fn.has "nvim-0.8" == 1 then
  168. vim.api.nvim_create_autocmd(
  169. { "CursorMoved", "CursorHold", "BufWinEnter", "BufFilePost", "InsertEnter", "BufWritePost", "TabClosed" },
  170. {
  171. group = "_winbar",
  172. callback = function()
  173. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  174. if not status_ok then
  175. -- TODO:
  176. require("lvim.core.breadcrumbs").get_winbar()
  177. end
  178. end,
  179. }
  180. )
  181. end
  182. end
  183. return M