Skip to content

Commit dfcc305

Browse files
committed
chore: refacto setup part 1
refacto setup for code entrypoint following options switched boolean values as options to the setup function: - `nvim_tree_disable_netrw` -> `disable_netrw` - `nvim_tree_hijack_netrw` -> `hijack_netrw` - `nvim_tree_auto_open` -> `open_on_setup` - `nvim_tree_auto_close` -> `auto_close` - `nvim_tree_tab_open` -> `tab_open` - `nvim-tree-update-cwd` -> `update_cwd` - `nvim_tree_hijack_cursor` -> `hijack_cursor` - `nvim_tree_system_open_command` -> `system_open.cmd` - `nvim_tree_system_open_command_args` -> `system_open.args` - `nvim_tree_follow` -> `update_focused_file.enable` - `nvim_tree_follow_update_path` -> `update_focused_file.update_cwd` Also added new option `update_focused_file.ignore_list` which will ignore filepath or filetypes that matches one entry of the list when updating the path if update_cwd is true.
1 parent 6780550 commit dfcc305

File tree

3 files changed

+157
-108
lines changed

3 files changed

+157
-108
lines changed

lua/nvim-tree.lua

Lines changed: 155 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
local luv = vim.loop
2+
local api = vim.api
3+
24
local lib = require'nvim-tree.lib'
35
local config = require'nvim-tree.config'
46
local colors = require'nvim-tree.colors'
57
local renderer = require'nvim-tree.renderer'
68
local fs = require'nvim-tree.fs'
7-
local utils = require'nvim-tree.utils'
89
local view = require'nvim-tree.view'
910

10-
local api = vim.api
11+
local _config = {
12+
is_windows = vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1,
13+
is_macos = vim.fn.has('mac') == 1 or vim.fn.has('macunix') == 1,
14+
is_unix = vim.fn.has('unix') == 1,
15+
update_focused_file = {
16+
enable = false,
17+
update_cwd = false,
18+
ignore_list = {},
19+
},
20+
system_open = {},
21+
ignore_ft_on_setup = {},
22+
open_on_setup = {},
23+
}
1124

1225
local M = {}
1326

@@ -22,7 +35,7 @@ function M.toggle()
2235
if view.win_open() then
2336
view.close()
2437
else
25-
if vim.g.nvim_tree_follow == 1 then
38+
if _config.update_focused_file.enable then
2639
M.find_file(true)
2740
end
2841
if not view.win_open() then
@@ -58,7 +71,7 @@ function M.tab_change()
5871
end)
5972
end
6073

