nvimtree.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. auto_reload_on_write = true,
  17. hijack_unnamed_buffer_when_opening = false,
  18. hijack_directories = {
  19. enable = true,
  20. auto_open = true,
  21. },
  22. update_to_buf_dir = {
  23. enable = true,
  24. auto_open = true,
  25. },
  26. auto_close = false,
  27. open_on_tab = false,
  28. hijack_cursor = false,
  29. update_cwd = false,
  30. diagnostics = {
  31. enable = true,
  32. icons = {
  33. hint = "",
  34. info = "",
  35. warning = "",
  36. error = "",
  37. },
  38. },
  39. update_focused_file = {
  40. enable = true,
  41. update_cwd = true,
  42. ignore_list = {},
  43. },
  44. system_open = {
  45. cmd = nil,
  46. args = {},
  47. },
  48. git = {
  49. enable = true,
  50. ignore = false,
  51. timeout = 200,
  52. },
  53. view = {
  54. width = 30,
  55. height = 30,
  56. hide_root_folder = false,
  57. side = "left",
  58. auto_resize = false,
  59. mappings = {
  60. custom_only = false,
  61. list = {},
  62. },
  63. number = false,
  64. relativenumber = false,
  65. signcolumn = "yes",
  66. },
  67. filters = {
  68. dotfiles = false,
  69. custom = { "node_modules", ".cache" },
  70. },
  71. trash = {
  72. cmd = "trash",
  73. require_confirm = true,
  74. },
  75. actions = {
  76. change_dir = {
  77. global = false,
  78. },
  79. open_file = {
  80. quit_on_open = false,
  81. },
  82. window_picker = {
  83. enable = false,
  84. chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
  85. exclude = {},
  86. },
  87. },
  88. },
  89. show_icons = {
  90. git = 1,
  91. folders = 1,
  92. files = 1,
  93. folder_arrows = 1,
  94. },
  95. git_hl = 1,
  96. root_folder_modifier = ":t",
  97. icons = {
  98. default = "",
  99. symlink = "",
  100. git = {
  101. unstaged = "",
  102. staged = "S",
  103. unmerged = "",
  104. renamed = "➜",
  105. deleted = "",
  106. untracked = "U",
  107. ignored = "◌",
  108. },
  109. folder = {
  110. default = "",
  111. open = "",
  112. empty = "",
  113. empty_open = "",
  114. symlink = "",
  115. },
  116. },
  117. }
  118. lvim.builtin.which_key.mappings["e"] = { "<cmd>NvimTreeToggle<CR>", "Explorer" }
  119. end
  120. function M.setup()
  121. local status_ok, nvim_tree_config = pcall(require, "nvim-tree.config")
  122. if not status_ok then
  123. Log:error "Failed to load nvim-tree.config"
  124. return
  125. end
  126. for opt, val in pairs(lvim.builtin.nvimtree) do
  127. vim.g["nvim_tree_" .. opt] = val
  128. end
  129. -- Implicitly update nvim-tree when project module is active
  130. if lvim.builtin.project.active then
  131. lvim.builtin.nvimtree.respect_buf_cwd = 1
  132. lvim.builtin.nvimtree.setup.update_cwd = true
  133. lvim.builtin.nvimtree.setup.update_focused_file = { enable = true, update_cwd = true }
  134. end
  135. local function telescope_find_files(_)
  136. require("lvim.core.nvimtree").start_telescope "find_files"
  137. end
  138. local function telescope_live_grep(_)
  139. require("lvim.core.nvimtree").start_telescope "live_grep"
  140. end
  141. -- Add useful keymaps
  142. if #lvim.builtin.nvimtree.setup.view.mappings.list == 0 then
  143. lvim.builtin.nvimtree.setup.view.mappings.list = {
  144. { key = { "l", "<CR>", "o" }, action = "edit", mode = "n" },
  145. { key = "h", action = "close_node" },
  146. { key = "v", action = "vsplit" },
  147. { key = "C", action = "cd" },
  148. { key = "gtf", action = "telescope_find_files", action_cb = telescope_find_files },
  149. { key = "gtg", action = "telescope_live_grep", action_cb = telescope_live_grep },
  150. }
  151. end
  152. if lvim.builtin.nvimtree.on_config_done then
  153. lvim.builtin.nvimtree.on_config_done(nvim_tree_config)
  154. end
  155. require("nvim-tree").setup(lvim.builtin.nvimtree.setup)
  156. end
  157. function M.start_telescope(telescope_mode)
  158. local node = require("nvim-tree.lib").get_node_at_cursor()
  159. local abspath = node.link_to or node.absolute_path
  160. local is_folder = node.open ~= nil
  161. local basedir = is_folder and abspath or vim.fn.fnamemodify(abspath, ":h")
  162. require("telescope.builtin")[telescope_mode] {
  163. cwd = basedir,
  164. }
  165. end
  166. return M