-
-
Notifications
You must be signed in to change notification settings - Fork 633
Expand file tree
/
Copy pathsearch-node.lua
More file actions
102 lines (81 loc) · 2.34 KB
/
search-node.lua
File metadata and controls
102 lines (81 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local core = require("nvim-tree.core")
local find_file = require("nvim-tree.actions.finders.find-file").fn
local M = {}
---@param search_dir string|nil
---@param input_path string
---@return string|nil
local function search(search_dir, input_path)
local realpaths_searched = {}
local explorer = core.get_explorer()
if not explorer then
return
end
if not search_dir then
return
end
---@param dir string
---@return string|nil
local function iter(dir)
local realpath, path, name, stat, handle, _
local filter_status = explorer.filters:prepare()
handle, _ = vim.uv.fs_scandir(dir)
if not handle then
return
end
realpath, _ = vim.uv.fs_realpath(dir)
if not realpath or vim.tbl_contains(realpaths_searched, realpath) then
return
end
table.insert(realpaths_searched, realpath)
name, _ = vim.uv.fs_scandir_next(handle)
while name do
path = dir .. "/" .. name
---@type uv.fs_stat.result|nil
stat, _ = vim.uv.fs_stat(path)
if not stat then
break
end
if not explorer.filters:should_filter(path, stat, filter_status) then
if string.find(path, "/" .. input_path .. "$") then
return path
end
if stat.type == "directory" then
path = iter(path)
if path then
return path
end
end
end
name, _ = vim.uv.fs_scandir_next(handle)
end
end
return iter(search_dir)
end
function M.fn()
if not core.get_explorer() then
return
end
-- temporarily set &path
local bufnr = vim.api.nvim_get_current_buf()
local path_existed, path_opt = pcall(vim.api.nvim_get_option_value, "path", { buf = bufnr })
vim.api.nvim_set_option_value("path", core.get_cwd() .. "/**", { buf = bufnr })
vim.ui.input({ prompt = "Search: ", completion = "file_in_path" }, function(input_path)
if not input_path or input_path == "" then
return
end
-- reset &path
if path_existed then
vim.api.nvim_set_option_value("path", path_opt, { buf = bufnr })
else
vim.api.nvim_set_option_value("path", nil, { buf = bufnr })
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)
end
return M