breadcrumbs.lua 5.6 KB

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