nvimtree.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = 0,
  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. vim.g.nvim_tree_disable_netrw = 0
  65. vim.g.nvim_tree_hijack_netrw = 0
  66. vim.g.netrw_banner = 0
  67. end
  68. local tree_cb = nvim_tree_config.nvim_tree_callback
  69. if not g.nvim_tree_bindings then
  70. g.nvim_tree_bindings = {
  71. { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
  72. { key = "h", cb = tree_cb "close_node" },
  73. { key = "v", cb = tree_cb "vsplit" },
  74. }
  75. end
  76. lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
  77. local tree_view = require "nvim-tree.view"
  78. -- Add nvim_tree open callback
  79. local open = tree_view.open
  80. tree_view.open = function()
  81. M.on_open()
  82. open()
  83. end
  84. vim.cmd "au WinClosed * lua require('core.nvimtree').on_close()"
  85. if lvim.builtin.nvimtree.on_config_done then
  86. lvim.builtin.nvimtree.on_config_done(nvim_tree_config)
  87. end
  88. end
  89. function M.on_open()
  90. if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.side == "left" then
  91. require("bufferline.state").set_offset(lvim.builtin.nvimtree.width + 1, "")
  92. end
  93. end
  94. function M.on_close()
  95. local buf = tonumber(vim.fn.expand "<abuf>")
  96. local ft = vim.api.nvim_buf_get_option(buf, "filetype")
  97. if ft == "NvimTree" and package.loaded["bufferline.state"] then
  98. require("bufferline.state").set_offset(0)
  99. end
  100. end
  101. function M.change_tree_dir(dir)
  102. local lib_status_ok, lib = pcall(require, "nvim-tree.lib")
  103. if lib_status_ok then
  104. lib.change_dir(dir)
  105. end
  106. end
  107. return M