breadcrumbs.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 = false,
  7. on_config_done = nil,
  8. options = {
  9. icons = {
  10. Array = icons.Array .. " ",
  11. Boolean = icons.Boolean,
  12. Class = icons.Class .. " ",
  13. Color = icons.Color .. " ",
  14. Constant = icons.Constant .. " ",
  15. Constructor = icons.Constructor .. " ",
  16. Enum = icons.Enum .. " ",
  17. EnumMember = icons.EnumMember .. " ",
  18. Event = icons.Event .. " ",
  19. Field = icons.Field .. " ",
  20. File = icons.File .. " ",
  21. Folder = icons.Folder .. " ",
  22. Function = icons.Function .. " ",
  23. Interface = icons.Interface .. " ",
  24. Key = icons.Key .. " ",
  25. Keyword = icons.Keyword .. " ",
  26. Method = icons.Method .. " ",
  27. Module = icons.Module .. " ",
  28. Namespace = icons.Namespace .. " ",
  29. Null = icons.Null .. " ",
  30. Number = icons.Number .. " ",
  31. Object = icons.Object .. " ",
  32. Operator = icons.Operator .. " ",
  33. Package = icons.Package .. " ",
  34. Property = icons.Property .. " ",
  35. Reference = icons.Reference .. " ",
  36. Snippet = icons.Snippet .. " ",
  37. String = icons.String .. " ",
  38. Struct = icons.Struct .. " ",
  39. Text = icons.Text .. " ",
  40. TypeParameter = icons.TypeParameter .. " ",
  41. Unit = icons.Unit .. " ",
  42. Value = icons.Value .. " ",
  43. Variable = icons.Variable .. " ",
  44. },
  45. highlight = true,
  46. separator = " " .. ">" .. " ",
  47. depth_limit = 0,
  48. depth_limit_indicator = "..",
  49. },
  50. }
  51. end
  52. M.setup = function()
  53. local status_ok, navic = pcall(require, "nvim-navic")
  54. if not status_ok then
  55. return
  56. end
  57. M.create_winbar()
  58. navic.setup(lvim.builtin.breadcrumbs.options)
  59. if lvim.builtin.breadcrumbs.on_config_done then
  60. lvim.builtin.breadcrumbs.on_config_done()
  61. end
  62. end
  63. M.winbar_filetype_exclude = {
  64. "help",
  65. "startify",
  66. "dashboard",
  67. "packer",
  68. "neo-tree",
  69. "neogitstatus",
  70. "NvimTree",
  71. "Trouble",
  72. "alpha",
  73. "lir",
  74. "Outline",
  75. "spectre_panel",
  76. "toggleterm",
  77. "DressingSelect",
  78. "Jaq",
  79. "harpoon",
  80. "dap-repl",
  81. "dap-terminal",
  82. "lab",
  83. "Markdown",
  84. "",
  85. }
  86. M.get_filename = function()
  87. local filename = vim.fn.expand "%:t"
  88. local extension = vim.fn.expand "%:e"
  89. local f = require "lvim.utils.functions"
  90. if not f.isempty(filename) then
  91. local file_icon, file_icon_color =
  92. require("nvim-web-devicons").get_icon_color(filename, extension, { default = true })
  93. local hl_group = "FileIconColor" .. extension
  94. vim.api.nvim_set_hl(0, hl_group, { fg = file_icon_color })
  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. if vim.tbl_contains(M.winbar_filetype_exclude, vim.bo.filetype) then
  140. return true
  141. end
  142. return false
  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