Skip to content

Instantly share code, notes, and snippets.

@marcelarie
Created December 19, 2021 14:47
Show Gist options
  • Save marcelarie/8dde88be95d7f1f22efac9ae09bd01fb to your computer and use it in GitHub Desktop.
Save marcelarie/8dde88be95d7f1f22efac9ae09bd01fb to your computer and use it in GitHub Desktop.
cmp source creation
local source = {}
---Source constructor.
source.new = function()
local self = setmetatable({}, { __index = source })
self.your_awesome_variable = 1
return self
end
---Return the source name for some information.
source.get_debug_name = function()
return 'example'
end
---Return the source is available or not.
---@return boolean
function source:is_available()
return true
end
---Return keyword pattern which will be used...
--- 1. Trigger keyword completion
--- 2. Detect menu start offset
--- 3. Reset completion state
---@return string
function source:get_keyword_pattern()
return '???'
end
---Return trigger characters.
---@return string[]
function source:get_trigger_characters()
return { ??? }
end
---Invoke completion (required).
--- If you want to abort completion, just call the callback without arguments.
---@param request cmp.CompletionRequest
---@param callback fun(response: lsp.CompletionResponse|nil)
function source:complete(request, callback)
callback({
{ label = 'January' },
{ label = 'February' },
{ label = 'March' },
{ label = 'April' },
{ label = 'May' },
{ label = 'June' },
{ label = 'July' },
{ label = 'August' },
{ label = 'September' },
{ label = 'October' },
{ label = 'November' },
{ label = 'December' },
})
end
---Resolve completion item that will be called when the item selected or before the item confirmation.
---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil)
function source:resolve(completion_item, callback)
callback(completion_item)
end
---Execute command that will be called when after the item confirmation.
---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil)
function source:execute(completion_item, callback)
callback(completion_item)
end
return source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment