Skip to content

Commit 6662b60

Browse files
authored
feat/chore: rewrite git with job and some other fixes (#743)
* feat/chore: rewrite git with job and some other fixes * fix: fs clear window, rename echo_warning -> warn also fix renaming and add an event blocker to avoid running many events at the same time
1 parent b853e10 commit 6662b60

15 files changed

+453
-354
lines changed

Diff for: README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Install with [packer](https://github.com/wbthomason/packer.nvim):
2323
```lua
2424
use {
2525
'kyazdani42/nvim-tree.lua',
26-
requires = 'kyazdani42/nvim-web-devicons',
26+
requires = {
27+
'kyazdani42/nvim-web-devicons', -- optional, for file icon
28+
},
2729
config = function() require'nvim-tree'.setup {} end
2830
}
2931
```
@@ -72,6 +74,11 @@ require'nvim-tree'.setup {
7274
dotfiles = false,
7375
custom = {}
7476
},
77+
git = {
78+
enable = true,
79+
ignore = true,
80+
timeout = 500,
81+
},
7582
view = {
7683
width = 30,
7784
height = 30,
@@ -89,7 +96,6 @@ require'nvim-tree'.setup {
8996
These additional options must be set **BEFORE** calling `require'nvim-tree'` or calling setup.
9097
They are being migrated to the setup function bit by bit, check [this issue](https://github.com/kyazdani42/nvim-tree.lua/issues/674) if you encounter any problems related to configs not working after update.
9198
```vim
92-
let g:nvim_tree_gitignore = 1 "0 by default
9399
let g:nvim_tree_quit_on_open = 1 "0 by default, closes the tree when you open a file
94100
let g:nvim_tree_indent_markers = 1 "0 by default, this option shows indent markers when folders are open
95101
let g:nvim_tree_git_hl = 1 "0 by default, will enable file highlight for git attributes (can be used without the icons).

Diff for: doc/nvim-tree-lua.txt

+29-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ function.
101101
cmd = nil,
102102
args = {}
103103
},
104+
git = {
105+
enable = true,
106+
ignore = true,
107+
},
104108
view = {
105109
width = 30,
106110
height = 30,
@@ -231,6 +235,31 @@ Here is a list of the options available in the setup call:
231235
- `NvimTreeLspDiagnosticsInformation`
232236
- `NvimTreeLspDiagnosticsHint`
233237

238+
*nvim-tree.git*
239+
- |git|: git integration with icons and colors
240+
241+
- |git.enable|: enable / disable the feature
242+
type: `boolean`
243+
default: `true`
244+
245+
- |git.ignore|: ignore files based on `.gitignore`.
246+
will add `ignored=matching` to the integration when `true`. Otherwise will
247+
add `ignored=no` to the integration which can lead to better performance.
248+
249+
- |git.timeout|: kills the git process after some time if it takes too long
250+
type: `number`
251+
default: `400` (ms)
252+
253+
You will still need to configure `g:nvim_tree_show_icons.git` or
254+
`g:nvim_tree_git_hl` to be able to see things in the tree. This will be
255+
changed in the future versions.
256+
257+
The configurable timeout will kill the current process and so disable the
258+
git integration for the project that takes too long.
259+
The git integration is blocking, so if your timeout is too long (like not in
260+
milliseconds but a few seconds), it will not render anything until the git
261+
process returned the data.
262+
234263
*nvim-tree.view*
235264
- |view|: window / buffer setup
236265

@@ -296,16 +325,6 @@ width of the window, can be *width_in_columns* or *'width_in_percent%'*
296325
where the window will open (default to 'left')
297326
- 'left' or 'right'
298327

299-
|g:nvim_tree_gitignore| *g:nvim_tree_gitignore*
300-
301-
Determines whether to include in g:nvim_tree_ignore
302-
files ignored by git.
303-
304-
Must be:
305-
0: not ignored
306-
1: ignored files from `git ls-files --others --ignored --exclude-standard --directory`
307-
308-
>
309328
|g:nvim_tree_show_icons| *g:nvim_tree_show_icons*
310329

311330
Dictionary, if your terminal or font doesn't support certain unicode

Diff for: lua/nvim-tree.lua

+16-13
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ local keypress_funcs = {
114114
elseif _config.is_unix then
115115
_config.system_open.cmd = 'xdg-open'
116116
else
117-
require'nvim-tree.utils'.echo_warning("Cannot open file with system application. Unrecognized platform.")
117+
require'nvim-tree.utils'.warn("Cannot open file with system application. Unrecognized platform.")
118118
return
119119
end
120120
end
@@ -173,16 +173,12 @@ function M.on_keypress(mode)
173173
if node.link_to and not node.entries then
174174
lib.open_file(mode, node.link_to)
175175
elseif node.entries ~= nil then
176-
lib.unroll_dir(node)
176+
lib.expand_or_collapse(node)
177177
else
178178
lib.open_file(mode, node.absolute_path)
179179
end
180180
end
181181

182-
function M.refresh()
183-
lib.refresh_tree()
184-
end
185-
186182
function M.print_clipboard()
187183
fs.print_clipboard()
188184
end
@@ -227,7 +223,7 @@ function M.on_enter(opts)
227223
M.hijack_current_window()
228224
end
229225

230-
lib.init(should_open, should_open)
226+
lib.init(should_open)
231227
end
232228

233229
local function is_file_readable(fname)
@@ -242,7 +238,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
242238

243239
local ft = api.nvim_buf_get_option(bufnr, 'filetype') or ""
244240
for _, value in pairs(_config.update_focused_file.ignore_list) do
245-
if vim.fn.stridx(filepath, value) ~= -1 or vim.fn.stridx(ft, value) ~= -1 then
241+
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
246242
return
247243
end
248244
end
@@ -359,7 +355,7 @@ local function setup_vim_commands()
359355
command! NvimTreeClose lua require'nvim-tree'.close()
360356
command! NvimTreeToggle lua require'nvim-tree'.toggle(false)
361357
command! NvimTreeFocus lua require'nvim-tree'.focus()
362-
command! NvimTreeRefresh lua require'nvim-tree'.refresh()
358+
command! NvimTreeRefresh lua require'nvim-tree.lib'.refresh_tree()
363359
command! NvimTreeClipboard lua require'nvim-tree'.print_clipboard()
364360
command! NvimTreeFindFile lua require'nvim-tree'.find_file(true)
365361
command! NvimTreeFindFileToggle lua require'nvim-tree'.toggle(true)
@@ -381,8 +377,8 @@ local function setup_autocommands(opts)
381377
""" reset highlights when colorscheme is changed
382378
au ColorScheme * lua require'nvim-tree'.reset_highlight()
383379
384-
au BufWritePost * lua require'nvim-tree'.refresh()
385-
au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh()
380+
au BufWritePost * lua require'nvim-tree.lib'.refresh_tree()
381+
au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.lib'.reload_git()
386382
]]
387383

388384
if opts.auto_close then
@@ -400,6 +396,7 @@ local function setup_autocommands(opts)
400396
if opts.update_focused_file.enable then
401397
vim.cmd "au BufEnter * lua require'nvim-tree'.find_file(false)"
402398
end
399+
vim.cmd "au BufUnload NvimTree lua require'nvim-tree.view'.View.tabpages = {}"
403400

404401
vim.cmd "augroup end"
405402
end
@@ -439,6 +436,11 @@ local DEFAULT_OPTS = {
439436
filters = {
440437
dotfiles = false,
441438
custom_filter = {}
439+
},
440+
git = {
441+
enable = true,
442+
ignore = true,
443+
timeout = 400,
442444
}
443445
}
444446

@@ -452,7 +454,7 @@ function M.setup(conf)
452454
_config.open_on_setup = opts.open_on_setup
453455
_config.ignore_ft_on_setup = opts.ignore_ft_on_setup
454456
if type(opts.update_to_buf_dir) == "boolean" then
455-
utils.echo_warning("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
457+
utils.warn("update_to_buf_dir is now a table, see :help nvim-tree.update_to_buf_dir")
456458
_config.update_to_buf_dir = {
457459
enable = opts.update_to_buf_dir,
458460
auto_open = opts.update_to_buf_dir,
@@ -462,13 +464,14 @@ function M.setup(conf)
462464
end
463465

464466
if opts.lsp_diagnostics ~= nil then
465-
utils.echo_warning("setup.lsp_diagnostics has been removed, see :help nvim-tree.diagnostics")
467+
utils.warn("setup.lsp_diagnostics has been removed, see :help nvim-tree.diagnostics")
466468
end
467469

468470
require'nvim-tree.colors'.setup()
469471
require'nvim-tree.view'.setup(opts.view or {})
470472
require'nvim-tree.diagnostics'.setup(opts)
471473
require'nvim-tree.populate'.setup(opts)
474+
require'nvim-tree.git'.setup(opts)
472475

473476
setup_autocommands(opts)
474477
setup_vim_commands()

Diff for: lua/nvim-tree/config.lua

-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ function M.get_icon_state()
5858
}
5959
end
6060

61-
function M.use_git()
62-
return M.get_icon_state().show_git_icon
63-
or vim.g.nvim_tree_git_hl == 1
64-
or vim.g.nvim_tree_gitignore == 1
65-
end
66-
6761
function M.nvim_tree_callback(callback_name)
6862
return string.format(":lua require'nvim-tree'.on_keypress('%s')<CR>", callback_name)
6963
end

Diff for: lua/nvim-tree/fs.lua

+15-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ local function create_file(file)
3434
else
3535
luv.fs_close(fd)
3636
events._dispatch_file_created(file)
37-
lib.refresh_tree(true)
37+
lib.refresh_tree()
3838
focus_file(file)
3939
end
4040
end))
@@ -98,7 +98,7 @@ function M.create(node)
9898
end
9999
api.nvim_out_write(ans..' was properly created\n')
100100
events._dispatch_folder_created(ans)
101-
lib.refresh_tree(true)
101+
lib.refresh_tree()
102102
focus_file(ans)
103103
end
104104

@@ -113,6 +113,9 @@ local function clear_buffer(absolute_path)
113113
api.nvim_set_current_win(winnr)
114114
end
115115
vim.api.nvim_buf_delete(buf.bufnr, {})
116+
if buf.windows[1] then
117+
vim.api.nvim_win_close(buf.windows[1], true)
118+
end
116119
return
117120
end
118121
end
@@ -239,7 +242,7 @@ local function do_paste(node, action_type, action_fn)
239242
end
240243

241244
clipboard[action_type] = {}
242-
return lib.refresh_tree(true)
245+
return lib.refresh_tree()
243246
end
244247

245248
local function add_to_clipboard(node, clip)
@@ -276,7 +279,7 @@ function M.remove(node)
276279
events._dispatch_file_removed(node.absolute_path)
277280
clear_buffer(node.absolute_path)
278281
end
279-
lib.refresh_tree(true)
282+
lib.refresh_tree()
280283
end
281284
end
282285

@@ -289,7 +292,13 @@ function M.rename(with_sub)
289292
local abs_path = with_sub and node.absolute_path:sub(0, namelen * (-1) -1) or node.absolute_path
290293
local new_name = vim.fn.input("Rename " ..node.name.. " to ", abs_path)
291294
utils.clear_prompt()
292-
if not new_name or #new_name == 0 then return end
295+
if not new_name or #new_name == 0 then
296+
return
297+
end
298+
if luv.fs_access(new_name, 'R') then
299+
utils.warn("Cannot rename: file already exists")
300+
return
301+
end
293302

294303
local success = luv.fs_rename(node.absolute_path, new_name)
295304
if not success then
@@ -298,7 +307,7 @@ function M.rename(with_sub)
298307
api.nvim_out_write(node.absolute_path..''..new_name..'\n')
299308
rename_loaded_buffers(node.absolute_path, new_name)
300309
events._dispatch_node_renamed(abs_path, new_name)
301-
lib.refresh_tree(true)
310+
lib.refresh_tree()
302311
end
303312
end
304313

0 commit comments

Comments
 (0)