Merge branch 'main' of https://github.com/hrsh7th/cmp-path into hrsh7th-main

This commit is contained in:
Nir Tzachar 2021-10-06 14:35:53 +03:00
commit 9b4c53f2e8

View File

@ -1,7 +1,7 @@
local cmp = require'cmp' local cmp = require'cmp'
local NAME_REGEX = [[\%([^/\\:\*?<>'"`\|]\)]] local NAME_REGEX = '\\%([^/\\\\:\\*?<>\'"`\\|]\\)'
local PATH_REGEX = ([[\%(/PAT\+\)*\ze/PAT*]]):gsub('PAT', NAME_REGEX) local PATH_REGEX = vim.regex(([[\%(/PAT\+\)*/\zePAT*$]]):gsub('PAT', NAME_REGEX))
local source = {} local source = {}
@ -18,11 +18,11 @@ source.get_trigger_characters = function()
end end
source.get_keyword_pattern = function() source.get_keyword_pattern = function()
return '/' .. NAME_REGEX .. '*' return NAME_REGEX .. '*'
end end
source.complete = function(self, request, callback) source.complete = function(self, params, callback)
local dirname = self:_dirname(request) local dirname = self:_dirname(params)
if not dirname then if not dirname then
return callback() return callback()
end end
@ -32,7 +32,7 @@ source.complete = function(self, request, callback)
return callback() return callback()
end end
self:_candidates(request.context, dirname, request.offset, function(err, candidates) self:_candidates(params, dirname, params.offset, function(err, candidates)
if err then if err then
return callback() return callback()
end end
@ -40,22 +40,23 @@ source.complete = function(self, request, callback)
end) end)
end end
source._dirname = function(self, request) source._dirname = function(self, params)
local s = vim.regex(PATH_REGEX):match_str(request.context.cursor_before_line) local s = PATH_REGEX:match_str(params.context.cursor_before_line)
if not s then if not s then
return nil return nil
end end
local dirname = string.sub(request.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(request.context.cursor_before_line, 1, s + 1) -- include '/' 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 if prefix:match('%.%./$') then
local buf_dirname = vim.fn.expand(('#%d:p:h'):format(params.context.bufnr))
return vim.fn.resolve(buf_dirname .. '/../' .. dirname) return vim.fn.resolve(buf_dirname .. '/../' .. dirname)
elseif prefix:match('%./$') then elseif prefix:match('%./$') then
local buf_dirname = vim.fn.expand(('#%d:p:h'):format(params.context.bufnr))
return vim.fn.resolve(buf_dirname .. '/' .. dirname) return vim.fn.resolve(buf_dirname .. '/' .. dirname)
elseif prefix:match('~/$') then elseif prefix:match('~/$') then
return vim.fn.expand('~/' .. dirname), request.offset return vim.fn.expand('~/' .. dirname), params.offset
elseif prefix:match('%$[%a_]+/$') then elseif prefix:match('%$[%a_]+/$') then
return vim.fn.expand(prefix:match('%$[%a_]+/$') .. dirname) return vim.fn.expand(prefix:match('%$[%a_]+/$') .. dirname)
elseif prefix:match('/$') then elseif prefix:match('/$') then
@ -106,7 +107,7 @@ local function try_get_lines(file, count)
end end
end end
source._candidates = function(_, context, dirname, offset, callback) source._candidates = function(_, params, dirname, offset, callback)
local fs, err = vim.loop.fs_scandir(dirname) local fs, err = vim.loop.fs_scandir(dirname)
if err then if err then
return callback(err, nil) return callback(err, nil)
@ -115,7 +116,7 @@ source._candidates = function(_, context, dirname, offset, callback)
local items = {} 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, offset) == '.'
while true do while true do
local name, type, e = vim.loop.fs_scandir_next(fs) local name, type, e = vim.loop.fs_scandir_next(fs)
if e then if e then
@ -133,9 +134,9 @@ source._candidates = function(_, context, dirname, offset, callback)
if accept then if accept then
if type == 'directory' then if type == 'directory' then
table.insert(items, { table.insert(items, {
word = '/' .. name, word = name,
label = '/' .. name, label = name,
insertText = '/' .. name .. '/', insertText = name .. '/',
kind = cmp.lsp.CompletionItemKind.Folder, kind = cmp.lsp.CompletionItemKind.Folder,
}) })
elseif type == 'link' then elseif type == 'link' then
@ -143,16 +144,16 @@ source._candidates = function(_, context, dirname, offset, callback)
if stat then if stat then
if stat.type == 'directory' then if stat.type == 'directory' then
table.insert(items, { table.insert(items, {
word = '/' .. name, word = name,
label = '/' .. name, label = name,
insertText = '/' .. name .. '/', insertText = name .. '/',
kind = cmp.lsp.CompletionItemKind.Folder, kind = cmp.lsp.CompletionItemKind.Folder,
}) })
else else
table.insert(items, { table.insert(items, {
label = name, label = name,
filterText = '/' .. name, filterText = name,
insertText = '/' .. name, insertText = name,
kind = cmp.lsp.CompletionItemKind.File, kind = cmp.lsp.CompletionItemKind.File,
data = {path = dirname .. '/' .. name}, data = {path = dirname .. '/' .. name},
}) })
@ -161,8 +162,8 @@ source._candidates = function(_, context, dirname, offset, callback)
elseif type == 'file' then elseif type == 'file' then
table.insert(items, { table.insert(items, {
label = name, label = name,
filterText = '/' .. name, filterText = name,
insertText = '/' .. name, insertText = name,
kind = cmp.lsp.CompletionItemKind.File, kind = cmp.lsp.CompletionItemKind.File,
data = {path = dirname .. '/' .. name}, data = {path = dirname .. '/' .. name},
}) })
@ -183,7 +184,7 @@ end
function source:resolve(completion_item, callback) function source:resolve(completion_item, callback)
if completion_item.kind == cmp.lsp.CompletionItemKind.File then if completion_item.kind == cmp.lsp.CompletionItemKind.File then
completion_item.documentation = try_get_lines(completion_item.data.path, 10) completion_item.documentation = try_get_lines(completion_item.data.path, defaults.max_lines)
end end
callback(completion_item) callback(completion_item)
end end