move lib.get_node_at_cursor to Explorer
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
local lib = require("nvim-tree.lib")
|
|
||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
local appearance = require("nvim-tree.appearance")
|
local appearance = require("nvim-tree.appearance")
|
||||||
local view = require("nvim-tree.view")
|
local view = require("nvim-tree.view")
|
||||||
@@ -121,7 +120,12 @@ function M.place_cursor_on_node()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local node = lib.get_node_at_cursor()
|
local explorer = core.get_explorer()
|
||||||
|
if not explorer then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local node = explorer:get_node_at_cursor()
|
||||||
if not node or node.name == ".." then
|
if not node or node.name == ".." then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local events = require("nvim-tree.events")
|
local events = require("nvim-tree.events")
|
||||||
local notify = require("nvim-tree.notify")
|
local notify = require("nvim-tree.notify")
|
||||||
@@ -104,11 +103,15 @@ function M.fn(default_modifier)
|
|||||||
default_modifier = default_modifier or ":t"
|
default_modifier = default_modifier or ":t"
|
||||||
|
|
||||||
return function(node, modifier)
|
return function(node, modifier)
|
||||||
if type(node) ~= "table" then
|
local explorer = core.get_explorer()
|
||||||
node = lib.get_node_at_cursor()
|
if not explorer then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if node == nil then
|
if type(node) ~= "table" then
|
||||||
|
node = explorer:get_node_at_cursor()
|
||||||
|
end
|
||||||
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -160,10 +163,7 @@ function M.fn(default_modifier)
|
|||||||
|
|
||||||
M.rename(node, prepend .. new_file_path .. append)
|
M.rename(node, prepend .. new_file_path .. append)
|
||||||
if not M.config.filesystem_watchers.enable then
|
if not M.config.filesystem_watchers.enable then
|
||||||
local explorer = core.get_explorer()
|
explorer:reload_explorer()
|
||||||
if explorer then
|
|
||||||
explorer:reload_explorer()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
find_file(utils.path_remove_trailing(new_file_path))
|
find_file(utils.path_remove_trailing(new_file_path))
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local view = require("nvim-tree.view")
|
local view = require("nvim-tree.view")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
|
||||||
local diagnostics = require("nvim-tree.diagnostics")
|
local diagnostics = require("nvim-tree.diagnostics")
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
@@ -30,15 +29,11 @@ local function status_is_valid(node, what, skip_gitignored)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Move to the next node that has a valid status. If none found, don't move.
|
---Move to the next node that has a valid status. If none found, don't move.
|
||||||
|
---@param explorer Explorer
|
||||||
---@param where string where to move (forwards or backwards)
|
---@param where string where to move (forwards or backwards)
|
||||||
---@param what string type of status
|
---@param what string type of status
|
||||||
---@param skip_gitignored boolean default false
|
---@param skip_gitignored boolean default false
|
||||||
local function move(where, what, skip_gitignored)
|
local function move(explorer, where, what, skip_gitignored)
|
||||||
local explorer = core.get_explorer()
|
|
||||||
if not explorer then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local first_node_line = core.get_nodes_starting_line()
|
local first_node_line = core.get_nodes_starting_line()
|
||||||
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
|
local nodes_by_line = utils.get_nodes_by_line(explorer.nodes, first_node_line)
|
||||||
local iter_start, iter_end, iter_step, cur, first, nex
|
local iter_start, iter_end, iter_step, cur, first, nex
|
||||||
@@ -88,16 +83,17 @@ local function expand_node(node)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Move to the next node recursively.
|
--- Move to the next node recursively.
|
||||||
|
---@param explorer Explorer
|
||||||
---@param what string type of status
|
---@param what string type of status
|
||||||
---@param skip_gitignored boolean default false
|
---@param skip_gitignored boolean default false
|
||||||
local function move_next_recursive(what, skip_gitignored)
|
local function move_next_recursive(explorer, what, skip_gitignored)
|
||||||
-- If the current node:
|
-- If the current node:
|
||||||
-- * is a directory
|
-- * is a directory
|
||||||
-- * and is not the root node
|
-- * and is not the root node
|
||||||
-- * and has a git/diag status
|
-- * and has a git/diag status
|
||||||
-- * and is not opened
|
-- * and is not opened
|
||||||
-- expand it.
|
-- expand it.
|
||||||
local node_init = lib.get_node_at_cursor()
|
local node_init = explorer:get_node_at_cursor()
|
||||||
if not node_init then
|
if not node_init then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -110,9 +106,9 @@ local function move_next_recursive(what, skip_gitignored)
|
|||||||
node_init:expand_or_collapse(false)
|
node_init:expand_or_collapse(false)
|
||||||
end
|
end
|
||||||
|
|
||||||
move("next", what, skip_gitignored)
|
move(explorer, "next", what, skip_gitignored)
|
||||||
|
|
||||||
local node_cur = lib.get_node_at_cursor()
|
local node_cur = explorer:get_node_at_cursor()
|
||||||
if not node_cur then
|
if not node_cur then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -128,10 +124,10 @@ local function move_next_recursive(what, skip_gitignored)
|
|||||||
while is_dir and i < MAX_DEPTH do
|
while is_dir and i < MAX_DEPTH do
|
||||||
expand_node(node_cur)
|
expand_node(node_cur)
|
||||||
|
|
||||||
move("next", what, skip_gitignored)
|
move(explorer, "next", what, skip_gitignored)
|
||||||
|
|
||||||
-- Save current node.
|
-- Save current node.
|
||||||
node_cur = lib.get_node_at_cursor()
|
node_cur = explorer:get_node_at_cursor()
|
||||||
-- Update is_dir.
|
-- Update is_dir.
|
||||||
if node_cur then
|
if node_cur then
|
||||||
is_dir = node_cur.nodes ~= nil
|
is_dir = node_cur.nodes ~= nil
|
||||||
@@ -158,24 +154,25 @@ end
|
|||||||
--- 4.4) Call a non-recursive prev.
|
--- 4.4) Call a non-recursive prev.
|
||||||
--- 4.5) Save the current node and start back from 4.1.
|
--- 4.5) Save the current node and start back from 4.1.
|
||||||
---
|
---
|
||||||
|
---@param explorer Explorer
|
||||||
---@param what string type of status
|
---@param what string type of status
|
||||||
---@param skip_gitignored boolean default false
|
---@param skip_gitignored boolean default false
|
||||||
local function move_prev_recursive(what, skip_gitignored)
|
local function move_prev_recursive(explorer, what, skip_gitignored)
|
||||||
local node_init, node_cur
|
local node_init, node_cur
|
||||||
|
|
||||||
-- 1)
|
-- 1)
|
||||||
node_init = lib.get_node_at_cursor()
|
node_init = explorer:get_node_at_cursor()
|
||||||
if node_init == nil then
|
if node_init == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 2)
|
-- 2)
|
||||||
move("prev", what, skip_gitignored)
|
move(explorer, "prev", what, skip_gitignored)
|
||||||
|
|
||||||
node_cur = lib.get_node_at_cursor()
|
node_cur = explorer:get_node_at_cursor()
|
||||||
if node_cur == node_init.parent then
|
if node_cur == node_init.parent then
|
||||||
-- 3)
|
-- 3)
|
||||||
move_prev_recursive(what, skip_gitignored)
|
move_prev_recursive(explorer, what, skip_gitignored)
|
||||||
else
|
else
|
||||||
-- i is used to limit iterations.
|
-- i is used to limit iterations.
|
||||||
local i = 0
|
local i = 0
|
||||||
@@ -205,10 +202,10 @@ local function move_prev_recursive(what, skip_gitignored)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- 4.4)
|
-- 4.4)
|
||||||
move("prev", what, skip_gitignored)
|
move(explorer, "prev", what, skip_gitignored)
|
||||||
|
|
||||||
-- 4.5)
|
-- 4.5)
|
||||||
node_cur = lib.get_node_at_cursor()
|
node_cur = explorer:get_node_at_cursor()
|
||||||
|
|
||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
@@ -223,6 +220,11 @@ end
|
|||||||
---@return fun()
|
---@return fun()
|
||||||
function M.fn(opts)
|
function M.fn(opts)
|
||||||
return function()
|
return function()
|
||||||
|
local explorer = core.get_explorer()
|
||||||
|
if not explorer then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local recurse = false
|
local recurse = false
|
||||||
local skip_gitignored = false
|
local skip_gitignored = false
|
||||||
|
|
||||||
@@ -236,14 +238,14 @@ function M.fn(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not recurse then
|
if not recurse then
|
||||||
move(opts.where, opts.what, skip_gitignored)
|
move(explorer, opts.where, opts.what, skip_gitignored)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if opts.where == "next" then
|
if opts.where == "next" then
|
||||||
move_next_recursive(opts.what, skip_gitignored)
|
move_next_recursive(explorer, opts.what, skip_gitignored)
|
||||||
elseif opts.where == "prev" then
|
elseif opts.where == "prev" then
|
||||||
move_prev_recursive(opts.what, skip_gitignored)
|
move_prev_recursive(explorer, opts.what, skip_gitignored)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local lib = require("nvim-tree.lib")
|
|
||||||
local Iterator = require("nvim-tree.iterators.node-iterator")
|
local Iterator = require("nvim-tree.iterators.node-iterator")
|
||||||
|
|
||||||
local DirectoryNode = require("nvim-tree.node.directory")
|
local DirectoryNode = require("nvim-tree.node.directory")
|
||||||
@@ -26,10 +25,13 @@ end
|
|||||||
|
|
||||||
---@param keep_buffers boolean
|
---@param keep_buffers boolean
|
||||||
function M.fn(keep_buffers)
|
function M.fn(keep_buffers)
|
||||||
local node = lib.get_node_at_cursor()
|
|
||||||
local explorer = core.get_explorer()
|
local explorer = core.get_explorer()
|
||||||
|
if not explorer then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if explorer == nil then
|
local node = explorer:get_node_at_cursor()
|
||||||
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
local lib = require("nvim-tree.lib")
|
|
||||||
local utils = require("nvim-tree.utils")
|
local utils = require("nvim-tree.utils")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@param explorer Explorer
|
---@param explorer Explorer
|
||||||
local function reload(explorer)
|
local function reload(explorer)
|
||||||
local node = lib.get_node_at_cursor()
|
local node = explorer:get_node_at_cursor()
|
||||||
explorer:reload_explorer()
|
explorer:reload_explorer()
|
||||||
utils.focus_node_or_parent(node)
|
if node then
|
||||||
|
utils.focus_node_or_parent(node)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function wrap_explorer(fn)
|
local function wrap_explorer(fn)
|
||||||
|
|||||||
@@ -55,26 +55,6 @@ local function wrap(f)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Inject the node as the first argument if present otherwise do nothing.
|
|
||||||
---@param fn function function to invoke
|
|
||||||
local function wrap_node(fn)
|
|
||||||
return function(node, ...)
|
|
||||||
node = node or lib.get_node_at_cursor()
|
|
||||||
if node then
|
|
||||||
return fn(node, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---Inject the node or nil as the first argument if absent.
|
|
||||||
---@param fn function function to invoke
|
|
||||||
local function wrap_node_or_nil(fn)
|
|
||||||
return function(node, ...)
|
|
||||||
node = node or lib.get_node_at_cursor()
|
|
||||||
return fn(node, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---Invoke a method on the singleton explorer.
|
---Invoke a method on the singleton explorer.
|
||||||
---Print error when setup not called.
|
---Print error when setup not called.
|
||||||
---@param explorer_method string explorer method name
|
---@param explorer_method string explorer method name
|
||||||
@@ -88,6 +68,26 @@ local function wrap_explorer(explorer_method)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Inject the node as the first argument if present otherwise do nothing.
|
||||||
|
---@param fn function function to invoke
|
||||||
|
local function wrap_node(fn)
|
||||||
|
return function(node, ...)
|
||||||
|
node = node or wrap_explorer("get_node_at_cursor")()
|
||||||
|
if node then
|
||||||
|
return fn(node, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Inject the node or nil as the first argument if absent.
|
||||||
|
---@param fn function function to invoke
|
||||||
|
local function wrap_node_or_nil(fn)
|
||||||
|
return function(node, ...)
|
||||||
|
node = node or wrap_explorer("get_node_at_cursor")()
|
||||||
|
return fn(node, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---Invoke a member's method on the singleton explorer.
|
---Invoke a member's method on the singleton explorer.
|
||||||
---Print error when setup not called.
|
---Print error when setup not called.
|
||||||
---@param explorer_member string explorer member name
|
---@param explorer_member string explorer member name
|
||||||
@@ -146,7 +146,7 @@ Api.tree.change_root_to_node = wrap_node(function(node)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn)
|
Api.tree.change_root_to_parent = wrap_node(actions.root.dir_up.fn)
|
||||||
Api.tree.get_node_under_cursor = wrap(lib.get_node_at_cursor)
|
Api.tree.get_node_under_cursor = wrap_explorer("get_node_at_cursor")
|
||||||
Api.tree.get_nodes = wrap(lib.get_nodes)
|
Api.tree.get_nodes = wrap(lib.get_nodes)
|
||||||
|
|
||||||
---@class ApiTreeFindFileOpts
|
---@class ApiTreeFindFileOpts
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
local core = require("nvim-tree.core")
|
||||||
local git = require("nvim-tree.git")
|
local git = require("nvim-tree.git")
|
||||||
local log = require("nvim-tree.log")
|
local log = require("nvim-tree.log")
|
||||||
local notify = require("nvim-tree.notify")
|
local notify = require("nvim-tree.notify")
|
||||||
@@ -386,6 +387,19 @@ function Explorer:get_cursor_position()
|
|||||||
return vim.api.nvim_win_get_cursor(winnr)
|
return vim.api.nvim_win_get_cursor(winnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@return Node|nil
|
||||||
|
function Explorer:get_node_at_cursor()
|
||||||
|
local cursor = self:get_cursor_position()
|
||||||
|
if not cursor then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return utils.get_nodes_by_line(self.nodes, core.get_nodes_starting_line())[cursor[1]]
|
||||||
|
end
|
||||||
|
|
||||||
function Explorer:setup(opts)
|
function Explorer:setup(opts)
|
||||||
config = opts
|
config = opts
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ local function create_overlay(self)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function LiveFilter:start_filtering()
|
function LiveFilter:start_filtering()
|
||||||
view.View.live_filter.prev_focused_node = require("nvim-tree.lib").get_node_at_cursor()
|
view.View.live_filter.prev_focused_node = self.explorer:get_node_at_cursor()
|
||||||
self.filter = self.filter or ""
|
self.filter = self.filter or ""
|
||||||
|
|
||||||
self.explorer.renderer:draw()
|
self.explorer.renderer:draw()
|
||||||
@@ -210,7 +210,7 @@ function LiveFilter:start_filtering()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function LiveFilter:clear_filter()
|
function LiveFilter:clear_filter()
|
||||||
local node = require("nvim-tree.lib").get_node_at_cursor()
|
local node = self.explorer:get_node_at_cursor()
|
||||||
local last_node = view.View.live_filter.prev_focused_node
|
local last_node = view.View.live_filter.prev_focused_node
|
||||||
|
|
||||||
self.filter = nil
|
self.filter = nil
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
local view = require("nvim-tree.view")
|
local view = require("nvim-tree.view")
|
||||||
local core = require("nvim-tree.core")
|
local core = require("nvim-tree.core")
|
||||||
local utils = require("nvim-tree.utils")
|
|
||||||
local events = require("nvim-tree.events")
|
local events = require("nvim-tree.events")
|
||||||
local notify = require("nvim-tree.notify")
|
local notify = require("nvim-tree.notify")
|
||||||
|
|
||||||
@@ -13,25 +12,6 @@ local M = {
|
|||||||
target_winid = nil,
|
target_winid = nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
---@return Node|nil
|
|
||||||
function M.get_node_at_cursor()
|
|
||||||
local explorer = core.get_explorer()
|
|
||||||
if not explorer then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cursor = explorer:get_cursor_position()
|
|
||||||
if not cursor then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if cursor[1] == 1 and view.is_root_folder_visible(core.get_cwd()) then
|
|
||||||
return explorer
|
|
||||||
end
|
|
||||||
|
|
||||||
return utils.get_nodes_by_line(explorer.nodes, core.get_nodes_starting_line())[cursor[1]]
|
|
||||||
end
|
|
||||||
|
|
||||||
---Api.tree.get_nodes
|
---Api.tree.get_nodes
|
||||||
---@return Node[]?
|
---@return Node[]?
|
||||||
function M.get_nodes()
|
function M.get_nodes()
|
||||||
|
|||||||
@@ -88,14 +88,12 @@ function M.set_inspect_opts(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Write to log file the inspection of a node
|
--- Write to log file the inspection of a node
|
||||||
--- defaults to the node under cursor if none is provided
|
|
||||||
---@param typ string as per log.types config
|
---@param typ string as per log.types config
|
||||||
---@param node Node? node to be inspected
|
---@param node Node node to be inspected
|
||||||
---@param fmt string for string.format
|
---@param fmt string for string.format
|
||||||
---@vararg any arguments for string.format
|
---@vararg any arguments for string.format
|
||||||
function M.node(typ, node, fmt, ...)
|
function M.node(typ, node, fmt, ...)
|
||||||
if M.enabled(typ) then
|
if M.enabled(typ) then
|
||||||
node = node or require("nvim-tree.lib").get_node_at_cursor()
|
|
||||||
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)), ...)
|
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)), ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ function Marks:bulk_move()
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local node_at_cursor = lib.get_node_at_cursor()
|
local node_at_cursor = self.explorer:get_node_at_cursor()
|
||||||
local default_path = core.get_cwd()
|
local default_path = core.get_cwd()
|
||||||
|
|
||||||
if node_at_cursor and node_at_cursor:is(DirectoryNode) then
|
if node_at_cursor and node_at_cursor:is(DirectoryNode) then
|
||||||
@@ -190,7 +190,7 @@ end
|
|||||||
---@private
|
---@private
|
||||||
---@param up boolean
|
---@param up boolean
|
||||||
function Marks:navigate(up)
|
function Marks:navigate(up)
|
||||||
local node = lib.get_node_at_cursor()
|
local node = self.explorer:get_node_at_cursor()
|
||||||
if not node then
|
if not node then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user