nvimtree.lua 3.0 KB

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