refacto: move set_index_and_redraw -> actions.find-file.fn
This commit is contained in:
parent
23c95a674f
commit
7829d7c7cf
@ -143,7 +143,7 @@ function M.find_file(with_open)
|
||||
end
|
||||
|
||||
update_base_dir_with_filepath(filepath, bufnr)
|
||||
lib.set_index_and_redraw(filepath)
|
||||
require"nvim-tree.actions.find-file".fn(filepath)
|
||||
end
|
||||
|
||||
function M.resize(size)
|
||||
@ -192,7 +192,7 @@ function M.open_on_directory()
|
||||
view.focus()
|
||||
view.replace_window()
|
||||
|
||||
lib.set_index_and_redraw(bufname)
|
||||
require"nvim-tree.actions.find-file".fn(bufname)
|
||||
vim.api.nvim_buf_delete(buf, { force = true })
|
||||
end
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@ function M.fn(node)
|
||||
else
|
||||
local newdir = vim.fn.fnamemodify(require'nvim-tree.lib'.Tree.cwd, ':h')
|
||||
require'nvim-tree.actions.change-dir'.fn(newdir)
|
||||
return require'nvim-tree.lib'.set_index_and_redraw(node.absolute_path)
|
||||
return require"nvim-tree.actions.find-file".fn(node.absolute_path)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
65
lua/nvim-tree/actions/find-file.lua
Normal file
65
lua/nvim-tree/actions/find-file.lua
Normal file
@ -0,0 +1,65 @@
|
||||
local view = require'nvim-tree.view'
|
||||
local utils = require'nvim-tree.utils'
|
||||
local explorer_module = require"nvim-tree.explorer"
|
||||
local git = require"nvim-tree.git"
|
||||
|
||||
local M = {}
|
||||
|
||||
local function get_explorer()
|
||||
return require"nvim-tree.lib".Tree
|
||||
end
|
||||
|
||||
function M.fn(fname)
|
||||
local i
|
||||
local hide_root_folder = view.View.hide_root_folder
|
||||
local Explorer = get_explorer()
|
||||
if Explorer.cwd == '/' or hide_root_folder then
|
||||
i = 0
|
||||
else
|
||||
i = 1
|
||||
end
|
||||
|
||||
local tree_altered = false
|
||||
|
||||
local function iterate_nodes(nodes)
|
||||
for _, node in ipairs(nodes) do
|
||||
i = i + 1
|
||||
if node.absolute_path == fname then
|
||||
return i
|
||||
end
|
||||
|
||||
local path_matches = utils.str_find(fname, node.absolute_path..utils.path_separator)
|
||||
if path_matches then
|
||||
if #node.nodes == 0 then
|
||||
node.open = true
|
||||
explorer_module.explore(node.nodes, node.absolute_path, node, {})
|
||||
git.load_project_status(node.absolute_path, function(status)
|
||||
if status.dirs or status.files then
|
||||
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
|
||||
end
|
||||
require"nvim-tree.lib".redraw()
|
||||
end)
|
||||
end
|
||||
if node.open == false then
|
||||
node.open = true
|
||||
tree_altered = true
|
||||
end
|
||||
if iterate_nodes(node.nodes) ~= nil then
|
||||
return i
|
||||
end
|
||||
elseif node.open == true then
|
||||
iterate_nodes(node.nodes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local index = iterate_nodes(Explorer.nodes)
|
||||
if tree_altered then
|
||||
require"nvim-tree.lib".redraw()
|
||||
end
|
||||
if index and view.win_open() then
|
||||
view.set_cursor({index, 0})
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
@ -2,10 +2,13 @@ local git = require "nvim-tree.git"
|
||||
local diagnostics = require "nvim-tree.diagnostics"
|
||||
local view = require "nvim-tree.view"
|
||||
local explorer_module = require'nvim-tree.explorer'
|
||||
local get_explorer = function() return require "nvim-tree.lib".Tree end
|
||||
|
||||
local M = {}
|
||||
|
||||
local function get_explorer()
|
||||
return require "nvim-tree.lib".Tree
|
||||
end
|
||||
|
||||
local function refresh_nodes(node, projects)
|
||||
local project_root = git.get_project_root(node.absolute_path or node.cwd)
|
||||
explorer_module.refresh(node.nodes, node.absolute_path or node.cwd, node, projects[project_root] or {})
|
||||
|
||||
@ -4,7 +4,6 @@ local luv = vim.loop
|
||||
local renderer = require'nvim-tree.renderer'
|
||||
local diagnostics = require'nvim-tree.diagnostics'
|
||||
local explorer = require'nvim-tree.explorer'
|
||||
local utils = require'nvim-tree.utils'
|
||||
local view = require'nvim-tree.view'
|
||||
local events = require'nvim-tree.events'
|
||||
local git = require'nvim-tree.git'
|
||||
@ -112,58 +111,6 @@ function M.expand_or_collapse(node)
|
||||
diagnostics.update()
|
||||
end
|
||||
|
||||
function M.set_index_and_redraw(fname)
|
||||
local i
|
||||
local hide_root_folder = view.View.hide_root_folder
|
||||
if M.Tree.cwd == '/' or hide_root_folder then
|
||||
i = 0
|
||||
else
|
||||
i = 1
|
||||
end
|
||||
|
||||
local tree_altered = false
|
||||
|
||||
local function iterate_nodes(nodes)
|
||||
for _, node in ipairs(nodes) do
|
||||
i = i + 1
|
||||
if node.absolute_path == fname then
|
||||
return i
|
||||
end
|
||||
|
||||
local path_matches = utils.str_find(fname, node.absolute_path..utils.path_separator)
|
||||
if path_matches then
|
||||
if #node.nodes == 0 then
|
||||
node.open = true
|
||||
explorer.explore(node.nodes, node.absolute_path, node, {})
|
||||
git.load_project_status(node.absolute_path, function(status)
|
||||
if status.dirs or status.files then
|
||||
require"nvim-tree.actions.reloaders".reload_node_status(node, git.projects)
|
||||
end
|
||||
M.redraw()
|
||||
end)
|
||||
end
|
||||
if node.open == false then
|
||||
node.open = true
|
||||
tree_altered = true
|
||||
end
|
||||
if iterate_nodes(node.nodes) ~= nil then
|
||||
return i
|
||||
end
|
||||
elseif node.open == true then
|
||||
iterate_nodes(node.nodes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local index = iterate_nodes(M.Tree.nodes)
|
||||
if tree_altered then
|
||||
M.redraw()
|
||||
end
|
||||
if index and view.win_open() then
|
||||
view.set_cursor({index, 0})
|
||||
end
|
||||
end
|
||||
|
||||
function M.set_target_win()
|
||||
local id = api.nvim_get_current_win()
|
||||
local tree_id = view.get_winnr()
|
||||
@ -217,6 +164,9 @@ M.dir_up = require'nvim-tree.actions.dir-up'.fn
|
||||
M.change_dir = require'nvim-tree.actions.change-dir'.fn
|
||||
-- @deprecated: use nvim-tree.actions.reloaders.reload_explorer
|
||||
M.refresh_tree = require'nvim-tree.actions.reloaders'.reload_explorer
|
||||
|
||||
-- @deprecated: use nvim-tree.actions.reloaders.reload_git
|
||||
M.reload_git = require'nvim-tree.actions.reloaders'.reload_git
|
||||
-- @deprecated: use nvim-tree.actions.find-file.fn
|
||||
M.set_index_and_redraw = require'nvim-tree.actions.find-file'.fn
|
||||
|
||||
return M
|
||||
|
||||
Loading…
Reference in New Issue
Block a user