From cb3ed033101e8f3663727fbd1a4a15cd42138f50 Mon Sep 17 00:00:00 2001 From: Jan Valiska Date: Thu, 7 May 2020 01:29:20 +0200 Subject: [PATCH 1/5] Fix path completion for files with spaces --- lua/source/path.lua | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/lua/source/path.lua b/lua/source/path.lua index f5227ba..534a754 100644 --- a/lua/source/path.lua +++ b/lua/source/path.lua @@ -13,22 +13,33 @@ local function onread(err, data) -- TODO handle err end if data then - for i in string.gmatch(data, "%S+") do + local vals = vim.split(data, "\n") + for _,i in pairs(vals) do if #i ~= 0 then - table.insert(M.items, i) + table.insert(M.items, {t = i:sub(1,1), name = i:sub(3)}) end end end end +local fileTypesMap = { + ['f'] = "[file]", + ['d'] = "[dir]", + ['c'] = "[char]", + ['l'] = "[link]", + ['b'] = "[block]", + ['p'] = "[pipe]", + ['s'] = "[socket]" +} + M.getCompletionItems = function(prefix, score_func) local complete_items = {} for _, val in ipairs(M.items) do - local score = score_func(prefix, val) + local score = score_func(prefix, val.name) if score < #prefix/3 or #prefix == 0 then table.insert(complete_items, { - word = val, - kind = 'Path', + word = val.name, + kind = fileTypesMap[val.t], score = score, icase = 1, dup = 1, @@ -55,7 +66,6 @@ M.triggerFunction = function(_, _, _, manager) keyword = keyword:match("%s*(%S+)%w*/.*$") end local path = vim.fn.expand('%:p:h') - print(keyword) if keyword ~= nil then -- dealing with special case in matching if keyword == "/" and line:sub(pos[2], pos[2]) then @@ -80,11 +90,12 @@ M.triggerFunction = function(_, _, _, manager) ::continue:: path = path..'/' M.items = {} - local stdout = vim.loop.new_pipe(false) - local stderr = vim.loop.new_pipe(false) + local stdout = vim.loop.new_pipe(true) + local stderr = vim.loop.new_pipe(true) local handle, pid - handle, pid = vim.loop.spawn('ls', { - args = {path, '-A'}, + print(path) + handle, pid = vim.loop.spawn('find', { + args = {path, '-mindepth', '1', '-maxdepth', '1', '-printf', '%y %f\n'}, stdio = {stdout,stderr} }, vim.schedule_wrap(function() From dc923c367e025d91abeea7c6803119025681c960 Mon Sep 17 00:00:00 2001 From: Jan Valiska Date: Thu, 7 May 2020 01:32:50 +0200 Subject: [PATCH 2/5] Revert back pipes parameters --- lua/source/path.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/source/path.lua b/lua/source/path.lua index 534a754..d359417 100644 --- a/lua/source/path.lua +++ b/lua/source/path.lua @@ -90,8 +90,8 @@ M.triggerFunction = function(_, _, _, manager) ::continue:: path = path..'/' M.items = {} - local stdout = vim.loop.new_pipe(true) - local stderr = vim.loop.new_pipe(true) + local stdout = vim.loop.new_pipe(false) + local stderr = vim.loop.new_pipe(false) local handle, pid print(path) handle, pid = vim.loop.spawn('find', { From 8365fff7cf82c7f9745e9c38d747df222baddeb7 Mon Sep 17 00:00:00 2001 From: Jan Valiska Date: Thu, 7 May 2020 01:46:30 +0200 Subject: [PATCH 3/5] Use conventional file type identifiers in path completion --- lua/source/path.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/source/path.lua b/lua/source/path.lua index d359417..10852c7 100644 --- a/lua/source/path.lua +++ b/lua/source/path.lua @@ -23,13 +23,13 @@ local function onread(err, data) end local fileTypesMap = { - ['f'] = "[file]", - ['d'] = "[dir]", - ['c'] = "[char]", - ['l'] = "[link]", - ['b'] = "[block]", - ['p'] = "[pipe]", - ['s'] = "[socket]" + ['f'] = "(file)", + ['d'] = "(dir)", + ['c'] = "(char)", + ['l'] = "(link)", + ['b'] = "(block)", + ['p'] = "(pipe)", + ['s'] = "(socket)" } M.getCompletionItems = function(prefix, score_func) @@ -39,7 +39,7 @@ M.getCompletionItems = function(prefix, score_func) if score < #prefix/3 or #prefix == 0 then table.insert(complete_items, { word = val.name, - kind = fileTypesMap[val.t], + kind = 'Path ' .. fileTypesMap[val.t], score = score, icase = 1, dup = 1, From 0e716d76ac361f390f36c5e70709089c2672eaa6 Mon Sep 17 00:00:00 2001 From: Jan Valiska Date: Thu, 7 May 2020 07:54:28 +0200 Subject: [PATCH 4/5] Remove unnecessary print function --- lua/source/path.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/source/path.lua b/lua/source/path.lua index 10852c7..1e500c8 100644 --- a/lua/source/path.lua +++ b/lua/source/path.lua @@ -93,7 +93,6 @@ M.triggerFunction = function(_, _, _, manager) local stdout = vim.loop.new_pipe(false) local stderr = vim.loop.new_pipe(false) local handle, pid - print(path) handle, pid = vim.loop.spawn('find', { args = {path, '-mindepth', '1', '-maxdepth', '1', '-printf', '%y %f\n'}, stdio = {stdout,stderr} From c6cc120b3c524062b7deda884d1e852a11c7e97f Mon Sep 17 00:00:00 2001 From: Jan Valiska Date: Thu, 7 May 2020 15:25:46 +0200 Subject: [PATCH 5/5] Use fs_scandir instead find command --- lua/source/path.lua | 58 +++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/lua/source/path.lua b/lua/source/path.lua index 1e500c8..482b232 100644 --- a/lua/source/path.lua +++ b/lua/source/path.lua @@ -6,31 +6,35 @@ local api = vim.api M.items = {} M.callback = false --- onread handler for vim.loop -local function onread(err, data) +-- onDirScanned handler for vim.loop +local function onDirScanned(err, data) if err then -- print('ERROR: ', err) -- TODO handle err end if data then - local vals = vim.split(data, "\n") - for _,i in pairs(vals) do - if #i ~= 0 then - table.insert(M.items, {t = i:sub(1,1), name = i:sub(3)}) - end + local function iter() + return vim.loop.fs_scandir_next(data) + end + for name, type in iter do + table.insert(M.items, {type = type, name=name}) end end + M.callback = true end -local fileTypesMap = { - ['f'] = "(file)", - ['d'] = "(dir)", - ['c'] = "(char)", - ['l'] = "(link)", - ['b'] = "(block)", - ['p'] = "(pipe)", - ['s'] = "(socket)" -} +local fileTypesMap = setmetatable({ + ['file'] = "(file)", + ['directory'] = "(dir)", + ['char'] = "(char)", + ['link'] = "(link)", + ['block'] = "(block)", + ['fifo'] = "(pipe)", + ['socket'] = "(socket)" +}, {__index = function() + return '(unknown)' + end +}) M.getCompletionItems = function(prefix, score_func) local complete_items = {} @@ -39,7 +43,7 @@ M.getCompletionItems = function(prefix, score_func) if score < #prefix/3 or #prefix == 0 then table.insert(complete_items, { word = val.name, - kind = 'Path ' .. fileTypesMap[val.t], + kind = 'Path ' .. fileTypesMap[val.type], score = score, icase = 1, dup = 1, @@ -55,7 +59,6 @@ M.getCallback = function() return M.callback end - M.triggerFunction = function(_, _, _, manager) local pos = api.nvim_win_get_cursor(0) local line = api.nvim_get_current_line() @@ -90,24 +93,7 @@ M.triggerFunction = function(_, _, _, manager) ::continue:: path = path..'/' M.items = {} - local stdout = vim.loop.new_pipe(false) - local stderr = vim.loop.new_pipe(false) - local handle, pid - handle, pid = vim.loop.spawn('find', { - args = {path, '-mindepth', '1', '-maxdepth', '1', '-printf', '%y %f\n'}, - stdio = {stdout,stderr} - }, - vim.schedule_wrap(function() - stdout:read_stop() - stderr:read_stop() - stdout:close() - stderr:close() - handle:close() - M.callback = true - end - )) - vim.loop.read_start(stdout, onread) - vim.loop.read_start(stderr, onread) + vim.loop.fs_scandir(path, onDirScanned) end return M