local cmp = require'cmp' local NAME_REGEX = [[\%([^/\\:\*?<>'"`\|]\)]] local PATH_REGEX = ([[\%(/PAT\+\)*\ze/PAT*]]):gsub('PAT', NAME_REGEX) local source = {} source.new = function() return setmetatable({}, { __index = source }) end source.get_trigger_characters = function() return { '/', '.' } end source.get_keyword_pattern = function() return '/' .. NAME_REGEX .. '*' end source.complete = function(self, request, callback) local dirname = self:_dirname(request) if not dirname then return callback() end local stat = self:_stat(dirname) if not stat then return callback() end self:_candidates(request.context, dirname, request.offset, function(err, candidates) if err then return callback() end callback(candidates) end) end source._dirname = function(self, request) local s = vim.regex(PATH_REGEX):match_str(request.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 buf_dirname = vim.fn.expand(('#%d:p:h'):format(request.context.bufnr)) if prefix:match('%.%./$') then return vim.fn.resolve(buf_dirname .. '/../' .. dirname) elseif prefix:match('%./$') then return vim.fn.resolve(buf_dirname .. '/' .. dirname) elseif prefix:match('~/$') then return vim.fn.expand('~/' .. dirname), request.offset elseif prefix:match('%$[%a_]+/$') then return vim.fn.expand(prefix:match('%$[%a_]+/$') .. dirname) elseif prefix:match('/$') then local accept = true -- Ignore URL components accept = accept and not prefix:match('%a/$') -- Ignore URL scheme accept = accept and not prefix:match('%a+:/$') and not prefix:match('%a+://$') -- Ignore HTML closing tags accept = accept and not prefix:match('