breadcrumbs.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 = lvim.icons.kind,
  9. highlight = true,
  10. separator = " " .. ">" .. " ",
  11. depth_limit = 0,
  12. depth_limit_indicator = "..",
  13. },
  14. }
  15. end
  16. M.setup = function()
  17. local status_ok, navic = pcall(require, "nvim-navic")
  18. if not status_ok then
  19. return
  20. end
  21. M.create_winbar()
  22. navic.setup(lvim.builtin.breadcrumbs.options)
  23. if lvim.builtin.breadcrumbs.on_config_done then
  24. lvim.builtin.breadcrumbs.on_config_done()
  25. end
  26. end
  27. M.winbar_filetype_exclude = {
  28. "help",
  29. "startify",
  30. "dashboard",
  31. "packer",
  32. "neo-tree",
  33. "neogitstatus",
  34. "NvimTree",
  35. "Trouble",
  36. "alpha",
  37. "lir",
  38. "Outline",
  39. "spectre_panel",
  40. "toggleterm",
  41. "DressingSelect",
  42. "Jaq",
  43. "harpoon",
  44. "dapui_scopes",
  45. "dapui_breakpoints",
  46. "dapui_stacks",
  47. "dapui_watches",
  48. "dap-repl",
  49. "dap-terminal",
  50. "dapui_console",
  51. "lab",
  52. "Markdown",
  53. "",
  54. }
  55. M.get_filename = function()
  56. local filename = vim.fn.expand "%:t"
  57. local extension = vim.fn.expand "%:e"
  58. local f = require "lvim.utils.functions"
  59. if not f.isempty(filename) then
  60. local file_icon, file_icon_color =
  61. require("nvim-web-devicons").get_icon_color(filename, extension, { default = true })
  62. local hl_group = "FileIconColor" .. extension
  63. vim.api.nvim_set_hl(0, hl_group, { fg = file_icon_color })
  64. if f.isempty(file_icon) then
  65. file_icon = lvim.icons.kind.File
  66. end
  67. local navic_text = vim.api.nvim_get_hl_by_name("Normal", true)
  68. vim.api.nvim_set_hl(0, "Winbar", { fg = navic_text.foreground })
  69. return " " .. "%#" .. hl_group .. "#" .. file_icon .. "%*" .. " " .. "%#Winbar#" .. filename .. "%*"
  70. end
  71. end
  72. local get_gps = function()
  73. local status_gps_ok, gps = pcall(require, "nvim-navic")
  74. if not status_gps_ok then
  75. return ""
  76. end
  77. local status_ok, gps_location = pcall(gps.get_location, {})
  78. if not status_ok then
  79. return ""
  80. end
  81. if not gps.is_available() or gps_location == "error" then
  82. return ""
  83. end
  84. if not require("lvim.utils.functions").isempty(gps_location) then
  85. -- TODO: replace with chevron
  86. return ">" .. " " .. gps_location
  87. else
  88. return ""
  89. end
  90. end
  91. local excludes = function()
  92. if vim.tbl_contains(M.winbar_filetype_exclude, vim.bo.filetype) then
  93. return true
  94. end
  95. return false
  96. end
  97. M.get_winbar = function()
  98. if excludes() then
  99. return
  100. end
  101. local f = require "lvim.utils.functions"
  102. local value = M.get_filename()
  103. local gps_added = false
  104. if not f.isempty(value) then
  105. local gps_value = get_gps()
  106. value = value .. " " .. gps_value
  107. if not f.isempty(gps_value) then
  108. gps_added = true
  109. end
  110. end
  111. if not f.isempty(value) and f.get_buf_option "mod" then
  112. -- TODO: replace with circle
  113. local mod = "%#LspCodeLens#" .. lvim.icons.ui.Circle .. "%*"
  114. if gps_added then
  115. value = value .. " " .. mod
  116. else
  117. value = value .. mod
  118. end
  119. end
  120. local num_tabs = #vim.api.nvim_list_tabpages()
  121. if num_tabs > 1 and not f.isempty(value) then
  122. local tabpage_number = tostring(vim.api.nvim_tabpage_get_number(0))
  123. value = value .. "%=" .. tabpage_number .. "/" .. tostring(num_tabs)
  124. end
  125. local status_ok, _ = pcall(vim.api.nvim_set_option_value, "winbar", value, { scope = "local" })
  126. if not status_ok then
  127. return
  128. end
  129. end
  130. M.create_winbar = function()
  131. vim.api.nvim_create_augroup("_winbar", {})
  132. if vim.fn.has "nvim-0.8" == 1 then
  133. vim.api.nvim_create_autocmd(
  134. { "CursorMoved", "CursorHold", "BufWinEnter", "BufFilePost", "InsertEnter", "BufWritePost", "TabClosed" },
  135. {
  136. group = "_winbar",
  137. callback = function()
  138. local status_ok, _ = pcall(vim.api.nvim_buf_get_var, 0, "lsp_floating_window")
  139. if not status_ok then
  140. -- TODO:
  141. require("lvim.core.breadcrumbs").get_winbar()
  142. end
  143. end,
  144. }
  145. )
  146. end
  147. end
  148. return M