format, use OOP syntax, improve docs

This commit is contained in:
Felipe Lema 2024-10-11 10:40:58 -03:00
parent 403576b67f
commit e3206e521c

View File

@ -30,7 +30,9 @@ source.get_trigger_characters = function() return {'/', '.'} end
source.get_keyword_pattern = function(_, _) return NAME_REGEX .. '*' end source.get_keyword_pattern = function(_, _) return NAME_REGEX .. '*' end
source.complete = function(self, params, callback) ---@param params cmp.SourceCompletionApiParams
---@param callback fun(response: lsp.CompletionResponse|nil)
function source:complete(params, callback)
local option = self:_validate_option(params) local option = self:_validate_option(params)
local dirname = self:_dirname(params, option) local dirname = self:_dirname(params, option)
@ -40,7 +42,10 @@ source.complete = function(self, params, callback)
local include_hidden = option.show_hidden_files_by_default or local include_hidden = option.show_hidden_files_by_default or
string.sub(params.context.cursor_before_line, params.offset, params.offset) == '.' string.sub(params.context.cursor_before_line, params.offset, params.offset) == '.'
self:_candidates(dirname, include_hidden, option, function(err, candidates) self:_candidates(dirname, include_hidden, option,
---@param err nil|string
---@param candidates lsp.CompletionResponse|nil
function(err, candidates)
if err then if err then
return callback() return callback()
end end
@ -49,10 +54,9 @@ source.complete = function(self, params, callback)
end end
--- get documentation in separate thread --- get documentation in separate thread
---@param _ any
---@param completion_item lsp.CompletionItem ---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil) ---@param callback fun(completion_item: lsp.CompletionItem|nil)
source.resolve = function(_, completion_item, callback) function source:resolve(completion_item, callback)
local data = completion_item.data local data = completion_item.data
---@diagnostic disable-next-line: undefined-field ---@diagnostic disable-next-line: undefined-field
if not data.stat or data.stat.type ~= 'file' then if not data.stat or data.stat.type ~= 'file' then
@ -138,7 +142,11 @@ source.resolve = function(_, completion_item, callback)
work:queue(data.path, constants.max_lines or -1, cmp.lsp.MarkupKind.Markdown) work:queue(data.path, constants.max_lines or -1, cmp.lsp.MarkupKind.Markdown)
end end
source._dirname = function(self, params, option) --- Try to match a path before cursor and return its dirname
--- Try to work around non-literal paths, like resolving env vars
---@param params cmp.SourceCompletionApiParams
---@param option cmp_path.Option
function source:_dirname(params, option)
local s = PATH_REGEX:match_str(params.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
@ -181,7 +189,7 @@ source._dirname = function(self, params, option)
accept = accept and not prefix:match('[%d%)]%s*/$') accept = accept and not prefix:match('[%d%)]%s*/$')
-- Ignore / comment -- Ignore / comment
accept = accept and accept = accept and
(not prefix:match('^[%s/]*$') or not self:_is_slash_comment()) (not prefix:match('^[%s/]*$') or not self:_is_slash_comment_p())
if accept then if accept then
return vim.fn.resolve('/' .. dirname) return vim.fn.resolve('/' .. dirname)
end end
@ -189,7 +197,12 @@ source._dirname = function(self, params, option)
return nil return nil
end end
source._candidates = function(_, dirname, include_hidden, option, callback) --- call cmp's callback(entries) after retrieving entries in a separate thread
---@param dirname string
---@param include_hidden boolean
---@param option cmp_path.Option
---@param callback function(err:nil|string, candidates:lsp.CompletionResponse|nil)
function source:_candidates(dirname, include_hidden, option, callback)
local entries, err = vim.uv.fs_scandir(dirname) local entries, err = vim.uv.fs_scandir(dirname)
if err then if err then
return callback(err, nil) return callback(err, nil)
@ -197,9 +210,10 @@ source._candidates = function(_, dirname, include_hidden, option, callback)
local work local work
work = assert(vim.uv.new_work( work = assert(vim.uv.new_work(
--- create path entries --- Collect path entries, serialize them and return them
--- This function is called in a separate thread, so errors are caught and serialized
---@param _entries uv_fs_t ---@param _entries uv_fs_t
---@param _dirname any see vim.fn.resolve() ---@param _dirname string see vim.fn.resolve()
---@param _include_hidden boolean ---@param _include_hidden boolean
---@param label_trailing_slash boolean ---@param label_trailing_slash boolean
---@param trailing_slash boolean ---@param trailing_slash boolean
@ -269,8 +283,9 @@ source._candidates = function(_, dirname, include_hidden, option, callback)
---@diagnostic disable-next-line: redundant-return-value ---@diagnostic disable-next-line: redundant-return-value
return nil, vim.json.encode(items) return nil, vim.json.encode(items)
end, end,
--- --- Receive serialiazed entries, deserialize them, call callback(entries)
---@param worker_error string|nil non-nil if some error happened in worker --- This function is called in the main thread
---@param worker_error string|nil non-nil if some error happened in worker thread
---@param serialized_items string array-of-items serialized as string ---@param serialized_items string array-of-items serialized as string
function(worker_error, serialized_items) function(worker_error, serialized_items)
if worker_error then if worker_error then
@ -289,7 +304,8 @@ source._candidates = function(_, dirname, include_hidden, option, callback)
cmp.lsp.CompletionItemKind.Folder) cmp.lsp.CompletionItemKind.Folder)
end end
source._is_slash_comment = function(_) --- using «/» as comment in current buffer?
function source:_is_slash_comment_p()
local commentstring = vim.bo.commentstring or '' local commentstring = vim.bo.commentstring or ''
local no_filetype = vim.bo.filetype == '' local no_filetype = vim.bo.filetype == ''
local is_slash_comment = false local is_slash_comment = false
@ -298,9 +314,10 @@ source._is_slash_comment = function(_)
return is_slash_comment and not no_filetype return is_slash_comment and not no_filetype
end end
---@param params cmp.SourceCompletionApiParams
---@return cmp_path.Option ---@return cmp_path.Option
source._validate_option = function(_, params) function source:_validate_option(params)
local option = vim.tbl_deep_extend('keep', params.option, defaults) local option = assert(vim.tbl_deep_extend('keep', params.option, defaults))
vim.validate({ vim.validate({
trailing_slash = {option.trailing_slash, 'boolean'}, trailing_slash = {option.trailing_slash, 'boolean'},
label_trailing_slash = {option.label_trailing_slash, 'boolean'}, label_trailing_slash = {option.label_trailing_slash, 'boolean'},
@ -310,5 +327,4 @@ source._validate_option = function(_, params)
return option return option
end end
return source return source