refacto: add get_nodes_starting_line core util

This commit is contained in:
kiyan
2022-03-26 14:46:44 +01:00
parent 20433ced8a
commit 1831417f05
7 changed files with 20 additions and 14 deletions

View File

@@ -38,7 +38,7 @@ function M.force_dirchange(foldername, with_open)
vim.cmd("lcd " .. vim.fn.fnameescape(foldername))
end
end
require("nvim-tree.core").init(foldername)
core.init(foldername)
if with_open then
require("nvim-tree.lib").open()
else

View File

@@ -24,7 +24,7 @@ function M.fn(fname)
return
end
local i = view.is_root_folder_visible() and 1 or 0
local i = core.get_nodes_starting_line() - 1
local tree_altered = false
local function iterate_nodes(nodes)

View File

@@ -17,7 +17,7 @@ local function get_line_from_node(node, find_parent)
node_path = node.absolute_path:match("(.*)" .. utils.path_separator)
end
local line = 2
local line = core.get_nodes_starting_line()
local function iter(nodes, recursive)
for _, _node in ipairs(nodes) do
local n = lib().get_last_group_node(_node)
@@ -58,7 +58,7 @@ function M.parent_node(should_close)
parent.open = false
altered_tree = true
end
if not view.is_root_folder_visible() then
if not view.is_root_folder_visible(core.get_cwd()) then
line = line - 1
end
view.set_cursor { line, 0 }
@@ -111,7 +111,7 @@ function M.sibling(direction)
local target_node = parent.nodes[index]
line, _ = get_line_from_node(target_node)(core.get_explorer().nodes, true)
if not view.is_root_folder_visible() then
if not view.is_root_folder_visible(core.get_cwd()) then
line = line - 1
end
view.set_cursor { line, 0 }
@@ -121,7 +121,7 @@ end
function M.find_git_item(where)
return function()
local node_cur = lib().get_node_at_cursor()
local nodes_by_line = lib().get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)
local nodes_by_line = lib().get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())
local cur, first, prev, nex = nil, nil, nil, nil
for line, node in pairs(nodes_by_line) do

View File

@@ -1,11 +1,11 @@
local events = require "nvim-tree.events"
local explorer = require "nvim-tree.explorer"
local view = require "nvim-tree.view"
local M = {}
local first_init_done = false
TreeExplorer = nil
local first_init_done = false
function M.init(foldername)
TreeExplorer = explorer.Explorer.new(foldername)
@@ -23,4 +23,12 @@ function M.get_cwd()
return TreeExplorer.cwd
end
function M.get_nodes_starting_line()
local offset = 1
if view.is_root_folder_visible(M.get_cwd()) then
offset = offset + 1
end
return offset
end
return M

View File

@@ -51,7 +51,7 @@ function M.get_node_at_cursor()
if core.get_explorer().cwd == "/" then
line = line + 1
end
return M.get_nodes_by_line(core.get_explorer().nodes, view.View.hide_root_folder and 1 or 2)[line]
return M.get_nodes_by_line(core.get_explorer().nodes, core.get_nodes_starting_line())[line]
end
end

View File

@@ -222,7 +222,7 @@ end
local M = {}
local function compute_header()
if view.is_root_folder_visible() then
if view.is_root_folder_visible(core.get_cwd()) then
local root_folder_modifier = vim.g.nvim_tree_root_folder_modifier or ":~"
local root_name = utils.path_join {
utils.path_remove_trailing(vim.fn.fnamemodify(core.get_cwd(), root_folder_modifier)),

View File

@@ -1,7 +1,5 @@
local a = vim.api
local core = require "nvim-tree.core"
local M = {}
M.View = {
@@ -344,8 +342,8 @@ function M._prevent_buffer_override()
end)
end
function M.is_root_folder_visible()
return core.get_cwd() ~= "/" and not M.View.hide_root_folder
function M.is_root_folder_visible(cwd)
return cwd ~= "/" and not M.View.hide_root_folder
end
function M.setup(opts)