Skip to content

Commit 7324fb1

Browse files
committed
move lib.get_node_at_cursor to Explorer
1 parent 0992969 commit 7324fb1

File tree

11 files changed

+83
-82
lines changed

11 files changed

+83
-82
lines changed

lua/nvim-tree.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local lib = require("nvim-tree.lib")
21
local log = require("nvim-tree.log")
32
local appearance = require("nvim-tree.appearance")
43
local view = require("nvim-tree.view")
@@ -121,7 +120,12 @@ function M.place_cursor_on_node()
121120
return
122121
end
123122

124-
local node = lib.get_node_at_cursor()
123+
local explorer = core.get_explorer()
124+
if not explorer then
125+
return
126+
end
127+
128+
local node = explorer:get_node_at_cursor()
125129
if not node or node.name == ".." then
126130
return
127131
end

lua/nvim-tree/actions/fs/rename-file.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local core = require("nvim-tree.core")
2-
local lib = require("nvim-tree.lib")
32
local utils = require("nvim-tree.utils")
43
local events = require("nvim-tree.events")
54
local notify = require("nvim-tree.notify")
@@ -104,11 +103,15 @@ function M.fn(default_modifier)
104103
default_modifier = default_modifier or ":t"
105104

106105
return function(node, modifier)
107-
if type(node) ~= "table" then
108-
node = lib.get_node_at_cursor()
106+
local explorer = core.get_explorer()
107+
if not explorer then
108+
return
109109
end
110110

111-
if node == nil then
111+
if type(node) ~= "table" then
112+
node = explorer:get_node_at_cursor()
113+
end
114+
if not node then
112115
return
113116
end
114117

@@ -160,10 +163,7 @@ function M.fn(default_modifier)
160163

161164
M.rename(node, prepend .. new_file_path .. append)
162165
if not M.config.filesystem_watchers.enable then
163-
local explorer = core.get_explorer()
164-
if explorer then
165-
explorer:reload_explorer()
166-
end
166+
explorer:reload_explorer()
167167
end
168168

169169
find_file(utils.path_remove_trailing(new_file_path))

lua/nvim-tree/actions/moves/item.lua

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local utils = require("nvim-tree.utils")
22
local view = require("nvim-tree.view")
33
local core = require("nvim-tree.core")
4-
local lib = require("nvim-tree.lib")
54
local diagnostics = require("nvim-tree.diagnostics")
65

76
local DirectoryNode = require("nvim-tree.node.directory")
@@ -30,15 +29,11 @@ local function status_is_valid(node, what, skip_gitignored)
3029
end
3130

3231
---Move to the next node that has a valid status. If none found, don't move.
32+
---@param explorer Explorer
3333
---@param where string where to move (forwards or backwards)
3434
---@param what string type of status
3535
---@param skip_gitignored boolean default false
36-
local function move(where, what, skip_gitignored)
37-
local explorer = core.get_explorer()
38-
if not explorer then
39-
return
40-
end
41-
36+
local function move(explorer, where, what, skip_gitignored)
4237
local first_node_line = core.get_nodes_starting_line()
4338
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
4439
local iter_start, iter_end, iter_step, cur, first, nex
@@ -88,16 +83,17 @@ local function expand_node(node)
8883
end
8984

9085
--- Move to the next node recursively.
86+
---@param explorer Explorer
9187
---@param what string type of status
9288
---@param skip_gitignored boolean default false
93-
local function move_next_recursive(what, skip_gitignored)
89+
local function move_next_recursive(explorer, what, skip_gitignored)
9490
-- If the current node:
9591
-- * is a directory
9692
-- * and is not the root node
9793
-- * and has a git/diag status
9894
-- * and is not opened
9995
-- expand it.
100-
local node_init = lib.get_node_at_cursor()
96+
local node_init = explorer:get_node_at_cursor()
10197
if not node_init then
10298
return
10399
end
@@ -110,9 +106,9 @@ local function move_next_recursive(what, skip_gitignored)
110106
node_init:expand_or_collapse(false)
111107
end
112108

113-
move("next", what, skip_gitignored)
109+
move(explorer, "next", what, skip_gitignored)
114110

115-
local node_cur = lib.get_node_at_cursor()
111+
local node_cur = explorer:get_node_at_cursor()
116112
if not node_cur then
117113
return
118114
end
@@ -128,10 +124,10 @@ local function move_next_recursive(what, skip_gitignored)
128124
while is_dir and i < MAX_DEPTH do
129125
expand_node(node_cur)
130126

131-
move("next", what, skip_gitignored)
127+
move(explorer, "next", what, skip_gitignored)
132128

