nvimtree.lua 4.5 KB

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