Skip to content

Commit e389812

Browse files
committed
chore: better git implementation on set_index_and_redraw
also refactor some code, remove match_name match_path properties from nodes because they are basically useless, better to use `vim.fn.stridx`
1 parent 291291c commit e389812

File tree

6 files changed

+53
-42
lines changed

6 files changed

+53
-42
lines changed

lua/nvim-tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ local function update_base_dir_with_filepath(filepath, bufnr)
242242

243243
local ft = api.nvim_buf_get_option(bufnr, 'filetype') or ""
244244
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
245+
if utils.str_find(filepath, value) or utils.str_find(ft, value) then
246246
return
247247
end
248248
end

lua/nvim-tree/git/init.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
local utils = require'nvim-tree.git.utils'
1+
local git_utils = require'nvim-tree.git.utils'
22
local updater = require'nvim-tree.git.tree-update'
33
local Runner = require'nvim-tree.git.runner'
4+
local utils = require'nvim-tree.utils'
45

56
local M = {
67
db = nil,
78
toplevels = {},
89
}
910

11+
function M.apply_updates(node)
12+
local filter_ignored = M.config.ignore and not require'nvim-tree.populate'.show_ignored
13+
updater.update(M.db, node, filter_ignored)
14+
end
15+
1016
function M.handle_update(node)
1117
return function()
12-
local filter_ignored = M.config.ignore and not require'nvim-tree.populate'.show_ignored
13-
updater.update(M.db, node, filter_ignored)
18+
M.apply_updates(node)
1419
require'nvim-tree.lib'.redraw()
1520
end
1621
end
@@ -19,7 +24,7 @@ function M.get_loaded_toplevel(path)
1924
if not M.config.enable then
2025
return
2126
end
22-
local toplevel = utils.get_toplevel(path)
27+
local toplevel = git_utils.get_toplevel(path)
2328
if not toplevel or not M.toplevels[toplevel] then
2429
return
2530
end
@@ -30,17 +35,17 @@ function M.set_toplevel(path, toplevel)
3035
if not M.config.enable then
3136
return
3237
end
33-
toplevel = toplevel or utils.get_toplevel(path)
38+
toplevel = toplevel or git_utils.get_toplevel(path)
3439
if not toplevel or M.toplevels[toplevel] ~= nil then
3540
return
3641
end
3742

38-
M.toplevels[toplevel] = utils.show_untracked(toplevel)
43+
M.toplevels[toplevel] = git_utils.show_untracked(toplevel)
3944
end
4045

4146
local function clear()
4247
M.config.enable = false
43-
require'nvim-tree.utils'.echo_warning("git integration has been disabled, timeout was exceeded")
48+
utils.echo_warning("git integration has been disabled, timeout was exceeded")
4449
end
4550

4651
function M.run_git_status(toplevel, node)
@@ -61,7 +66,7 @@ function M.run(node, toplevel)
6166
return
6267
end
6368