133129
-- Save current node.
134-
node_cur = lib.get_node_at_cursor()
130+
node_cur = explorer:get_node_at_cursor()
135131
-- Update is_dir.
136132
if node_cur then
137133
is_dir = node_cur.nodes ~= nil
@@ -158,24 +154,25 @@ end
158154
--- 4.4) Call a non-recursive prev.
159155
--- 4.5) Save the current node and start back from 4.1.
160156
---
157+
---@param explorer Explorer
161158
---@param what string type of status
162159
---@param skip_gitignored boolean default false
163-
local function move_prev_recursive(what, skip_gitignored)
160+
local function move_prev_recursive(explorer, what, skip_gitignored)
164161
local node_init, node_cur
165162

166163
-- 1)
167-
node_init = lib.get_node_at_cursor()
164+
node_init = explorer:get_node_at_cursor()
168165
if node_init == nil then
169166
return
170167
end
171168

172169
-- 2)
173-
move("prev", what, skip_gitignored)
170+
move(explorer, "prev", what, skip_gitignored)
174171

175-
node_cur = lib.get_node_at_cursor()
172+
node_cur = explorer:get_node_at_cursor()
176173
if node_cur == node_init.parent then
177174
-- 3)
178-
move_prev_recursive(what, skip_gitignored)
175+
move_prev_recursive(explorer, what, skip_gitignored)
179176
else
180177
-- i is used to limit iterations.
181178
local i = 0
@@ -205,10 +202,10 @@ local function move_prev_recursive(what, skip_gitignored)
205202
end
206203

207204
-- 4.4)
208-
move("prev", what, skip_gitignored)
205+
move(explorer, "prev", what, skip_gitignored)
209206

210207
-- 4.5)
211-
node_cur = lib.get_node_at_cursor()
208+
node_cur = explorer:get_node_at_cursor()
212209

213210
i = i + 1
214211
end
@@ -223,6 +220,11 @@ end
223220
---@return fun()
224221
function M.fn(opts)
225222
return function()
223+
local explorer = core.get_explorer()
224+
if not explorer then
225+
return
226+
end
227+
226228
local recurse = false
227229
local skip_gitignored = false
228230

@@ -236,14 +238,14 @@ function M.fn(opts)
236238
end
237239

238240
if not recurse then
239-
move(opts.where, opts.what, skip_gitignored)
241+
move(explorer, opts.where, opts.what, skip_gitignored)
240242
return
241243
end
242244

243245
if opts.where == "next" then
244-
move_next_recursive(opts.what, skip_gitignored)
246+
move_next_recursive(explorer, opts.what, skip_gitignored)
245247
elseif opts.where == "prev" then
246-
move_prev_recursive(opts.what, skip_gitignored)
248+
move_prev_recursive(explorer, opts.what, skip_gitignored)
247249
end
248250
end
249251
end

lua/nvim-tree/actions/tree/modifiers/collapse-all.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require("nvim-tree.utils")
22
local core = require("nvim-tree.core")
3-
local lib = require("nvim-tree.lib")
43
local Iterator = require("nvim-tree.iterators.node-iterator")
54

65
local DirectoryNode = require("nvim-tree.node.directory")
@@ -26,10 +25,13 @@ end
2625

2726
---@param keep_buffers boolean
2827
function M.fn(keep_buffers)
29-
local node = lib.get_node_at_cursor()
3028
local explorer = core.get_explorer()
29+
if not explorer then
30+
return
31+
end
3132

32-
if explorer == nil then
33+
local node = explorer:get_node_at_cursor()
34+
if not node then
3335
return
3436
end
3537

lua/nvim-tree/actions/tree/modifiers/toggles.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
local lib = require("nvim-tree.lib")
21
local utils = require("nvim-tree.utils")
32
local core = require("nvim-tree.core")
43
local M = {}
54

65
---@param explorer Explorer
76
local function reload(explorer)
8-
local node = lib.get_node_at_cursor()
7+
local node = explorer:get_node_at_cursor()
98
explorer:reload_explorer()
10-
utils.focus_node_or_parent(node)
9+
if node then
10+
utils.focus_node_or_parent(node)
11+
end
1112
end
1213

1314
local function wrap_explorer(fn)

lua/nvim-tree/api.lua

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,24 @@ local function wrap(f)
5555
end
5656
end
5757

