From 62ecedd4bd255a31581ba16c3b29d725539e242e Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Sat, 4 Sep 2021 21:27:26 +0900 Subject: [PATCH 1/6] Avoid `vim.fn.expand` --- lua/cmp_path/init.lua | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 2a22481..355de21 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -17,8 +17,8 @@ source.get_keyword_pattern = function() return '/' .. NAME_REGEX .. '*' end -source.complete = function(self, request, callback) - local dirname = self:_dirname(request) +source.complete = function(self, params, callback) + local dirname = self:_dirname(params) if not dirname then return callback() end @@ -28,7 +28,7 @@ source.complete = function(self, request, callback) return callback() end - self:_candidates(request.context, dirname, request.offset, function(err, candidates) + self:_candidates(params, dirname, params.offset, function(err, candidates) if err then return callback() end @@ -36,22 +36,23 @@ source.complete = function(self, request, callback) end) end -source._dirname = function(self, request) - local s = vim.regex(PATH_REGEX):match_str(request.context.cursor_before_line) +source._dirname = function(self, params) + local s = vim.regex(PATH_REGEX):match_str(params.context.cursor_before_line) if not s then return nil end - local dirname = string.sub(request.context.cursor_before_line, s + 2) -- exclude '/' - local prefix = string.sub(request.context.cursor_before_line, 1, s + 1) -- include '/' + local dirname = string.sub(params.context.cursor_before_line, s + 2) -- exclude '/' + local prefix = string.sub(params.context.cursor_before_line, 1, s + 1) -- include '/' - local buf_dirname = vim.fn.expand(('#%d:p:h'):format(request.context.bufnr)) if prefix:match('%.%./$') then + local buf_dirname = vim.fn.expand(('#%d:p:h'):format(params.context.bufnr)) return vim.fn.resolve(buf_dirname .. '/../' .. dirname) elseif prefix:match('%./$') then + local buf_dirname = vim.fn.expand(('#%d:p:h'):format(params.context.bufnr)) return vim.fn.resolve(buf_dirname .. '/' .. dirname) elseif prefix:match('~/$') then - return vim.fn.expand('~/' .. dirname), request.offset + return vim.fn.expand('~/' .. dirname), params.offset elseif prefix:match('%$[%a_]+/$') then return vim.fn.expand(prefix:match('%$[%a_]+/$') .. dirname) elseif prefix:match('/$') then @@ -81,7 +82,7 @@ source._stat = function(_, path) return nil end -source._candidates = function(_, context, dirname, offset, callback) +source._candidates = function(self, params, dirname, offset, callback) local fs, err = vim.loop.fs_scandir(dirname) if err then return callback(err, nil) @@ -89,7 +90,7 @@ source._candidates = function(_, context, dirname, offset, callback) local items = {} - local include_hidden = string.sub(context.cursor_before_line, offset + 1, offset + 1) == '.' + local include_hidden = string.sub(params.context.cursor_before_line, offset + 1, offset + 1) == '.' while true do local name, type, e = vim.loop.fs_scandir_next(fs) if e then From 8889960ab76f80b588f6edbac6468126caad5d19 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Sat, 4 Sep 2021 21:28:21 +0900 Subject: [PATCH 2/6] Regex compile first --- lua/cmp_path/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 355de21..66622e9 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -1,7 +1,7 @@ local cmp = require'cmp' local NAME_REGEX = [[\%([^/\\:\*?<>'"`\|]\)]] -local PATH_REGEX = ([[\%(/PAT\+\)*\ze/PAT*]]):gsub('PAT', NAME_REGEX) +local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*\ze/PAT*]]):gsub('PAT', NAME_REGEX)) local source = {} @@ -37,7 +37,7 @@ source.complete = function(self, params, callback) end source._dirname = function(self, params) - local s = vim.regex(PATH_REGEX):match_str(params.context.cursor_before_line) + local s = PATH_REGEX:match_str(params.context.cursor_before_line) if not s then return nil end From 8450341c7e50cc8ae0faee1fafe60b3f516cea79 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Mon, 6 Sep 2021 19:28:26 +0900 Subject: [PATCH 3/6] Don't include '/' as keywords --- lua/cmp_path/init.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 66622e9..5b86351 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -1,7 +1,7 @@ local cmp = require'cmp' local NAME_REGEX = [[\%([^/\\:\*?<>'"`\|]\)]] -local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*\ze/PAT*]]):gsub('PAT', NAME_REGEX)) +local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*/\zePAT*]]):gsub('PAT', NAME_REGEX)) local source = {} @@ -14,7 +14,7 @@ source.get_trigger_characters = function() end source.get_keyword_pattern = function() - return '/' .. NAME_REGEX .. '*' + return NAME_REGEX .. '*' end source.complete = function(self, params, callback) @@ -82,7 +82,7 @@ source._stat = function(_, path) return nil end -source._candidates = function(self, params, dirname, offset, callback) +source._candidates = function(_, params, dirname, offset, callback) local fs, err = vim.loop.fs_scandir(dirname) if err then return callback(err, nil) @@ -108,9 +108,9 @@ source._candidates = function(self, params, dirname, offset, callback) if accept then if type == 'directory' then table.insert(items, { - word = '/' .. name, - label = '/' .. name, - insertText = '/' .. name .. '/', + word = name, + label = name, + insertText = name .. '/', kind = cmp.lsp.CompletionItemKind.Folder, }) elseif type == 'link' then @@ -118,16 +118,16 @@ source._candidates = function(self, params, dirname, offset, callback) if stat then if stat.type == 'directory' then table.insert(items, { - word = '/' .. name, - label = '/' .. name, - insertText = '/' .. name .. '/', + word = name, + label = name, + insertText = name .. '/', kind = cmp.lsp.CompletionItemKind.Folder, }) else table.insert(items, { label = name, - filterText = '/' .. name, - insertText = '/' .. name, + filterText = name, + insertText = name, kind = cmp.lsp.CompletionItemKind.File, }) end @@ -135,8 +135,8 @@ source._candidates = function(self, params, dirname, offset, callback) elseif type == 'file' then table.insert(items, { label = name, - filterText = '/' .. name, - insertText = '/' .. name, + filterText = name, + insertText = name, kind = cmp.lsp.CompletionItemKind.File, }) end From 3a29c5131a4c6b319849362a2eecba9d8b1a4aac Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Wed, 8 Sep 2021 13:36:26 +0900 Subject: [PATCH 4/6] Fix PATH_REGEX --- lua/cmp_path/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 5b86351..84eda19 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -1,7 +1,7 @@ local cmp = require'cmp' -local NAME_REGEX = [[\%([^/\\:\*?<>'"`\|]\)]] -local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*/\zePAT*]]):gsub('PAT', NAME_REGEX)) +local NAME_REGEX = '\\%([^/\\\\:\\*?<>\'"`\\|]\\)' +local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*/\zePAT*$]]):gsub('PAT', NAME_REGEX)) local source = {} From 833f9f5a3872c24a8addd65299e433c6eb3d6815 Mon Sep 17 00:00:00 2001 From: py2048 <66913697+py2048@users.noreply.github.com> Date: Sat, 11 Sep 2021 23:21:08 +0700 Subject: [PATCH 5/6] Fix hidden files bug --- lua/cmp_path/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 84eda19..80446aa 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -90,7 +90,7 @@ source._candidates = function(_, params, dirname, offset, callback) local items = {} - local include_hidden = string.sub(params.context.cursor_before_line, offset + 1, offset + 1) == '.' + local include_hidden = string.sub(params.context.cursor_before_line, offset, offset) == '.' while true do local name, type, e = vim.loop.fs_scandir_next(fs) if e then From 8d88c92dd5a202a996b4caac7284e4ea2f086765 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Wed, 6 Oct 2021 20:23:03 +0900 Subject: [PATCH 6/6] Fix #7 --- lua/cmp_path/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/cmp_path/init.lua b/lua/cmp_path/init.lua index 80446aa..723ffc5 100644 --- a/lua/cmp_path/init.lua +++ b/lua/cmp_path/init.lua @@ -42,7 +42,7 @@ source._dirname = function(self, params) return nil end - local dirname = string.sub(params.context.cursor_before_line, s + 2) -- exclude '/' + local dirname = string.gsub(string.sub(params.context.cursor_before_line, s + 2), '%a*$', '') -- exclude '/' local prefix = string.sub(params.context.cursor_before_line, 1, s + 1) -- include '/' if prefix:match('%.%./$') then