61-
local function gen_go_to(mode)
74+
local function go_to(mode)
6275
local icon_state = config.get_icon_state()
6376
local flags = mode == 'prev_git_item' and 'b' or ''
6477
local icons = table.concat(vim.tbl_values(icon_state.icons.git_icons), '\\|')
@@ -88,36 +101,40 @@ local keypress_funcs = {
88101
last_sibling = function(node) lib.sibling(node, math.huge) end,
89102
prev_sibling = function(node) lib.sibling(node, -1) end,
90103
next_sibling = function(node) lib.sibling(node, 1) end,
91-
prev_git_item = gen_go_to('prev_git_item'),
92-
next_git_item = gen_go_to('next_git_item'),
104+
prev_git_item = go_to('prev_git_item'),
105+
next_git_item = go_to('next_git_item'),
93106
dir_up = lib.dir_up,
94107
close = function() M.close() end,
95108
preview = function(node)
96109
if node.entries ~= nil or node.name == '..' then return end
97110
return lib.open_file('preview', node.absolute_path)
98111
end,
99112
system_open = function(node)
100-
if vim.g.nvim_tree_system_open_command == nil then
101-
if vim.fn.has('win32') == 1 or vim.fn.has('win32unix') == 1 then
102-
vim.g.nvim_tree_system_open_command = 'cmd'
103-
vim.g.nvim_tree_system_open_command_args = {'/c', 'start', '""'}
104-
elseif vim.fn.has('mac') == 1 or vim.fn.has('macunix') == 1 then
105-
vim.g.nvim_tree_system_open_command = 'open'
106-
elseif vim.fn.has('unix') == 1 then
107-
vim.g.nvim_tree_system_open_command = 'xdg-open'
113+
if not _config.system_open.cmd then
114+
if _config.is_windows then
115+
_config.system_open = {
116+
cmd = "cmd",
117+
args = {'/c', 'start', '""'}
118+
}
119+
elseif _config.is_macos then
120+
_config.system_open.cmd = 'open'
121+
elseif _config.is_linux then
122+
_config.system_open.cmd = 'xdg-open'
108123
else
109-
error('\nNvimTree system_open: cannot open file with system application. Unrecognized platform.\nPlease fill g:nvim_tree_system_open_command with the name of the system file launcher.')
124+
require'nvim-tree.utils'.echo_warning("Cannot open file with system application. Unrecognized platform.")
110125
return
111126
end
112127
end
113128

114-
local process = {}
115-
process.args = vim.g.nvim_tree_system_open_command_args or {}
129+
local process = {
130+
cmd = _config.system_open.cmd,
131+
args = _config.system_open.args,
132+
errors = '\n',
133+
stderr = luv.new_pipe(false)
134+
}
116135
table.insert(process.args, node.link_to or node.absolute_path)
117-
process.errors = '\n'
118-
process.stderr = luv.new_pipe(false)
119-
process.handle, process.pid = luv.spawn(vim.g.nvim_tree_system_open_command,
120-
{args = process.args, stdio = {nil, nil, process.stderr}},
136+
process.handle, process.pid = luv.spawn(process.cmd,
137+
{ args = process.args, stdio = { nil, nil, process.stderr }},
121138
function(code)
122139
process.stderr:read_stop()
123140
process.stderr:close()
@@ -129,7 +146,7 @@ local keypress_funcs = {
129146
end
130147
)
131148
if not process.handle then
132-
error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. vim.g.nvim_tree_system_open_command .. "'.")
149+
error("\n" .. process.pid .. "\nNvimTree system_open: failed to spawn process using '" .. process.cmd .. "'.")
133150
return
134151
end
135152
luv.read_start(process.stderr,
@@ -190,29 +207,31 @@ function M.hijack_current_window()
190207
end
191208
end
192209

193-
function M.on_enter()
210+
function M.on_enter(opts)
194211
local bufnr = api.nvim_get_current_buf()
195212
local bufname = api.nvim_buf_get_name(bufnr)
196213
local buftype = api.nvim_buf_get_option(bufnr, 'filetype')
197-
local ft_ignore = vim.g.nvim_tree_auto_ignore_ft or {}
214+
local ft_ignore = _config.ignore_ft_on_setup
198215

199216
local stats = luv.fs_stat(bufname)
200217
local is_dir = stats and stats.type == 'directory'
201-
202-
local disable_netrw = vim.g.nvim_tree_disable_netrw or 1
203-
local hijack_netrw = vim.g.nvim_tree_hijack_netrw or 1
204218
if is_dir then
205219
lib.Tree.cwd = vim.fn.expand(bufname)
206220
end
207-
local netrw_disabled = hijack_netrw == 1 or disable_netrw == 1
221+
222+
local netrw_disabled = opts.disable_netrw or opts.hijack_netrw
223+
208224
local lines = not is_dir and api.nvim_buf_get_lines(bufnr, 0, -1, false) or {}
209225
local buf_has_content = #lines > 1 or (#lines == 1 and lines[1] ~= "")
210-
local should_open = vim.g.nvim_tree_auto_open == 1
226+
227+
local should_open = _config.open_on_setup
211228
and ((is_dir and netrw_disabled) or (bufname == "" and not buf_has_content))
212229
and not vim.tbl_contains(ft_ignore, buftype)
230+
213231
if should_open then
214232
M.hijack_current_window()
215233
end
234+
216235
lib.init(should_open, should_open)
217236
end
218237

@@ -221,17 +240,26 @@ local function is_file_readable(fname)
221240
return stat and stat.type == "file" and luv.fs_access(fname, 'R')
222241
end
223242

224-
local function update_base_dir_with_filepath(filepath)
225-
if vim.g.nvim_tree_follow_update_path ~= 1 then
243+
local function update_base_dir_with_filepath(filepath, bufnr)
244+
if not _config.update_focused_file.update_cwd then
226245
return
227246
end
247+
248+
local ft = api.nvim_buf_get_option(bufnr, 'filetype') or ""
249+
for _, value in pairs(_config.update_focused_file.ignore_list) do
250+
if vim.fn.stridx(filepath, value) ~= -1 or vim.fn.stridx(ft, value) ~= -1 then
251+
return
252+
end
253+
end
254+
228255
if not vim.startswith(filepath, lib.Tree.cwd or vim.loop.cwd()) then
229256
lib.change_dir(vim.fn.fnamemodify(filepath, ':p:h'))
230257
end
231258
end
232259

233260
function M.find_file(with_open)
234261
local bufname = vim.fn.bufname()
262+
local bufnr = api.nvim_get_current_buf()
235263
local filepath = vim.fn.fnamemodify(bufname, ':p')
236264

237265
if with_open then
@@ -242,7 +270,7 @@ function M.find_file(with_open)
242270
if not is_file_readable(filepath) then
243271
return
244272
end
245-
update_base_dir_with_filepath(filepath)
273+
update_base_dir_with_filepath(filepath, bufnr)
246274
lib.set_index_and_redraw(filepath)
247275
end
248276

@@ -270,22 +298,6 @@ function M.on_leave()
270298
end, 50)
271299
end
272300

273-
local function update_root_dir()
274-
local bufname = api.nvim_buf_get_name(api.nvim_get_current_buf())
275-
if not is_file_readable(bufname) or not lib.Tree.cwd then return end
276-
277-
-- this logic is a hack
278-
-- depending on vim-rooter or autochdir, it would not behave the same way when those two are not enabled
279-
-- until i implement multiple workspaces/project, it should stay like this
280-
if bufname:match(utils.path_to_matching_str(lib.Tree.cwd)) then
281-
return
282-
end
283-
local new_cwd = luv.cwd()
284-
if lib.Tree.cwd == new_cwd then return end
285-
286-
lib.change_dir(new_cwd)
287-
end
288-
289301
function M.open_on_directory()
290302
local buf = api.nvim_get_current_buf()
291303
local bufname = api.nvim_buf_get_name(buf)
@@ -303,13 +315,6 @@ function M.open_on_directory()
303315
view.replace_window()
304316
end
305317

306-
function M.buf_enter()
307-
update_root_dir()
308-
if vim.g.nvim_tree_follow == 1 then
309-
M.find_file(false)
310-
end
311-
end
312-
313318
function M.reset_highlight()
314319
colors.setup()
315320
renderer.render_hl(view.View.bufnr)
@@ -334,8 +339,100 @@ function M.place_cursor_on_node()
334339
api.nvim_win_set_cursor(0, {cursor[1], idx})
335340
end
336341

337-
view.setup()
338-
colors.setup()
339-
vim.defer_fn(M.on_enter, 1)
342+
local function manage_netrw(disable_netrw, hijack_netrw)
343+
if disable_netrw then
344+
vim.g.loaded_netrw = 1
345+
vim.g.loaded_netrwPlugin = 1
346+
elseif hijack_netrw then
347+
vim.cmd "silent! autocmd! FileExplorer *"
348+
end
349+
end
350+
351+
local function setup_vim_commands()
352+
vim.cmd [[
353+
command! NvimTreeOpen lua require'nvim-tree'.open()
354+
command! NvimTreeClose lua require'nvim-tree'.close()
355+
command! NvimTreeToggle lua require'nvim-tree'.toggle()
356+
command! NvimTreeFocus lua require'nvim-tree'.focus()
357+
command! NvimTreeRefresh lua require'nvim-tree'.refresh()
358+
command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard()
359+
command! NvimTreeFindFile lua require'nvim-tree'.find_file()
360+
command! -nargs=1 NvimTreeResize lua require'nvim-tree'.resize(<args>)
361+
]]
362+
end
363+
364+
local function setup_autocommands(opts)
365+
vim.cmd "augroup NvimTree"
366+
vim.cmd [[
367+
""" reset highlights when colorscheme is changed
368+
au ColorScheme * lua require'nvim-tree'.reset_highlight()
369+
370+
au BufWritePost * lua require'nvim-tree'.refresh()
371+
au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh()
372+
373+
""" deletes the existing buffer when saved in a session to avoid conflicts
374+
au SessionLoadPost * lua require'nvim-tree.view'._wipe_rogue_buffer()
375+
]]
376+
377+
if vim.g.nvim_tree_lsp_diagnostics ~= 1 then
378+
vim.cmd "au User LspDiagnosticsChanged lua require'nvim-tree.diagnostics'.update()"
379+
end
380+
if opts.auto_close then
381+
vim.cmd "au WinClosed * lua require'nvim-tree'.on_leave()"
382+
end
383+
if opts.tab_open then
384+
vim.cmd "au TabEnter * lua require'nvim-tree'.tab_change()"
385+
end
386+
if opts.hijack_cursor then
387+
vim.cmd "au CursorMoved NvimTree lua require'nvim-tree'.place_cursor_on_node()"
388+
end
389+
if opts.update_cwd then
390+
vim.cmd "au DirChanged * lua require'nvim-tree.lib'.change_dir(vim.loop.cwd())"
391+
end
392+
if opts.update_focused_file.enable then
393+
vim.cmd "au BufEnter * lua require'nvim-tree'.find_file(false)"
394+
end
395+
396+
vim.cmd "augroup end"
397+
end
398+
399+
local DEFAULT_OPTS = {
400+
disable_netrw = true,
401+
hijack_netrw = true,
402+
open_on_setup = false,
403+
auto_close = false,
404+
tab_open = false,
405+
hijack_cursor = false,
406+
update_cwd = false,
407+
update_focused_file = {
408+
enable = false,
409+
update_cwd = false,
410+
ignore_list = {}
411+
},
412+
ignore_ft_on_setup = {},
413+
system_open = {
414+
cmd = nil,
415+
args = {}
416+
},
417+
}
418+
419+
function M.setup(conf)
420+
local opts = vim.tbl_deep_extend('force', DEFAULT_OPTS, conf or {})
421+
422+
manage_netrw(opts.disable_netrw, opts.hijack_netrw)
423+
424+
_config.update_focused_file = opts.update_focused_file
425+
_config.system_open = opts.system_open
426+
_config.open_on_setup = opts.open_on_setup
427+
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
428+
429+
require'nvim-tree.colors'.setup()
430+
require'nvim-tree.view'.setup()
431+
432+
setup_autocommands(opts)
433+
setup_vim_commands()
434+
435+
M.on_enter(opts)
436+
end
340437

341438
return M

lua/nvim-tree/config.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ function M.get_icon_state()
5858
end
5959
end
6060

61+
local has_devicons = pcall(require, 'nvim-web-devicons')
6162
return {
62-
show_file_icon = show_icons.files == 1 and vim.g.nvim_web_devicons == 1,
63+
show_file_icon = show_icons.files == 1 and has_devicons,
6364
show_folder_icon = show_icons.folders == 1,
6465
show_git_icon = show_icons.git == 1,
6566
show_folder_arrows = show_icons.folder_arrows == 1,

plugin/tree.vim

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)