nvimtree.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. function M.config()
  4. lvim.builtin.nvimtree = {
  5. active = true,
  6. on_config_done = nil,
  7. setup = {
  8. disable_netrw = true,
  9. hijack_netrw = true,
  10. open_on_setup = false,
  11. ignore_ft_on_setup = {
  12. "startify",
  13. "dashboard",
  14. "alpha",
  15. },
  16. update_to_buf_dir = {
  17. enable = true,
  18. auto_open = true,
  19. },
  20. auto_close = true,
  21. open_on_tab = false,
  22. hijack_cursor = false,
  23. update_cwd = false,
  24. diagnostics = {
  25. enable = true,
  26. icons = {
  27. hint = "",
  28. info = "",
  29. warning = "",
  30. error = "",
  31. },
  32. },
  33. update_focused_file = {
  34. enable = true,
  35. update_cwd = true,
  36. ignore_list = {},
  37. },
  38. system_open = {
  39. cmd = nil,
  40. args = {},
  41. },
  42. git = {
  43. enable = true,
  44. ignore = true,
  45. timeout = 200,
  46. },
  47. view = {
  48. width = 30,
  49. height = 30,
  50. side = "left",
  51. auto_resize = true,
  52. number = false,
  53. relativenumber = false,
  54. mappings = {
  55. custom_only = false,
  56. list = {},
  57. },
  58. },
  59. filters = {
  60. dotfiles = false,
  61. custom = { ".git", "node_modules", ".cache" },
  62. },
  63. },
  64. show_icons = {
  65. git = 1,
  66. folders = 1,
  67. files = 1,
  68. folder_arrows = 1,
  69. tree_width = 30,
  70. },
  71. quit_on_open = 0,
  72. git_hl = 1,
  73. disable_window_picker = 0,
  74. root_folder_modifier = ":t",
  75. icons = {
  76. default = "",
  77. symlink = "",
  78. git = {
  79. unstaged = "",
  80. staged = "S",
  81. unmerged = "",
  82. renamed = "➜",
  83. deleted = "",
  84. untracked = "U",
  85. ignored = "◌",
  86. },
  87. folder = {
  88. default = "",
  89. open = "",
  90. empty = "",
  91. empty_open = "",
  92. symlink = "",
  93. },
  94. },
  95. }
  96. lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
  97. end
  98. function M.setup()
  99. local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
  100. if not status_ok then
  101. Log:error "Failed to load nvim-tree.config"
  102. return
  103. end
  104. local g = vim.g
  105. for opt, val in pairs(lvim.builtin.nvimtree) do
  106. g["nvim_tree_" .. opt] = val
  107. end
  108. -- Implicitly update nvim-tree when project module is active
  109. if lvim.builtin.project.active then
  110. lvim.builtin.nvimtree.respect_buf_cwd = 1
  111. lvim.builtin.nvimtree.setup.update_cwd = true
  112. lvim.builtin.nvimtree.setup.disable_netrw = false
  113. lvim.builtin.nvimtree.setup.hijack_netrw = false
  114. vim.g.netrw_banner = false
  115. end
  116. local tree_cb = nvim_tree_config.nvim_tree_callback
  117. if not lvim.builtin.nvimtree.setup.view.mappings.list then
  118. lvim.builtin.nvimtree.setup.view.mappings.list = {
  119. { key = { "l", "<CR>", "o" }, cb = tree_cb "edit" },
  120. { key = "h", cb = tree_cb "close_node" },
  121. { key = "v", cb = tree_cb "vsplit" },
  122. }
  123. end
  124. local tree_view = require "nvim-tree.view"
  125. -- Add nvim_tree open callback
  126. local open = tree_view.open
  127. tree_view.open = function()
  128. M.on_open()
  129. open()
  130. end
  131. vim.cmd "au WinClosed * lua require('lvim.core.nvimtree').on_close()"
  132. if lvim.builtin.nvimtree.on_config_done then
  133. lvim.builtin.nvimtree.on_config_done(nvim_tree_config)
  134. end
  135. require("nvim-tree").setup(lvim.builtin.nvimtree.setup)
  136. end
  137. function M.on_open()
  138. if package.loaded["bufferline.state"] and lvim.builtin.nvimtree.setup.view.side == "left" then
  139. require("bufferline.state").set_offset(lvim.builtin.nvimtree.setup.view.width + 1, "")
  140. end
  141. end
  142. function M.on_close()
  143. local buf = tonumber(vim.fn.expand "<abuf>")
  144. local ft = vim.api.nvim_buf_get_option(buf, "filetype")
  145. if ft == "NvimTree" and package.loaded["bufferline.state"] then
  146. require("bufferline.state").set_offset(0)
  147. end
  148. end
  149. function M.change_tree_dir(dir)
  150. local lib_status_ok, lib = pcall(require, "nvim-tree.lib")
  151. if lib_status_ok then
  152. lib.change_dir(dir)
  153. end
  154. end
  155. return M