nvimtree.lua 2.8 KB

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