Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit c6cc120

Browse files
committed
Use fs_scandir instead find command
1 parent 0e716d7 commit c6cc120

1 file changed

Lines changed: 22 additions & 36 deletions

File tree

lua/source/path.lua

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,35 @@ local api = vim.api
66
M.items = {}
77
M.callback = false
88

9-
-- onread handler for vim.loop
10-
local function onread(err, data)
9+
-- onDirScanned handler for vim.loop
10+
local function onDirScanned(err, data)
1111
if err then
1212
-- print('ERROR: ', err)
1313
-- TODO handle err
1414
end
1515
if data then
16-
local vals = vim.split(data, "\n")
17-
for _,i in pairs(vals) do
18-
if #i ~= 0 then
19-
table.insert(M.items, {t = i:sub(1,1), name = i:sub(3)})
20-
end
16+
local function iter()
17+
return vim.loop.fs_scandir_next(data)
18+
end
19+
for name, type in iter do
20+
table.insert(M.items, {type = type, name=name})
2121
end
2222
end
23+
M.callback = true
2324
end
2425

25-
local fileTypesMap = {
26-
['f'] = "(file)",
27-
['d'] = "(dir)",
28-
['c'] = "(char)",
29-
['l'] = "(link)",
30-
['b'] = "(block)",
31-
['p'] = "(pipe)",
32-
['s'] = "(socket)"
33-
}
26+
local fileTypesMap = setmetatable({
27+
['file'] = "(file)",
28+
['directory'] = "(dir)",
29+
['char'] = "(char)",
30+
['link'] = "(link)",
31+
['block'] = "(block)",
32+
['fifo'] = "(pipe)",
33+
['socket'] = "(socket)"
34+
}, {__index = function()
35+
return '(unknown)'
36+
end
37+
})
3438

3539
M.getCompletionItems = function(prefix, score_func)
3640
local complete_items = {}
@@ -39,7 +43,7 @@ M.getCompletionItems = function(prefix, score_func)
3943
if score < #prefix/3 or #prefix == 0 then
4044
table.insert(complete_items, {
4145
word = val.name,
42-
kind = 'Path ' .. fileTypesMap[val.t],
46+
kind = 'Path ' .. fileTypesMap[val.type],
4347
score = score,
4448
icase = 1,
4549
dup = 1,
@@ -55,7 +59,6 @@ M.getCallback = function()
5559
return M.callback
5660
end
5761

58-
5962
M.triggerFunction = function(_, _, _, manager)
6063
local pos = api.nvim_win_get_cursor(0)
6164
local line = api.nvim_get_current_line()
@@ -90,24 +93,7 @@ M.triggerFunction = function(_, _, _, manager)
9093
::continue::
9194
path = path..'/'
9295
M.items = {}
93-
local stdout = vim.loop.new_pipe(false)
94-
local stderr = vim.loop.new_pipe(false)
95-
local handle, pid
96-
handle, pid = vim.loop.spawn('find', {
97-
args = {path, '-mindepth', '1', '-maxdepth', '1', '-printf', '%y %f\n'},
98-
stdio = {stdout,stderr}
99-
},
100-
vim.schedule_wrap(function()
101-
stdout:read_stop()
102-
stderr:read_stop()
103-
stdout:close()
104-
stderr:close()
105-
handle:close()
106-
M.callback = true
107-
end
108-
))
109-
vim.loop.read_start(stdout, onread)
110-
vim.loop.read_start(stderr, onread)
96+
vim.loop.fs_scandir(path, onDirScanned)
11197
end
11298

11399
return M

0 commit comments

Comments
 (0)