refactor(actions): move actions into semantic modules (#1410)
This commit is contained in:
61
lua/nvim-tree/actions/finders/find-file.lua
Normal file
61
lua/nvim-tree/actions/finders/find-file.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
local log = require "nvim-tree.log"
|
||||
local uv = vim.loop
|
||||
local view = require "nvim-tree.view"
|
||||
local utils = require "nvim-tree.utils"
|
||||
local renderer = require "nvim-tree.renderer"
|
||||
local core = require "nvim-tree.core"
|
||||
local Iterator = require "nvim-tree.iterators.node-iterator"
|
||||
|
||||
local M = {}
|
||||
|
||||
local running = {}
|
||||
|
||||
---Find a path in the tree, expand it and focus it
|
||||
---@param fname string full path
|
||||
function M.fn(fname)
|
||||
if running[fname] or not core.get_explorer() then
|
||||
return
|
||||
end
|
||||
running[fname] = true
|
||||
|
||||
local ps = log.profile_start("find file %s", fname)
|
||||
-- always match against the real path
|
||||
local fname_real = uv.fs_realpath(fname)
|
||||
if not fname_real then
|
||||
return
|
||||
end
|
||||
|
||||
local line = core.get_nodes_starting_line()
|
||||
|
||||
local found = Iterator.builder(core.get_explorer().nodes)
|
||||
:matcher(function(node)
|
||||
return node.absolute_path == fname_real or node.link_to == fname_real
|
||||
end)
|
||||
:applier(function(node)
|
||||
line = line + 1
|
||||
local abs_match = vim.startswith(fname_real, node.absolute_path .. utils.path_separator)
|
||||
local link_match = node.link_to and vim.startswith(fname_real, node.link_to .. utils.path_separator)
|
||||
|
||||
if abs_match or link_match then
|
||||
node.open = true
|
||||
if #node.nodes == 0 then
|
||||
core.get_explorer():expand(node)
|
||||
end
|
||||
end
|
||||
end)
|
||||
:recursor(function(node)
|
||||
return node.open and node.nodes
|
||||
end)
|
||||
:iterate()
|
||||
|
||||
if found and view.is_visible() then
|
||||
renderer.draw()
|
||||
view.set_cursor { line, 0 }
|
||||
end
|
||||
|
||||
running[fname] = false
|
||||
|
||||
log.profile_end(ps, "find file %s", fname)
|
||||
end
|
||||
|
||||
return M
|
||||
80
lua/nvim-tree/actions/finders/search-node.lua
Normal file
80
lua/nvim-tree/actions/finders/search-node.lua
Normal file
@@ -0,0 +1,80 @@
|
||||
local api = vim.api
|
||||
local uv = vim.loop
|
||||
|
||||
local utils = require "nvim-tree.utils"
|
||||
local core = require "nvim-tree.core"
|
||||
local filters = require "nvim-tree.explorer.filters"
|
||||
local find_file = require("nvim-tree.actions.finders.find-file").fn
|
||||
|
||||
local M = {}
|
||||
|
||||
local function search(dir, input_path)
|
||||
local path, name, stat, handle, _
|
||||
|
||||
if not dir then
|
||||
return
|
||||
end
|
||||
|
||||
handle, _ = uv.fs_scandir(dir)
|
||||
if not handle then
|
||||
return
|
||||
end
|
||||
|
||||
name, _ = uv.fs_scandir_next(handle)
|
||||
while name do
|
||||
path = dir .. "/" .. name
|
||||
|
||||
stat, _ = uv.fs_stat(path)
|
||||
if not stat then
|
||||
break
|
||||
end
|
||||
|
||||
if not filters.should_ignore(path) then
|
||||
if string.find(path, "/" .. input_path .. "$") then
|
||||
return path
|
||||
end
|
||||
|
||||
if stat.type == "directory" then
|
||||
path = search(path, input_path)
|
||||
if path then
|
||||
return path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
name, _ = uv.fs_scandir_next(handle)
|
||||
end
|
||||
end
|
||||
|
||||
function M.fn()
|
||||
if not core.get_explorer() then
|
||||
return
|
||||
end
|
||||
|
||||
-- temporarily set &path
|
||||
local bufnr = api.nvim_get_current_buf()
|
||||
local path_existed, path_opt = pcall(api.nvim_buf_get_option, bufnr, "path")
|
||||
api.nvim_buf_set_option(bufnr, "path", core.get_cwd() .. "/**")
|
||||
|
||||
-- completes files/dirs under cwd
|
||||
local input_path = vim.fn.input("Search: ", "", "file_in_path")
|
||||
utils.clear_prompt()
|
||||
|
||||
-- reset &path
|
||||
if path_existed then
|
||||
api.nvim_buf_set_option(bufnr, "path", path_opt)
|
||||
else
|
||||
api.nvim_buf_set_option(bufnr, "path", nil)
|
||||
end
|
||||
|
||||
-- strip trailing slash
|
||||
input_path = string.gsub(input_path, "/$", "")
|
||||
|
||||
-- search under cwd
|
||||
local found = search(core.get_cwd(), input_path)
|
||||
if found then
|
||||
find_file(found)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user