Skip to content

Commit 155682e

Browse files
committed
chore: add more optimizations to git
do not redraw when applying updates on git, only redraw when a new project is indexed.
1 parent b39c654 commit 155682e

File tree

4 files changed

+38
-45
lines changed

4 files changed

+38
-45
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ local function setup_autocommands(opts)
381381
au ColorScheme * lua require'nvim-tree'.reset_highlight()
382382
383383
au BufWritePost * lua require'nvim-tree.lib'.refresh_tree(true)
384-
au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree'.refresh()
384+
au User FugitiveChanged,NeogitStatusRefreshed lua require'nvim-tree.git'.full_reload()
385385
]]
386386

387387
if opts.auto_close then

lua/nvim-tree/git/init.lua

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ local M = {
88
toplevels = {},
99
}
1010

11+
function M.has_git_plugin()
12+
local has_neogit = pcall(require, 'neogit')
13+
return vim.fn.exists('g:loaded_fugitive') == 1 or has_neogit
14+
end
15+
1116
function M.apply_updates(node)
1217
local filter_ignored = M.config.ignore and not require'nvim-tree.populate'.show_ignored
1318
updater.update(M.db, node, filter_ignored)
@@ -50,23 +55,23 @@ end
5055

5156
function M.run_git_status(toplevel, node)
5257
local show_untracked = M.toplevels[toplevel]
53-
local runner = Runner.new {
58+
Runner.new {
5459
db = M.db,
5560
toplevel = toplevel,
5661
show_untracked = show_untracked,
5762
with_ignored = M.config.ignore,
58-
timeout = M.config.timeout
59-
}
60-
61-
runner:run(M.handle_update(node), clear)
63+
timeout = M.config.timeout,
64+
on_end = M.handle_update(node),
65+
after_clear = clear,
66+
}:run()
6267
end
6368

64-
function M.run(node, toplevel)
69+
function M.run(node)
6570
if not M.config.enable then
6671
return
6772
end
6873

69-
toplevel = toplevel or git_utils.get_toplevel(node.absolute_path)
74+
local toplevel = git_utils.get_toplevel(node.absolute_path)
7075
if not toplevel then
7176
return
7277
end
@@ -75,7 +80,7 @@ function M.run(node, toplevel)
7580
M.set_toplevel(nil, toplevel)
7681
M.run_git_status(toplevel, node)
7782
else
78-
M.handle_update(node)()
83+
M.apply_updates(node)
7984
end
8085
end
8186

@@ -88,30 +93,31 @@ local function check_sqlite()
8893
end
8994
end
9095

91-
function M.reload()
96+
function M.full_reload()
9297
if not M.config.enable then
9398
return
9499
end
95100

101+
local tree = require'nvim-tree.lib'.Tree
96102
for toplevel, show_untracked in pairs(M.toplevels) do
97-
local runner = Runner.new {
98-
db = M.db,
99-
toplevel = toplevel,
100-
show_untracked = show_untracked,
101-
with_ignored = M.config.ignore,
102-
timeout = M.config.timeout
103-
}
104-
local tree = require'nvim-tree.lib'.Tree
105103
local node
106104
if utils.str_find(tree.cwd, toplevel) then
107105
node = { entries = tree.entries, absolute_path = tree.cwd }
108106
else
109107
node = utils.find_node(tree.entries, function(n)
110-
return toplevel == n.absolute_path or vim.startswith(n.absolute_path, toplevel)
108+
return utils.str_find(n.absolute_path, toplevel)
111109
end)
112110
end
113111
if node then
114-
runner:run(M.handle_update(node), clear)
112+
Runner.new {
113+
db = M.db,
114+
toplevel = toplevel,
115+
show_untracked = show_untracked,
116+
with_ignored = M.config.ignore,
117+
timeout = M.config.timeout,
118+
on_end = M.handle_update(node),
119+
after_clear = clear,
120+
}:run()
115121
end
116122
end
117123
end

