move lib.get_node_at_cursor to Explorer
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
local core = require("nvim-tree.core")
|
||||
local lib = require("nvim-tree.lib")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local events = require("nvim-tree.events")
|
||||
local notify = require("nvim-tree.notify")
|
||||
@@ -104,11 +103,15 @@ function M.fn(default_modifier)
|
||||
default_modifier = default_modifier or ":t"
|
||||
|
||||
return function(node, modifier)
|
||||
if type(node) ~= "table" then
|
||||
node = lib.get_node_at_cursor()
|
||||
local explorer = core.get_explorer()
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
|
||||
if node == nil then
|
||||
if type(node) ~= "table" then
|
||||
node = explorer:get_node_at_cursor()
|
||||
end
|
||||
if not node then
|
||||
return
|
||||
end
|
||||
|
||||
@@ -160,10 +163,7 @@ function M.fn(default_modifier)
|
||||
|
||||
M.rename(node, prepend .. new_file_path .. append)
|
||||
if not M.config.filesystem_watchers.enable then
|
||||
local explorer = core.get_explorer()
|
||||
if explorer then
|
||||
explorer:reload_explorer()
|
||||
end
|
||||
explorer:reload_explorer()
|
||||
end
|
||||
|
||||
find_file(utils.path_remove_trailing(new_file_path))
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
local utils = require("nvim-tree.utils")
|
||||
local view = require("nvim-tree.view")
|
||||
local core = require("nvim-tree.core")
|
||||
local lib = require("nvim-tree.lib")
|
||||
local diagnostics = require("nvim-tree.diagnostics")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
@@ -30,15 +29,11 @@ local function status_is_valid(node, what, skip_gitignored)
|
||||
end
|
||||
|
||||
---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 what string type of status
|
||||
---@param skip_gitignored boolean default false
|
||||
local function move(where, what, skip_gitignored)
|
||||
local explorer = core.get_explorer()
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
|
||||
local function move(explorer, where, what, skip_gitignored)
|
||||
local first_node_line = core.get_nodes_starting_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
|
||||
@@ -88,16 +83,17 @@ local function expand_node(node)
|
||||
end
|
||||
|
||||
--- Move to the next node recursively.
|
||||
---@param explorer Explorer
|
||||
---@param what string type of status
|
||||
---@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:
|
||||
-- * is a directory
|
||||
-- * and is not the root node
|
||||
-- * and has a git/diag status
|
||||
-- * and is not opened
|
||||
-- expand it.
|
||||
local node_init = lib.get_node_at_cursor()
|
||||
local node_init = explorer:get_node_at_cursor()
|
||||
if not node_init then
|
||||
return
|
||||
end
|
||||
@@ -110,9 +106,9 @@ local function move_next_recursive(what, skip_gitignored)
|
||||
node_init:expand_or_collapse(false)
|
||||
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
|
||||
return
|
||||
end
|
||||
@@ -128,10 +124,10 @@ local function move_next_recursive(what, skip_gitignored)
|
||||
while is_dir and i < MAX_DEPTH do
|
||||
expand_node(node_cur)
|
||||
|
||||
move("next", what, skip_gitignored)
|
||||
move(explorer, "next", what, skip_gitignored)
|
||||
|
||||
-- Save current node.
|
||||
node_cur = lib.get_node_at_cursor()
|
||||
node_cur = explorer:get_node_at_cursor()
|
||||
-- Update is_dir.
|
||||
if node_cur then
|
||||
is_dir = node_cur.nodes ~= nil
|
||||
@@ -158,24 +154,25 @@ end
|
||||
--- 4.4) Call a non-recursive prev.
|
||||
--- 4.5) Save the current node and start back from 4.1.
|
||||
---
|
||||
---@param explorer Explorer
|
||||
---@param what string type of status
|
||||
---@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
|
||||
|
||||
-- 1)
|
||||
node_init = lib.get_node_at_cursor()
|
||||
node_init = explorer:get_node_at_cursor()
|
||||
if node_init == nil then
|
||||
return
|
||||
end
|
||||
|
||||
-- 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
|
||||
-- 3)
|
||||
move_prev_recursive(what, skip_gitignored)
|
||||
move_prev_recursive(explorer, what, skip_gitignored)
|
||||
else
|
||||
-- i is used to limit iterations.
|
||||
local i = 0
|
||||
@@ -205,10 +202,10 @@ local function move_prev_recursive(what, skip_gitignored)
|
||||
end
|
||||
|
||||
-- 4.4)
|
||||
move("prev", what, skip_gitignored)
|
||||
move(explorer, "prev", what, skip_gitignored)
|
||||
|
||||
-- 4.5)
|
||||
node_cur = lib.get_node_at_cursor()
|
||||
node_cur = explorer:get_node_at_cursor()
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
@@ -223,6 +220,11 @@ end
|
||||
---@return fun()
|
||||
function M.fn(opts)
|
||||
return function()
|
||||
local explorer = core.get_explorer()
|
||||
if not explorer then
|
||||
return
|
||||
end
|
||||
|
||||
local recurse = false
|
||||
local skip_gitignored = false
|
||||
|
||||
@@ -236,14 +238,14 @@ function M.fn(opts)
|
||||
end
|
||||
|
||||
if not recurse then
|
||||
move(opts.where, opts.what, skip_gitignored)
|
||||
move(explorer, opts.where, opts.what, skip_gitignored)
|
||||
return
|
||||
end
|
||||
|
||||
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
|
||||
move_prev_recursive(opts.what, skip_gitignored)
|
||||
move_prev_recursive(explorer, opts.what, skip_gitignored)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
local utils = require("nvim-tree.utils")
|
||||
local core = require("nvim-tree.core")
|
||||
local lib = require("nvim-tree.lib")
|
||||
local Iterator = require("nvim-tree.iterators.node-iterator")
|
||||
|
||||
local DirectoryNode = require("nvim-tree.node.directory")
|
||||
@@ -26,10 +25,13 @@ end
|
||||
|
||||
---@param keep_buffers boolean
|
||||
function M.fn(keep_buffers)
|
||||
local node = lib.get_node_at_cursor()
|
||||
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
|
||||
end
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
local lib = require("nvim-tree.lib")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local core = require("nvim-tree.core")
|
||||
local M = {}
|
||||
|
||||
---@param explorer Explorer
|
||||
local function reload(explorer)
|
||||
local node = lib.get_node_at_cursor()
|
||||
local node = explorer:get_node_at_cursor()
|
||||
explorer:reload_explorer()
|
||||
utils.focus_node_or_parent(node)
|
||||
if node then
|
||||
utils.focus_node_or_parent(node)
|
||||
end
|
||||
end
|
||||
|
||||
local function wrap_explorer(fn)
|
||||
|
||||
@@ -55,26 +55,6 @@ local function wrap(f)
|
||||
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.
|
||||
---Print error when setup not called.
|
||||
---@param explorer_method string explorer method name
|
||||
@@ -88,6 +68,26 @@ local function wrap_explorer(explorer_method)
|
||||
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.
|
||||
---Print error when setup not called.
|
||||
---@param explorer_member string explorer member name
|
||||
@@ -146,7 +146,7 @@ Api.tree.change_root_to_node = wrap_node(function(node)
|
||||
end)
|
||||
|
||||
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)
|
||||
|
||||
---@class ApiTreeFindFileOpts
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
local core = require("nvim-tree.core")
|
||||
local git = require("nvim-tree.git")
|
||||
local log = require("nvim-tree.log")
|
||||
local notify = require("nvim-tree.notify")
|
||||
@@ -386,6 +387,19 @@ function Explorer:get_cursor_position()
|
||||
return vim.api.nvim_win_get_cursor(winnr)
|
||||
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)
|
||||
config = opts
|
||||
|
||||
@@ -196,7 +196,7 @@ local function create_overlay(self)
|
||||
end
|
||||
|
||||
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.explorer.renderer:draw()
|
||||
@@ -210,7 +210,7 @@ function LiveFilter:start_filtering()
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
self.filter = nil
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
local view = require("nvim-tree.view")
|
||||
local core = require("nvim-tree.core")
|
||||
local utils = require("nvim-tree.utils")
|
||||
local events = require("nvim-tree.events")
|
||||
local notify = require("nvim-tree.notify")
|
||||
|
||||
@@ -13,25 +12,6 @@ local M = {
|
||||
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
|
||||
---@return Node[]?
|
||||
function M.get_nodes()
|
||||
|
||||
@@ -88,14 +88,12 @@ function M.set_inspect_opts(opts)
|
||||
end
|
||||
|
||||
--- 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 node Node? node to be inspected
|
||||
---@param node Node node to be inspected
|
||||
---@param fmt string for string.format
|
||||
---@vararg any arguments for string.format
|
||||
function M.node(typ, node, fmt, ...)
|
||||
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)), ...)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -151,7 +151,7 @@ function Marks:bulk_move()
|
||||
return
|
||||
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()
|
||||
|
||||
if node_at_cursor and node_at_cursor:is(DirectoryNode) then
|
||||
@@ -190,7 +190,7 @@ end
|
||||
---@private
|
||||
---@param up boolean
|
||||
function Marks:navigate(up)
|
||||
local node = lib.get_node_at_cursor()
|
||||
local node = self.explorer:get_node_at_cursor()
|
||||
if not node then
|
||||
return
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user