64-
toplevel = toplevel or utils.get_toplevel(node.absolute_path)
69+
toplevel = toplevel or git_utils.get_toplevel(node.absolute_path)
6570
if not toplevel then
6671
return
6772
end
@@ -78,7 +83,7 @@ local function check_sqlite()
7883
local has_sqlite = pcall(require, 'sqlite')
7984
if M.config.enable and not has_sqlite then
8085
local info = "Git integration requires `tami5/sqlite.lua` to be installed (see :help nvim-tree.git)"
81-
require'nvim-tree.utils'.echo_warning(info)
86+
utils.echo_warning(info)
8287
M.config.enable = false
8388
end
8489
end
@@ -98,10 +103,10 @@ function M.reload()
98103
}
99104
local tree = require'nvim-tree.lib'.Tree
100105
local node
101-
if tree.cwd == toplevel then
106+
if utils.str_find(tree.cwd, toplevel) then
102107
node = { entries = tree.entries, absolute_path = tree.cwd }
103108
else
104-
node = require'nvim-tree.utils'.find_node(tree.entries, function(n)
109+
node = utils.find_node(tree.entries, function(n)
105110
return toplevel == n.absolute_path or vim.startswith(n.absolute_path, toplevel)
106111
end)
107112
end

lua/nvim-tree/git/runner.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919
-- @param cwd: {string} root cwd
2020
-- @returns {string} or {nil}
2121
local function handle_incoming_data(db, p_data, data, cwd)
22-
if data and vim.fn.stridx(data, '\n') ~= -1 then
22+
if data and utils.str_find(data, '\n') then
2323
local prev = p_data..data
2424
local i = 0
2525
for line in prev:gmatch('[^\n]*\n') do

lua/nvim-tree/lib.lua

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ local function get_line_from_node(node, find_parent)
7979
local function iter(entries, recursive)
8080
for _, entry in ipairs(entries) do
8181
local n = M.get_last_group_node(entry)
82-
if node_path:match('^'..n.match_path..'$') ~= nil then
82+
if node_path == n.absolute_path then
8383
return line, entry
8484
end
8585

@@ -186,41 +186,50 @@ function M.set_index_and_redraw(fname)
186186
else
187187
i = 1
188188
end
189-
local tree_altered = false
190189

191-
local function iter(entries)
192-
for _, entry in ipairs(entries) do
190+
local should_redraw = false
191+
192+
local new_git_toplevels = {}
193+
194+
local function iterate_nodes(nodes)
195+
for _, node in ipairs(nodes) do
193196
i = i + 1
194-
if entry.absolute_path == fname then
197+
if node.absolute_path == fname then
195198
return i
196199
end
197200

198-
if fname:match(entry.match_path..utils.path_separator) ~= nil then
199-
git.set_toplevel(entry.absolute_path)
200-
if #entry.entries == 0 then
201-
populate(entry.entries, entry.absolute_path, entry)
202-
tree_altered = true
201+
local path_matches = utils.str_find(fname, node.absolute_path)
202+
if path_matches then
203+
if #node.entries == 0 then
204+
populate(node.entries, node.absolute_path, node)
205+
local toplevel = require'nvim-tree.git.utils'.get_toplevel(node.absolute_path)
206+
if git.toplevels[toplevel] then
207+
git.apply_updates(node)
208+
elseif toplevel and not vim.tbl_contains(new_git_toplevels, toplevel) then
209+
table.insert(new_git_toplevels, toplevel)
210+
git.run(node, toplevel)
211+
end
212+
should_redraw = true
203213
end
204-
if entry.open == false then
205-
entry.open = true
206-
tree_altered = true
214+
if node.open == false then
215+
node.open = true
216+
should_redraw = true
207217
end
208-
if iter(entry.entries) ~= nil then
218+
if iterate_nodes(node.entries) ~= nil then
209219
return i
210220
end
211-
elseif entry.open == true then
212-
iter(entry.entries)
221+
elseif node.open == true then
222+
iterate_nodes(node.entries)
213223
end
214224
end
215225
end
216226

217-
local index = iter(M.Tree.entries)
227+
local index = iterate_nodes(M.Tree.entries)
218228
if not view.win_open() then
219229
return
220230
end
221-
if tree_altered then
231+
if should_redraw then
222232
M.redraw()
223-
git.reload()
224233
end
225234
if index then
226235
view.set_cursor({index, 0})
@@ -483,7 +492,7 @@ function M.sibling(node, direction)
483492

484493
-- Check if current node is already at root entries
485494
for index, entry in ipairs(M.Tree.entries) do
486-
if node_path:match('^'..entry.match_path..'$') ~= nil then
495+
if node_path == entry.absolute_path then
487496
line = index
488497
end
489498
end

lua/nvim-tree/populate.lua

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
local api = vim.api
22
local luv = vim.loop
33

4+
local utils = require'nvim-tree.utils'
5+
46
local M = {
57
show_ignored = false,
68
show_dotfiles = vim.g.nvim_tree_hide_dotfiles ~= 1,
79
}
810

9-
local utils = require'nvim-tree.utils'
10-
local path_to_matching_str = utils.path_to_matching_str
11-
1211
local function dir_new(cwd, name)
1312
local absolute_path = utils.path_join({cwd, name})
1413
local stat = luv.fs_stat(absolute_path)
@@ -26,8 +25,6 @@ local function dir_new(cwd, name)
2625
absolute_path = absolute_path,
2726
-- TODO: last modified could also involve atime and ctime
2827
last_modified = last_modified,
29-
match_name = path_to_matching_str(name),
30-
match_path = path_to_matching_str(absolute_path),
3128
open = false,
3229
group_next = nil, -- If node is grouped, this points to the next child dir/link node
3330
has_children = has_children,
@@ -43,8 +40,6 @@ local function file_new(cwd, name)
4340
absolute_path = absolute_path,
4441
executable = is_exec,
4542
extension = string.match(name, ".?[^.]+%.(.*)") or "",
46-
match_name = path_to_matching_str(name),
47-
match_path = path_to_matching_str(absolute_path),
4843
}
4944
end
5045

@@ -78,8 +73,6 @@ local function link_new(cwd, name)
7873
open = open,
7974
group_next = nil, -- If node is grouped, this points to the next child dir/link node
8075
entries = entries,
81-
match_name = path_to_matching_str(name),
82-
match_path = path_to_matching_str(absolute_path),
8376
}
8477
end
8578

lua/nvim-tree/utils.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function M.echo_warning(msg)
1212
api.nvim_command('echohl None')
1313
end
1414

15+
function M.str_find(haystack, needle)
16+
return vim.fn.stridx(haystack, needle) ~= -1
17+
end
18+
1519
function M.read_file(path)
1620
local fd = uv.fs_open(path, "r", 438)
1721
if not fd then return '' end

0 commit comments

Comments
 (0)