Skip to content

refactor: move reload function into utils module #2247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions lua/nvim-tree/actions/tree-modifiers/toggles.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local lib = require "nvim-tree.lib"
local core = require "nvim-tree.core"
local utils = require "nvim-tree.utils"
local filters = require "nvim-tree.explorer.filters"
local reloaders = require "nvim-tree.actions.reloaders.reloaders"
Expand All @@ -9,24 +8,7 @@ local M = {}
local function reload()
local node = lib.get_node_at_cursor()
reloaders.reload_explorer()
local explorer = core.get_explorer()

if explorer == nil then
return
end

while node do
local found_node, _ = utils.find_node(explorer.nodes, function(node_)
return node_.absolute_path == node.absolute_path
end)

if found_node or node.parent == nil then
utils.focus_file(node.absolute_path)
break
end

node = node.parent
end
utils.focus_node_or_parent(node)
end

function M.custom()
Expand Down
21 changes: 21 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,27 @@ function M.focus_file(path)
require("nvim-tree.view").set_cursor { i + 1, 1 }
end

function M.focus_node_or_parent(node)
local explorer = require("nvim-tree.core").get_explorer()

if explorer == nil then
return
end

while node do
local found_node, i = M.find_node(explorer.nodes, function(node_)
return node_.absolute_path == node.absolute_path
end)

if found_node or node.parent == nil then
require("nvim-tree.view").set_cursor { i + 1, 1 }
break
end

node = node.parent
end
end

function M.get_win_buf_from_path(path)
for _, w in pairs(vim.api.nvim_tabpage_list_wins(0)) do
local b = vim.api.nvim_win_get_buf(w)
Expand Down