From 1831417f053f52bf5b30673c2c84d438300d8f11 Mon Sep 17 00:00:00 2001 From: kiyan Date: Sat, 26 Mar 2022 14:46:44 +0100 Subject: [PATCH] refacto: add get_nodes_starting_line core util --- lua/nvim-tree/actions/change-dir.lua | 2 +- lua/nvim-tree/actions/find-file.lua | 2 +- lua/nvim-tree/actions/movements.lua | 8 ++++---- lua/nvim-tree/core.lua | 12 ++++++++++-- lua/nvim-tree/lib.lua | 2 +- lua/nvim-tree/renderer/init.lua | 2 +- lua/nvim-tree/view.lua | 6 ++---- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lua/nvim-tree/actions/change-dir.lua b/lua/nvim-tree/actions/change-dir.lua index 62116f36..51bbcfbb 100644 --- a/lua/nvim-tree/actions/change-dir.lua +++ b/lua/nvim-tree/actions/change-dir.lua @@ -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 diff --git a/lua/nvim-tree/actions/find-file.lua b/lua/nvim-tree/actions/find-file.lua index a7d2957a..e07d52af 100644 --- a/lua/nvim-tree/actions/find-file.lua +++ b/lua/nvim-tree/actions/find-file.lua @@ -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) diff --git a/lua/nvim-tree/actions/movements.lua b/lua/nvim-tree/actions/movements.lua index 4bbf1662..66d872c8 100644 --- a/lua/nvim-tree/actions/movements.lua +++ b/lua/nvim-tree/actions/movements.lua @@ -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 diff --git a/lua/nvim-tree/core.lua b/lua/nvim-tree/core.lua index b1306d76..bc057439 100644 --- a/lua/nvim-tree/core.lua +++ b/lua/nvim-tree/core.lua @@ -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 diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index 6b3e8590..842c7a67 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -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 diff --git a/lua/nvim-tree/renderer/init.lua b/lua/nvim-tree/renderer/init.lua index 354eea80..ffe3d6cf 100644 --- a/lua/nvim-tree/renderer/init.lua +++ b/lua/nvim-tree/renderer/init.lua @@ -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)), diff --git a/lua/nvim-tree/view.lua b/lua/nvim-tree/view.lua index f689659d..3cec02f1 100644 --- a/lua/nvim-tree/view.lua +++ b/lua/nvim-tree/view.lua @@ -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)