58+
---Invoke a method on the singleton explorer.
59+
---Print error when setup not called.
60+
---@param explorer_method string explorer method name
61+
---@return fun(...) : any
62+
local function wrap_explorer(explorer_method)
63+
return wrap(function(...)
64+
local explorer = core.get_explorer()
65+
if explorer then
66+
return explorer[explorer_method](explorer, ...)
67+
end
68+
end)
69+
end
70+
5871
---Inject the node as the first argument if present otherwise do nothing.
5972
---@param fn function function to invoke
6073
local function wrap_node(fn)
6174
return function(node, ...)
62-
node = node or lib.get_node_at_cursor()
75+
node = node or wrap_explorer("get_node_at_cursor")()
6376
if node then
6477
return fn(node, ...)
6578
end
@@ -70,24 +83,11 @@ end
7083
---@param fn function function to invoke
7184
local function wrap_node_or_nil(fn)
7285
return function(node, ...)
73-
node = node or lib.get_node_at_cursor()
86+
node = node or wrap_explorer("get_node_at_cursor")()
7487
return fn(node, ...)
7588
end
7689
end
7790

78-
---Invoke a method on the singleton explorer.
79-
---Print error when setup not called.
80-
---@param explorer_method string explorer method name
81-
---@return fun(...) : any
82-
local function wrap_explorer(explorer_method)
83-
return wrap(function(...)
84-
local explorer = core.get_explorer()
85-
if explorer then
86-
return explorer[explorer_method](explorer, ...)
87-
end
88-
end)
89-
end
90-
9191
---Invoke a member's method on the singleton explorer.
9292
---Print error when setup not called.
9393
---@param explorer_member string explorer member name
@@ -146,7 +146,7 @@ Api.tree.change_root_to_node = wrap_node(function(node)
146146
end)
147147

148148
Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn)
149-
Api.tree.get_node_under_cursor = wrap(lib.get_node_at_cursor)
149+
Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
150150
Api.tree.get_nodes = wrap(lib.get_nodes)
151151

152152
---@class ApiTreeFindFileOpts

lua/nvim-tree/explorer/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local core = require("nvim-tree.core")
12
local git = require("nvim-tree.git")
23
local log = require("nvim-tree.log")
34
local notify = require("nvim-tree.notify")
@@ -386,6 +387,19 @@ function Explorer:get_cursor_position()
386387
return vim.api.nvim_win_get_cursor(winnr)
387388
end
388389

390+
---@return Node|nil
391+
function Explorer:get_node_at_cursor()
392+
local cursor = self:get_cursor_position()
393+
if not cursor then
394+
return
395+
end
396+
397+
if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then
398+
return self
399+
end
400+
401+
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]]
402+
end
389403

390404
function Explorer:setup(opts)
391405
config = opts

lua/nvim-tree/explorer/live-filter.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ local function create_overlay(self)
196196
end
197197

198198
function LiveFilter:start_filtering()
199-
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
199+
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
200200
self.filter = self.filter or ""
201201

202202
self.explorer.renderer:draw()
@@ -210,7 +210,7 @@ function LiveFilter:start_filtering()
210210
end
211211

212212
function LiveFilter:clear_filter()
213-
local node = require("nvim-tree.lib").get_node_at_cursor()
213+
local node = self.explorer:get_node_at_cursor()
214214
local last_node = view.View.live_filter.prev_focused_node
215215

216216
self.filter = nil

lua/nvim-tree/lib.lua

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local view = require("nvim-tree.view")
22
local core = require("nvim-tree.core")
3-
local utils = require("nvim-tree.utils")
43
local events = require("nvim-tree.events")
54
local notify = require("nvim-tree.notify")
65

@@ -13,25 +12,6 @@ local M = {
1312
target_winid = nil,
1413
}
1514

16-
---@return Node|nil
17-
function M.get_node_at_cursor()
18-
local explorer = core.get_explorer()
19-
if not explorer then
20-
return
21-
end
22-
23-
local cursor = explorer:get_cursor_position()
24-
if not cursor then
25-
return
26-
end
27-
28-
if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then
29-
return explorer
30-
end
31-
32-
return utils.get_nodes_by_line(explorer.nodes, core.get_nodes_starting_line())[cursor[1]]
33-
end
34-
3515
---Api.tree.get_nodes
3616
---@return Node[]?
3717
function M.get_nodes()

lua/nvim-tree/log.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ function M.set_inspect_opts(opts)
8888
end
8989

9090
--- Write to log file the inspection of a node
91-
--- defaults to the node under cursor if none is provided
9291
---@param typ string as per log.types config
93-
---@param node Node? node to be inspected
92+
---@param node Node node to be inspected
9493
---@param fmt string for string.format
9594
---@vararg any arguments for string.format
9695
function M.node(typ, node, fmt, ...)
9796
if M.enabled(typ) then
98-
node = node or require("nvim-tree.lib").get_node_at_cursor()
9997
M.raw(typ, string.format("[%s] [%s] %s\n%s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???"), vim.inspect(node, inspect_opts)), ...)
10098
end
10199
end

0 commit comments

Comments
 (0)