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

Commit ea02002

Browse files
authored
Merge pull request #56 from JanValiska/master
Fixes completion of files with spaces and adds file type identifier
2 parents ff75e8f + c6cc120 commit ea02002

1 file changed

Lines changed: 25 additions & 29 deletions

File tree

lua/source/path.lua

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,44 @@ 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-
for i in string.gmatch(data, "%S+") do
17-
if #i ~= 0 then
18-
table.insert(M.items, i)
19-
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})
2021
end
2122
end
23+
M.callback = true
2224
end
2325

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+
})
38+
2439
M.getCompletionItems = function(prefix, score_func)
2540
local complete_items = {}
2641
for _, val in ipairs(M.items) do
27-
local score = score_func(prefix, val)
42+
local score = score_func(prefix, val.name)
2843
if score < #prefix/3 or #prefix == 0 then
2944
table.insert(complete_items, {
30-
word = val,
31-
kind = 'Path',
45+
word = val.name,
46+
kind = 'Path ' .. fileTypesMap[val.type],
3247
score = score,
3348
icase = 1,
3449
dup = 1,
@@ -44,7 +59,6 @@ M.getCallback = function()
4459
return M.callback
4560
end
4661

47-
4862
M.triggerFunction = function(_, _, _, manager)
4963
local pos = api.nvim_win_get_cursor(0)
5064
local line = api.nvim_get_current_line()
@@ -55,7 +69,6 @@ M.triggerFunction = function(_, _, _, manager)
5569
keyword = keyword:match("%s*(%S+)%w*/.*$")
5670
end
5771
local path = vim.fn.expand('%:p:h')
58-
print(keyword)
5972
if keyword ~= nil then
6073
-- dealing with special case in matching
6174
if keyword == "/" and line:sub(pos[2], pos[2]) then
@@ -80,24 +93,7 @@ M.triggerFunction = function(_, _, _, manager)
8093
::continue::
8194
path = path..'/'
8295
M.items = {}
83-
local stdout = vim.loop.new_pipe(false)
84-
local stderr = vim.loop.new_pipe(false)
85-
local handle, pid
86-
handle, pid = vim.loop.spawn('ls', {
87-
args = {path, '-A'},
88-
stdio = {stdout,stderr}
89-
},
90-
vim.schedule_wrap(function()
91-
stdout:read_stop()
92-
stderr:read_stop()
93-
stdout:close()
94-
stderr:close()
95-
handle:close()
96-
M.callback = true
97-
end
98-
))
99-
vim.loop.read_start(stdout, onread)
100-
vim.loop.read_start(stderr, onread)
96+
vim.loop.fs_scandir(path, onDirScanned)
10197
end
10298

10399
return M

0 commit comments

Comments
 (0)