Skip to content

Commit 242a0af

Browse files
committed
feat(scandir): follow symlinks
1 parent bda256f commit 242a0af

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

lua/plenary/scandir.lua

+27-20
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,32 @@ local gen_search_pat = function(pattern)
100100
end
101101

102102
local process_item = function(opts, name, typ, current_dir, next_dir, bp, data, giti, msp)
103-
if opts.hidden or name:sub(1, 1) ~= "." then
104-
if typ == "directory" then
105-
local entry = current_dir .. os_sep .. name
106-
if opts.depth then
107-
table.insert(next_dir, handle_depth(bp, entry, opts.depth))
108-
else
109-
table.insert(next_dir, entry)
110-
end
111-
if opts.add_dirs or opts.only_dirs then
112-
if not giti or giti(bp, entry .. "/") then
103+
if opts.symlink or typ ~= "link" then
104+
if opts.symlink and typ == "link" then
105+
typ = uv.fs_stat(current_dir .. os_sep .. name).type
106+
end
107+
108+
if opts.hidden or name:sub(1, 1) ~= "." then
109+
if typ == "directory" then
110+
local entry = current_dir .. os_sep .. name
111+
if opts.depth then
112+
table.insert(next_dir, handle_depth(bp, entry, opts.depth))
113+
else
114+
table.insert(next_dir, entry)
115+
end
116+
if opts.add_dirs or opts.only_dirs then
117+
if not giti or giti(bp, entry .. "/") then
118+
if not msp or msp(entry) then
119+
table.insert(data, entry)
120+
if opts.on_insert then
121+
opts.on_insert(entry, typ)
122+
end
123+
end
124+
end
125+
end
126+
elseif not opts.only_dirs then
127+
local entry = current_dir .. os_sep .. name
128+
if not giti or giti(bp, entry) then
113129
if not msp or msp(entry) then
114130
table.insert(data, entry)
115131
if opts.on_insert then
@@ -118,16 +134,6 @@ local process_item = function(opts, name, typ, current_dir, next_dir, bp, data,
118134
end
119135
end
120136
end
121-
elseif not opts.only_dirs then
122-
local entry = current_dir .. os_sep .. name
123-
if not giti or giti(bp, entry) then
124-
if not msp or msp(entry) then
125-
table.insert(data, entry)
126-
if opts.on_insert then
127-
opts.on_insert(entry, typ)
128-
end
129-
end
130-
end
131137
end
132138
end
133139
end
@@ -146,6 +152,7 @@ end
146152
-- opts.search_pattern (regex): regex for which files will be added, string, table of strings, or fn(e) -> bool
147153
-- opts.on_insert(entry): Will be called for each element
148154
-- opts.silent (bool): if true will not echo messages that are not accessible
155+
-- opts.symlink (bool): if true will follow symlinks
149156
-- @return array with files
150157
m.scan_dir = function(path, opts)
151158
opts = opts or {}

tests/plenary/job.symlink

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
job

tests/plenary/scandir_spec.lua

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ describe("scandir", function()
6565
eq(false, contains(dirs, "./asdf/asdf/adsf.lua"))
6666
end)
6767

68+
it("with symlinks", function()
69+
local dirs = scan.scan_dir(".", { symlink = true })
70+
eq("table", type(dirs))
71+
eq(true, contains(dirs, "./tests/plenary/job.symlink/validation_spec.lua"))
72+
eq(true, contains(dirs, "./README.md"))
73+
eq(true, contains(dirs, "./lua/plenary/job.lua"))
74+
end)
75+
6876
it("with add directories", function()
6977
local dirs = scan.scan_dir(".", { add_dirs = true })
7078
eq("table", type(dirs))

0 commit comments

Comments
 (0)