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

Commit 65059b0

Browse files
committed
fix: issue with path completion(#23, #55)
1 parent 2fb8bba commit 65059b0

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

lua/source/path.lua

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@ local function onDirScanned(_, data)
1313
return vim.loop.fs_scandir_next(data)
1414
end
1515
for name, type in iter do
16-
table.insert(M.items, {type = type, name=name})
16+
table.insert(M.items, {type = type, name=name})
1717
end
1818
end
1919
M.callback = true
2020
end
2121

22+
2223
local fileTypesMap = setmetatable({
23-
['file'] = "(file)",
24-
['directory'] = "(dir)",
25-
['char'] = "(char)",
26-
['link'] = "(link)",
27-
['block'] = "(block)",
28-
['fifo'] = "(pipe)",
29-
['socket'] = "(socket)"
24+
file = "(file)",
25+
directory = "(dir)",
26+
char = "(char)",
27+
link = "(link)",
28+
block = "(block)",
29+
fifo = "(pipe)",
30+
socket = "(socket)"
3031
}, {__index = function()
3132
return '(unknown)'
3233
end
@@ -39,7 +40,8 @@ M.getCompletionItems = function(prefix, score_func)
3940
if score < #prefix/3 or #prefix == 0 then
4041
table.insert(complete_items, {
4142
word = val.name,
42-
kind = 'Path ' .. fileTypesMap[val.type],
43+
kind = 'Path',
44+
menu = fileTypesMap[val.type],
4345
score = score,
4446
icase = 1,
4547
dup = 1,
@@ -58,13 +60,19 @@ M.triggerFunction = function()
5860
local pos = api.nvim_win_get_cursor(0)
5961
local line = api.nvim_get_current_line()
6062
local line_to_cursor = line:sub(1, pos[2])
61-
local keyword = line_to_cursor:match("[^%s\"].*")
62-
if keyword ~= '/' then
63-
-- TODO rewrite this
64-
local index = string.find(keyword:reverse(), '/') or 1
63+
local keyword
64+
if vim.v.completed_item ~= nil and vim.v.completed_item.kind == 'Path' and line_to_cursor:find(vim.v.completed_item.word) then
65+
keyword = M.keyword..vim.v.completed_item.word..'/'
66+
else
67+
M.keyword = nil
68+
keyword = line_to_cursor:match("[^%s\"\']+%S*/?$")
69+
end
70+
71+
if keyword ~= nil and keyword ~= '/' then
72+
local index = string.find(keyword:reverse(), '/')
73+
if index == nil then index = keyword:len() + 1 end
6574
local length = string.len(keyword) - index + 1
6675
keyword = string.sub(keyword, 1, length)
67-
-- keyword = keyword:match("%s*(%S+)%w*/.*$")
6876
end
6977

7078
local path = vim.fn.expand('%:p:h')
@@ -79,6 +87,7 @@ M.triggerFunction = function()
7987
end
8088
end
8189

90+
M.keyword = keyword
8291
M.items = {}
8392
loop.fs_scandir(path, onDirScanned)
8493
end

0 commit comments

Comments
 (0)