nvimtree.lua 3.2 KB

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