breadcrumbs.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. local status_ok, navic = pcall(require, "nvim-navic")
  51. if not status_ok then
  52. return
  53. end
  54. navic.setup(lvim.builtin.breadcrumbs.options)
  55. M.create_winbar()
  56. end
  57. M.setup = function()
  58. local status_ok, navic = pcall(require, "nvim-navic")
  59. if not status_ok then
  60. return
  61. end
  62. navic.setup(lvim.builtin.breadcrumbs.options)
  63. if lvim.builtin.breadcrumbs.on_config_done then
  64. lvim.builtin.breadcrumbs.on_config_done()
  65. end
  66. end
  67. M.winbar_filetype_exclude = {
  68. "help",
  69. "startify",
  70. "dashboard",
  71. "packer",
  72. "neogitstatus",
  73. "NvimTree",
  74. "Trouble",
  75. "alpha",
  76. "lir",
  77. "Outline",
  78. "spectre_panel",
  79. "toggleterm",
  80. "DressingSelect",
  81. "Jaq",
  82. "harpoon",
  83. "dapui_scopes",
  84. "dapui_breakpoints",
  85. "dapui_stacks",
  86. "dapui_watches",
  87. "dap-repl",
  88. "dap-terminal",
  89. "dapui_console",
  90. "lab",
  91. "Markdown",
  92. "",
  93. }
  94. M.get_filename = function()
  95. local filename = vim.fn.expand "%:t"
  96. local extension = vim.fn.expand "%:e"
  97. local f = require "lvim.utils.functions"
  98. if not f.isempty(filename) then
  99. local file_icon, file_icon_color =
  100. require("nvim-web-devicons").get_icon_color(filename, extension, { default = true })
  101. local hl_group = "FileIconColor" .. extension
  102. vim.api.nvim_set_hl(0, hl_group, { fg = file_icon_color })
  103. if f.isempty(file_icon) then
  104. file_icon = ""
  105. end
  106. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  107. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  108. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  109. end
  110. end
  111. local get_gps = function()
  112. local status_gps_ok, gps = pcall(require, "nvim-navic")
  113. if not status_gps_ok then
  114. return ""
  115. end
  116. local status_ok, gps_location = pcall(gps.get_location, {})
  117. if not status_ok then
  118. return ""
  119. end
  120. if not gps.is_available() or gps_location == "error" then
  121. return ""
  122. end
  123. if not require("lvim.utils.functions").isempty(gps_location) then
  124. -- TODO: replace with chevron
  125. return ">" .. " " .. gps_location
  126. else
  127. return ""
  128. end
  129. end
  130. local excludes = function()
  131. if vim.tbl_contains(M.winbar_filetype_exclude, vim.bo.filetype) then
  132. vim.opt_local.winbar = nil
  133. return true
  134. end
  135. return false
  136. end
  137. M.get_winbar = function()
  138. if excludes() then
  139. return
  140. end
  141. local f = require "lvim.utils.functions"
  142. local value = M.get_filename()
  143. local gps_added = false
  144. if not f.isempty(value) then
  145. local gps_value = get_gps()
  146. value = value .. " " .. gps_value
  147. if not f.isempty(gps_value) then
  148. gps_added = true
  149. end
  150. end
  151. if not f.isempty(value) and f.get_buf_option "mod" then
  152. -- TODO: replace with circle
  153. local mod = "%#LspCodeLens#" .. "" .. "%*"
  154. if gps_added then
  155. value = value .. " " .. mod
  156. else
  157. value = value .. mod
  158. end
  159. end
  160. local num_tabs = #vim.api.nvim_list_tabpages()
  161. if num_tabs > 1 and not f.isempty(value) then
  162. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  163. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  164. end
  165. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  166. if not status_ok then
  167. return
  168. end
  169. end
  170. M.create_winbar = function()
  171. vim.api.nvim_create_augroup("_winbar", {})
  172. if vim.fn.has "nvim-0.8" == 1 then
  173. vim.api.nvim_create_autocmd(
  174. { "CursorMoved", "CursorHold", "BufWinEnter", "BufFilePost", "InsertEnter", "BufWritePost", "TabClosed" },
  175. {
  176. group = "_winbar",
  177. callback = function()
  178. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  179. if not status_ok then
  180. -- TODO:
  181. require("lvim.core.breadcrumbs").get_winbar()
  182. end
  183. end,
  184. }
  185. )
  186. end
  187. end
  188. return M