nvimtree.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. local M = {}
  2. local Log = require "core.log"
  3. M.config = function()
  4. lvim.builtin.nvimtree = {
  5. active = true,
  6. side = "left",
  7. width = 30,
  8. show_icons = {
  9. git = 1,
  10. folders = 1,
  11. files = 1,
  12. folder_arrows = 1,
  13. tree_width = 30,
  14. },
  15. ignore = { ".git", "node_modules", ".cache" },
  16. auto_open = 1,
  17. auto_close = 1,
  18. quit_on_open = 0,
  19. follow = 1,
  20. hide_dotfiles = 1,
  21. git_hl = 1,
  22. root_folder_modifier = ":t",
  23. tab_open = 0,
  24. allow_resize = 1,
  25. lsp_diagnostics = 1,
  26. auto_ignore_ft = { "startify", "dashboard" },
  27. icons = {
  28. default = "",
  29. symlink = "",
  30. git = {
  31. unstaged = "",
  32. staged = "S",
  33. unmerged = "",
  34. renamed = "➜",
  35. deleted = "",
  36. untracked = "U",
  37. ignored = "◌",
  38. },
  39. folder = {
  40. default = "",
  41. open = "",
  42. empty = "",
  43. empty_open = "",
  44. symlink = "",
  45. },
  46. },
  47. }
  48. end
  49. M.setup = function()
  50. local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
  51. if not status_ok then
  52. Log:get_default().error "Failed to load nvim-tree.config"
  53. return
  54. end
  55. local g = vim.g
  56. for opt, val in pairs(lvim.builtin.nvimtree) do
  57. g["nvim_tree_" .. opt] = val
  58. end
  59. -- Implicitly update nvim-tree when project module is active
  60. if lvim.builtin.project.active then
  61. vim.g.nvim_tree_update_cwd = 1
  62. vim.g.nvim_tree_respect_buf_cwd = 1
  63. end
  64. local tree_cb = nvim_tree_config.nvim_tree_callback
  65. if not g.nvim_tree_bindings then
  66. g.nvim_tree_bindings = {
  67. { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
  68. { key = "h", cb = tree_cb "close_node" },
  69. { key = "v", cb = tree_cb "vsplit" },
  70. }
  71. end
  72. lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
  73. local tree_view = require "nvim-tree.view"
  74. -- Add nvim_tree open callback
  75. local open = tree_view.open
  76. tree_view.open = function()
  77. M.on_open()
  78. open()
  79. end
  80. vim.cmd "au WinClosed * lua require('core.nvimtree').on_close()"
  81. end
  82. M.on_open = function()
  83. if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
  84. require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
  85. end
  86. end
  87. M.on_close = function()
  88. local buf = tonumber(vim.fn.expand "<abuf>")
  89. local ft = vim.api.nvim_buf_get_option(buf, "filetype")
  90. if ft == "NvimTree" and package.loaded["bufferline.state"] then
  91. require("bufferline.state").set_offset(0)
  92. end
  93. end
  94. function M.change_tree_dir(dir)
  95. local lib_status_ok, lib = pcall(require, "nvim-tree.lib")
  96. if lib_status_ok then
  97. lib.change_dir(dir)
  98. end
  99. end
  100. return M