breadcrumbs.lua 5.2 KB

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