lua/nvim-tree/git/runner.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function Runner:_populate_db()
6464
handle = uv.spawn("git", self:_getopts(stdout), vim.schedule_wrap(function()
6565
if not has_timedout then
6666
self.db:insert_cache()
67-
self.and_then()
67+
self.on_end()
6868
else
6969
self.db:clear()
7070
end
@@ -87,9 +87,7 @@ function Runner:_populate_db()
8787
end))
8888
end
8989

90-
function Runner:run(and_then, after_clear)
91-
self.and_then = and_then
92-
self.after_clear = after_clear
90+
function Runner:run()
9391
self:_populate_db()
9492
end
9593

@@ -101,6 +99,8 @@ function Runner.new(opts)
10199
show_untracked = opts.show_untracked,
102100
with_ignored = opts.with_ignored,
103101
timeout = opts.timeout,
102+
on_end = opts.on_end,
103+
after_clear = opts.after_clear,
104104
}, Runner)
105105
end
106106

lua/nvim-tree/lib.lua

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@ function M.init(with_open)
2828
M.Tree.cwd = luv.cwd()
2929
end
3030
populate(M.Tree.entries, M.Tree.cwd)
31+
git.run({ absolute_path = M.Tree.cwd, entries = M.Tree.entries })
3132

3233
local stat = luv.fs_stat(M.Tree.cwd)
3334
M.Tree.last_modified = stat.mtime.sec
3435

3536
if with_open then
3637
M.open()
37-
elseif view.win_open() then
38-
M.refresh_tree()
3938
end
4039

4140
M.redraw()
42-
git.run({ absolute_path = M.Tree.cwd, entries = M.Tree.entries })
4341

4442
if not first_init_done then
4543
events._dispatch_ready()
@@ -130,16 +128,10 @@ function M.unroll_dir(node)
130128
if node.has_children then node.has_children = false end
131129
if #node.entries == 0 then
132130
populate(node.entries, node.link_to or node.absolute_path, node)
133-
end
134-
135-
local toplevel = git.get_loaded_toplevel(node.absolute_path)
136-
if toplevel then
137-
git.run(node, toplevel)
138-
else
139-
M.redraw()
140131
git.run(node)
141132
end
142133

134+
M.redraw()
143135
diagnostics.update()
144136
end
145137

@@ -169,7 +161,10 @@ function M.refresh_tree(disable_clock)
169161
M.redraw()
170162
end
171163

172-
git.reload()
164+
-- do not refresh git if neogit or fugitive are loaded and root dir is a project
165+
if not git.toplevels[M.Tree.cwd] or not git.has_git_plugin() then
166+
git.full_reload()
167+
end
173168
diagnostics.update()
174169
if not disable_clock then
175170
vim.defer_fn(function() refreshing = false end, vim.g.nvim_tree_refresh_wait or 1000)
@@ -188,26 +183,18 @@ function M.set_index_and_redraw(fname)
188183

189184
local should_redraw = false
190185

191-
local new_git_toplevels = {}
192-
193186
local function iterate_nodes(nodes)
194187
for _, node in ipairs(nodes) do
195188
i = i + 1
196189
if node.absolute_path == fname then
197190
return i
198191
end
199192

200-
local path_matches = utils.str_find(fname, node.absolute_path)
193+
local path_matches = utils.str_find(fname, node.absolute_path..utils.path_separator)
201194
if path_matches then
202195
if #node.entries == 0 then
203196
populate(node.entries, node.absolute_path, node)
204-
local toplevel = require'nvim-tree.git.utils'.get_toplevel(node.absolute_path)
205-
if git.toplevels[toplevel] then
206-
git.apply_updates(node)
207-
elseif toplevel and not vim.tbl_contains(new_git_toplevels, toplevel) then
208-
table.insert(new_git_toplevels, toplevel)
209-
git.run(node, toplevel)
210-
end
197+
git.run(node)
211198
should_redraw = true
212199
end
213200
if node.open == false then

0 commit comments

Comments
 (0)