Improve typings

This commit is contained in:
hrsh7th
2022-07-24 12:44:53 +09:00
parent 89df2cb223
commit bf5593df8e
24 changed files with 154 additions and 130 deletions

View File

@@ -14,7 +14,7 @@ config.cache = cache.new()
---@type cmp.ConfigSchema ---@type cmp.ConfigSchema
config.global = require('cmp.config.default')() config.global = require('cmp.config.default')()
---@type table<number, cmp.ConfigSchema> ---@type table<integer, cmp.ConfigSchema>
config.buffers = {} config.buffers = {}
---@type table<string, cmp.ConfigSchema> ---@type table<string, cmp.ConfigSchema>
@@ -36,7 +36,7 @@ end
---Set configuration for buffer ---Set configuration for buffer
---@param c cmp.ConfigSchema ---@param c cmp.ConfigSchema
---@param bufnr number|nil ---@param bufnr integer
config.set_buffer = function(c, bufnr) config.set_buffer = function(c, bufnr)
local revision = (config.buffers[bufnr] or {}).revision or 1 local revision = (config.buffers[bufnr] or {}).revision or 1
config.buffers[bufnr] = c or {} config.buffers[bufnr] = c or {}
@@ -150,9 +150,6 @@ end
---Return the current menu is native or not. ---Return the current menu is native or not.
config.is_native_menu = function() config.is_native_menu = function()
local c = config.get() local c = config.get()
if c.experimental and c.experimental.native_menu then
return true
end
if c.view and c.view.entries then if c.view and c.view.entries then
return c.view.entries == 'native' or c.view.entries.name == 'native' return c.view.entries == 'native' or c.view.entries.name == 'native'
end end
@@ -164,6 +161,7 @@ end
---@return cmp.ConfigSchema ---@return cmp.ConfigSchema
config.normalize = function(c) config.normalize = function(c)
-- make sure c is not 'nil' -- make sure c is not 'nil'
---@type any
c = c == nil and {} or c c = c == nil and {} or c
-- Normalize mapping. -- Normalize mapping.

View File

@@ -10,8 +10,8 @@ local api = require('cmp.utils.api')
---@field public prev_context cmp.Context ---@field public prev_context cmp.Context
---@field public option cmp.ContextOption ---@field public option cmp.ContextOption
---@field public filetype string ---@field public filetype string
---@field public time number ---@field public time integer
---@field public bufnr number ---@field public bufnr integer
---@field public cursor vim.Position|lsp.Position ---@field public cursor vim.Position|lsp.Position
---@field public cursor_line string ---@field public cursor_line string
---@field public cursor_after_line string ---@field public cursor_after_line string
@@ -31,8 +31,8 @@ context.empty = function()
end end
---Create new context ---Create new context
---@param prev_context cmp.Context ---@param prev_context? cmp.Context
---@param option cmp.ContextOption ---@param option? cmp.ContextOption
---@return cmp.Context ---@return cmp.Context
context.new = function(prev_context, option) context.new = function(prev_context, option)
option = option or {} option = option or {}
@@ -65,7 +65,7 @@ context.get_reason = function(self)
end end
---Get keyword pattern offset ---Get keyword pattern offset
---@return number|nil ---@return integer
context.get_offset = function(self, keyword_pattern) context.get_offset = function(self, keyword_pattern)
return self.cache:ensure({ 'get_offset', keyword_pattern, self.cursor_before_line }, function() return self.cache:ensure({ 'get_offset', keyword_pattern, self.cursor_before_line }, function()
return pattern.offset(keyword_pattern .. '\\m$', self.cursor_before_line) or self.cursor.col return pattern.offset(keyword_pattern .. '\\m$', self.cursor_before_line) or self.cursor.col

View File

@@ -49,7 +49,7 @@ core.register_source = function(self, s)
end end
---Unregister source ---Unregister source
---@param source_id string ---@param source_id integer
core.unregister_source = function(self, source_id) core.unregister_source = function(self, source_id)
self.sources[source_id] = nil self.sources[source_id] = nil
end end

View File

@@ -7,15 +7,15 @@ local types = require('cmp.types')
local matcher = require('cmp.matcher') local matcher = require('cmp.matcher')
---@class cmp.Entry ---@class cmp.Entry
---@field public id number ---@field public id integer
---@field public cache cmp.Cache ---@field public cache cmp.Cache
---@field public match_cache cmp.Cache ---@field public match_cache cmp.Cache
---@field public score number ---@field public score integer
---@field public exact boolean ---@field public exact boolean
---@field public matches table ---@field public matches table
---@field public context cmp.Context ---@field public context cmp.Context
---@field public source cmp.Source ---@field public source cmp.Source
---@field public source_offset number ---@field public source_offset integer
---@field public source_insert_range lsp.Range ---@field public source_insert_range lsp.Range
---@field public source_replace_range lsp.Range ---@field public source_replace_range lsp.Range
---@field public completion_item lsp.CompletionItem ---@field public completion_item lsp.CompletionItem
@@ -52,12 +52,13 @@ entry.new = function(ctx, source, completion_item)
end end
---Make offset value ---Make offset value
---@return number ---@return integer
entry.get_offset = function(self) entry.get_offset = function(self)
return self.cache:ensure({ 'get_offset', self.resolved_completion_item and 1 or 0 }, function() return self.cache:ensure({ 'get_offset', self.resolved_completion_item and 1 or 0 }, function()
local offset = self.source_offset local offset = self.source_offset
if misc.safe(self:get_completion_item().textEdit) then if misc.safe(self:get_completion_item().textEdit) then
local range = misc.safe(self:get_completion_item().textEdit.insert) or misc.safe(self:get_completion_item().textEdit.range) local range = misc.safe(self:get_completion_item().textEdit.insert) or
misc.safe(self:get_completion_item().textEdit.range)
if range then if range then
local c = misc.to_vimindex(self.context.cursor_line, range.start.character) local c = misc.to_vimindex(self.context.cursor_line, range.start.character)
for idx = c, self.source_offset do for idx = c, self.source_offset do
@@ -130,17 +131,20 @@ entry.get_word = function(self)
end end
---Get overwrite information ---Get overwrite information
---@return number, number ---@return integer, integer
entry.get_overwrite = function(self) entry.get_overwrite = function(self)
return self.cache:ensure({ 'get_overwrite', self.resolved_completion_item and 1 or 0 }, function() return self.cache:ensure({ 'get_overwrite', self.resolved_completion_item and 1 or 0 }, function()
if misc.safe(self:get_completion_item().textEdit) then if misc.safe(self:get_completion_item().textEdit) then
local r = misc.safe(self:get_completion_item().textEdit.insert) or misc.safe(self:get_completion_item().textEdit.range) local r = misc.safe(self:get_completion_item().textEdit.insert) or
misc.safe(self:get_completion_item().textEdit.range)
if r then
local s = misc.to_vimindex(self.context.cursor_line, r.start.character) local s = misc.to_vimindex(self.context.cursor_line, r.start.character)
local e = misc.to_vimindex(self.context.cursor_line, r['end'].character) local e = misc.to_vimindex(self.context.cursor_line, r['end'].character)
local before = self.context.cursor.col - s local before = self.context.cursor.col - s
local after = e - self.context.cursor.col local after = e - self.context.cursor.col
return { before, after } return { before, after }
end end
end
return { 0, 0 } return { 0, 0 }
end) end)
end end
@@ -184,13 +188,14 @@ end
---Return the item is deprecated or not. ---Return the item is deprecated or not.
---@return boolean ---@return boolean
entry.is_deprecated = function(self) entry.is_deprecated = function(self)
return self:get_completion_item().deprecated or vim.tbl_contains(self:get_completion_item().tags or {}, types.lsp.CompletionItemTag.Deprecated) return self:get_completion_item().deprecated or
vim.tbl_contains(self:get_completion_item().tags or {}, types.lsp.CompletionItemTag.Deprecated)
end end
---Return view information. ---Return view information.
---@param suggest_offset number ---@param suggest_offset integer
---@param entries_buf number The buffer this entry will be rendered into. ---@param entries_buf integer The buffer this entry will be rendered into.
---@return { abbr: { text: string, bytes: number, width: number, hl_group: string }, kind: { text: string, bytes: number, width: number, hl_group: string }, menu: { text: string, bytes: number, width: number, hl_group: string } } ---@return { abbr: { text: string, bytes: integer, width: integer, hl_group: string }, kind: { text: string, bytes: integer, width: integer, hl_group: string }, menu: { text: string, bytes: integer, width: integer, hl_group: string } }
entry.get_view = function(self, suggest_offset, entries_buf) entry.get_view = function(self, suggest_offset, entries_buf)
local item = self:get_vim_item(suggest_offset) local item = self:get_vim_item(suggest_offset)
return self.cache:ensure({ 'get_view', self.resolved_completion_item and 1 or 0, entries_buf }, function() return self.cache:ensure({ 'get_view', self.resolved_completion_item and 1 or 0, entries_buf }, function()
@@ -208,7 +213,8 @@ entry.get_view = function(self, suggest_offset, entries_buf)
view.kind.text = item.kind or '' view.kind.text = item.kind or ''
view.kind.bytes = #view.kind.text view.kind.bytes = #view.kind.text
view.kind.width = vim.fn.strdisplaywidth(view.kind.text) view.kind.width = vim.fn.strdisplaywidth(view.kind.text)
view.kind.hl_group = item.kind_hl_group or ('CmpItemKind' .. (types.lsp.CompletionItemKind[self:get_kind()] or '')) view.kind.hl_group = item.kind_hl_group or ('CmpItemKind' .. (types.lsp.CompletionItemKind[self:get_kind()] or '')
)
view.menu = {} view.menu = {}
view.menu.text = item.menu or '' view.menu.text = item.menu or ''
view.menu.bytes = #view.menu.text view.menu.bytes = #view.menu.text
@@ -221,7 +227,7 @@ entry.get_view = function(self, suggest_offset, entries_buf)
end end
---Make vim.CompletedItem ---Make vim.CompletedItem
---@param suggest_offset number ---@param suggest_offset integer
---@return vim.CompletedItem ---@return vim.CompletedItem
entry.get_vim_item = function(self, suggest_offset) entry.get_vim_item = function(self, suggest_offset)
return self.cache:ensure({ 'get_vim_item', suggest_offset, self.resolved_completion_item and 1 or 0 }, function() return self.cache:ensure({ 'get_vim_item', suggest_offset, self.resolved_completion_item and 1 or 0 }, function()
@@ -313,7 +319,8 @@ entry.get_insert_range = function(self)
insert_range = { insert_range = {
start = { start = {
line = self.context.cursor.row - 1, line = self.context.cursor.row - 1,
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_insert_range.start.character), character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()),
self.source_insert_range.start.character),
}, },
['end'] = self.source_insert_range['end'], ['end'] = self.source_insert_range['end'],
} }
@@ -332,7 +339,8 @@ entry.get_replace_range = function(self)
replace_range = { replace_range = {
start = { start = {
line = self.source_replace_range.start.line, line = self.source_replace_range.start.line,
character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()), self.source_replace_range.start.character), character = math.min(misc.to_utfindex(self.context.cursor_line, self:get_offset()),
self.source_replace_range.start.character),
}, },
['end'] = self.source_replace_range['end'], ['end'] = self.source_replace_range['end'],
} }
@@ -344,7 +352,7 @@ end
---Match line. ---Match line.
---@param input string ---@param input string
---@param matching_config cmp.MatchingConfig ---@param matching_config cmp.MatchingConfig
---@return { score: number, matches: table[] } ---@return { score: integer, matches: table[] }
entry.match = function(self, input, matching_config) entry.match = function(self, input, matching_config)
return self.match_cache:ensure({ return self.match_cache:ensure({
input, input,
@@ -372,7 +380,7 @@ entry.match = function(self, input, matching_config)
local diff = self.source_offset - self:get_offset() local diff = self.source_offset - self:get_offset()
if diff > 0 then if diff > 0 then
local prefix = string.sub(self.context.cursor_line, self:get_offset(), self:get_offset() + diff) local prefix = string.sub(self.context.cursor_line, self:get_offset(), self:get_offset() + diff)
local accept = false local accept = nil
accept = accept or string.match(prefix, '^[^%a]+$') accept = accept or string.match(prefix, '^[^%a]+$')
accept = accept or string.find(self:get_completion_item().textEdit.newText, prefix, 1, true) accept = accept or string.find(self:get_completion_item().textEdit.newText, prefix, 1, true)
if accept then if accept then
@@ -425,13 +433,23 @@ entry.get_documentation = function(self)
}) })
end end
if type(item.documentation) == 'string' and item.documentation ~= '' then local documentation = item.documentation
if type(documentation) == 'string' and documentation ~= '' then
local value = str.trim(documentation)
if value ~= '' then
table.insert(documents, { table.insert(documents, {
kind = types.lsp.MarkupKind.PlainText, kind = types.lsp.MarkupKind.PlainText,
value = str.trim(item.documentation), value = value,
}) })
elseif type(item.documentation) == 'table' and item.documentation.value ~= '' then end
table.insert(documents, item.documentation) elseif type(documentation) == 'table' and documentation.value ~= '' then
local value = str.trim(documentation.value)
if value ~= '' then
table.insert(documents, {
kind = documentation.kind,
value = value,
})
end
end end
return vim.lsp.util.convert_input_to_markdown_lines(documents) return vim.lsp.util.convert_input_to_markdown_lines(documents)

View File

@@ -50,7 +50,7 @@ end
---Register completion sources ---Register completion sources
---@param name string ---@param name string
---@param s cmp.Source ---@param s cmp.Source
---@return number ---@return integer
cmp.register_source = function(name, s) cmp.register_source = function(name, s)
local src = source.new(name, s) local src = source.new(name, s)
cmp.core:register_source(src) cmp.core:register_source(src)
@@ -58,7 +58,7 @@ cmp.register_source = function(name, s)
end end
---Unregister completion source ---Unregister completion source
---@param id number ---@param id integer
cmp.unregister_source = function(id) cmp.unregister_source = function(id)
cmp.core:unregister_source(id) cmp.core:unregister_source(id)
end end

View File

@@ -79,7 +79,7 @@ end
---@param input string ---@param input string
---@param word string ---@param word string
---@param option { synonyms: string[], disallow_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean } ---@param option { synonyms: string[], disallow_fuzzy_matching: boolean, disallow_partial_matching: boolean, disallow_prefix_unmatching: boolean }
---@return number ---@return integer
matcher.match = function(input, word, option) matcher.match = function(input, word, option)
option = option or {} option = option or {}

View File

@@ -10,16 +10,16 @@ local pattern = require('cmp.utils.pattern')
local char = require('cmp.utils.char') local char = require('cmp.utils.char')
---@class cmp.Source ---@class cmp.Source
---@field public id number ---@field public id integer
---@field public name string ---@field public name string
---@field public source any ---@field public source any
---@field public cache cmp.Cache ---@field public cache cmp.Cache
---@field public revision number ---@field public revision integer
---@field public incomplete boolean ---@field public incomplete boolean
---@field public is_triggered_by_symbol boolean ---@field public is_triggered_by_symbol boolean
---@field public entries cmp.Entry[] ---@field public entries cmp.Entry[]
---@field public offset number ---@field public offset integer
---@field public request_offset number ---@field public request_offset integer
---@field public context cmp.Context ---@field public context cmp.Context
---@field public completion_context lsp.CompletionContext|nil ---@field public completion_context lsp.CompletionContext|nil
---@field public status cmp.SourceStatus ---@field public status cmp.SourceStatus
@@ -132,10 +132,10 @@ source.get_entries = function(self, ctx)
end end
---Get default insert range ---Get default insert range
---@return lsp.Range|nil ---@return lsp.Range
source.get_default_insert_range = function(self) source.get_default_insert_range = function(self)
if not self.context then if not self.context then
return nil error('context is not initialized yet.')
end end
return self.cache:ensure({ 'get_default_insert_range', self.revision }, function() return self.cache:ensure({ 'get_default_insert_range', self.revision }, function()
@@ -153,10 +153,10 @@ source.get_default_insert_range = function(self)
end end
---Get default replace range ---Get default replace range
---@return lsp.Range|nil ---@return lsp.Range
source.get_default_replace_range = function(self) source.get_default_replace_range = function(self)
if not self.context then if not self.context then
return nil error('context is not initialized yet.')
end end
return self.cache:ensure({ 'get_default_replace_range', self.revision }, function() return self.cache:ensure({ 'get_default_replace_range', self.revision }, function()
@@ -223,7 +223,7 @@ source.get_keyword_pattern = function(self)
end end
---Get keyword_length ---Get keyword_length
---@return number ---@return integer
source.get_keyword_length = function(self) source.get_keyword_length = function(self)
local c = self:get_source_config() local c = self:get_source_config()
if c.keyword_length then if c.keyword_length then

View File

@@ -51,7 +51,7 @@ cmp.ItemField = {
---@class cmp.SnippetExpansionParams ---@class cmp.SnippetExpansionParams
---@field public body string ---@field public body string
---@field public insert_text_mode number ---@field public insert_text_mode integer
---@class cmp.CompleteParams ---@class cmp.CompleteParams
---@field public reason? cmp.ContextReason ---@field public reason? cmp.ContextReason
@@ -67,7 +67,7 @@ cmp.ItemField = {
---@class cmp.SourceApiParams: cmp.SourceConfig ---@class cmp.SourceApiParams: cmp.SourceConfig
---@class cmp.SourceCompletionApiParams : cmp.SourceConfig ---@class cmp.SourceCompletionApiParams : cmp.SourceConfig
---@field public offset number ---@field public offset integer
---@field public context cmp.Context ---@field public context cmp.Context
---@field public completion_context lsp.CompletionContext ---@field public completion_context lsp.CompletionContext
@@ -78,7 +78,7 @@ cmp.ItemField = {
---@field public s nil|function(fallback: function): void ---@field public s nil|function(fallback: function): void
---@class cmp.ConfigSchema ---@class cmp.ConfigSchema
---@field private revision number ---@field private revision integer
---@field public enabled fun():boolean|boolean ---@field public enabled fun():boolean|boolean
---@field public performance cmp.PerformanceConfig ---@field public performance cmp.PerformanceConfig
---@field public preselect cmp.PreselectMode ---@field public preselect cmp.PreselectMode
@@ -95,8 +95,8 @@ cmp.ItemField = {
---@field public experimental cmp.ExperimentalConfig ---@field public experimental cmp.ExperimentalConfig
---@class cmp.PerformanceConfig ---@class cmp.PerformanceConfig
---@field public debounce number ---@field public debounce integer
---@field public throttle number ---@field public throttle integer
---@class cmp.WindowConfig ---@class cmp.WindowConfig
---@field completion cmp.WindowConfig ---@field completion cmp.WindowConfig
@@ -106,15 +106,15 @@ cmp.ItemField = {
---@field public autocomplete cmp.TriggerEvent[] ---@field public autocomplete cmp.TriggerEvent[]
---@field public completeopt string ---@field public completeopt string
---@field public get_trigger_characters fun(trigger_characters: string[]): string[] ---@field public get_trigger_characters fun(trigger_characters: string[]): string[]
---@field public keyword_length number ---@field public keyword_length integer
---@field public keyword_pattern string ---@field public keyword_pattern string
---@class cmp.WindowConfig ---@class cmp.WindowConfig
---@field public border string|string[] ---@field public border string|string[]
---@field public winhighlight string ---@field public winhighlight string
---@field public zindex number|nil ---@field public zindex integer|nil
---@field public max_width number|nil ---@field public max_width integer|nil
---@field public max_height number|nil ---@field public max_height integer|nil
---@class cmp.ConfirmationConfig ---@class cmp.ConfirmationConfig
---@field public default_behavior cmp.ConfirmBehavior ---@field public default_behavior cmp.ConfirmBehavior
@@ -126,7 +126,7 @@ cmp.ItemField = {
---@field public disallow_prefix_unmatching boolean ---@field public disallow_prefix_unmatching boolean
---@class cmp.SortingConfig ---@class cmp.SortingConfig
---@field public priority_weight number ---@field public priority_weight integer
---@field public comparators function[] ---@field public comparators function[]
---@class cmp.FormattingConfig ---@class cmp.FormattingConfig
@@ -145,12 +145,12 @@ cmp.ItemField = {
---@class cmp.SourceConfig ---@class cmp.SourceConfig
---@field public name string ---@field public name string
---@field public option table|nil ---@field public option table|nil
---@field public priority number|nil ---@field public priority integer|nil
---@field public trigger_characters string[]|nil ---@field public trigger_characters string[]|nil
---@field public keyword_pattern string|nil ---@field public keyword_pattern string|nil
---@field public keyword_length number|nil ---@field public keyword_length integer|nil
---@field public max_item_count number|nil ---@field public max_item_count integer|nil
---@field public group_index number|nil ---@field public group_index integer|nil
---@class cmp.ViewConfig ---@class cmp.ViewConfig
---@field public entries cmp.EntriesConfig ---@field public entries cmp.EntriesConfig

View File

@@ -6,7 +6,7 @@ local lsp = {}
lsp.Position = { lsp.Position = {
---Convert lsp.Position to vim.Position ---Convert lsp.Position to vim.Position
---@param buf number|string ---@param buf integer|string
---@param position lsp.Position ---@param position lsp.Position
---@return vim.Position ---@return vim.Position
to_vim = function(buf, position) to_vim = function(buf, position)
@@ -26,7 +26,7 @@ lsp.Position = {
} }
end, end,
---Convert vim.Position to lsp.Position ---Convert vim.Position to lsp.Position
---@param buf number|string ---@param buf integer|string
---@param position vim.Position ---@param position vim.Position
---@return lsp.Position ---@return lsp.Position
to_lsp = function(buf, position) to_lsp = function(buf, position)
@@ -49,7 +49,7 @@ lsp.Position = {
lsp.Range = { lsp.Range = {
---Convert lsp.Range to vim.Range ---Convert lsp.Range to vim.Range
---@param buf number|string ---@param buf integer|string
---@param range lsp.Range ---@param range lsp.Range
---@return vim.Range ---@return vim.Range
to_vim = function(buf, range) to_vim = function(buf, range)
@@ -60,7 +60,7 @@ lsp.Range = {
end, end,
---Convert vim.Range to lsp.Range ---Convert vim.Range to lsp.Range
---@param buf number|string ---@param buf integer|string
---@param range vim.Range ---@param range vim.Range
---@return lsp.Range ---@return lsp.Range
to_lsp = function(buf, range) to_lsp = function(buf, range)
@@ -145,8 +145,8 @@ lsp.CompletionItemKind = vim.tbl_add_reverse_lookup(lsp.CompletionItemKind)
---@field public value string ---@field public value string
---@class lsp.Position ---@class lsp.Position
---@field public line number ---@field public line integer
---@field public character number ---@field public character integer
---@class lsp.Range ---@class lsp.Range
---@field public start lsp.Position ---@field public start lsp.Position
@@ -161,9 +161,14 @@ lsp.CompletionItemKind = vim.tbl_add_reverse_lookup(lsp.CompletionItemKind)
---@field public range lsp.Range|nil ---@field public range lsp.Range|nil
---@field public newText string ---@field public newText string
---@class lsp.InsertReplaceTextEdit ---@alias lsp.InsertReplaceTextEdit lsp.internal.InsertTextEdit|lsp.internal.ReplaceTextEdit
---@field public insert lsp.Range|nil
---@field public replace lsp.Range|nil ---@class lsp.internal.InsertTextEdit
---@field public insert lsp.Range
---@field public newText string
---@class lsp.internal.ReplaceTextEdit
---@field public insert lsp.Range
---@field public newText string ---@field public newText string
---@class lsp.CompletionItemLabelDetails ---@class lsp.CompletionItemLabelDetails

View File

@@ -12,8 +12,8 @@
---@field public menu_hl_group string|nil ---@field public menu_hl_group string|nil
---@class vim.Position ---@class vim.Position
---@field public row number ---@field public row integer
---@field public col number ---@field public col integer
---@class vim.Range ---@class vim.Range
---@field public start vim.Position ---@field public start vim.Position

View File

@@ -2,13 +2,13 @@ local async = {}
---@class cmp.AsyncThrottle ---@class cmp.AsyncThrottle
---@field public running boolean ---@field public running boolean
---@field public timeout number ---@field public timeout integer
---@field public sync function(self: cmp.AsyncThrottle, timeout: number|nil) ---@field public sync function(self: cmp.AsyncThrottle, timeout: integer|nil)
---@field public stop function ---@field public stop function
---@field public __call function ---@field public __call function
---@param fn function ---@param fn function
---@param timeout number ---@param timeout integer
---@return cmp.AsyncThrottle ---@return cmp.AsyncThrottle
async.throttle = function(fn, timeout) async.throttle = function(fn, timeout)
local time = nil local time = nil
@@ -60,7 +60,7 @@ end
---Timeout callback function ---Timeout callback function
---@param fn function ---@param fn function
---@param timeout number ---@param timeout integer
---@return function ---@return function
async.timeout = function(fn, timeout) async.timeout = function(fn, timeout)
local timer local timer

View File

@@ -12,7 +12,7 @@ end
---@param list any[] ---@param list any[]
---@param item any ---@param item any
---@param func fun(a: any, b: any): 1|-1|0 ---@param func fun(a: any, b: any): 1|-1|0
---@return number ---@return integer
binary.search = function(list, item, func) binary.search = function(list, item, func)
local s = 1 local s = 1
local e = #list local e = #list

View File

@@ -2,7 +2,7 @@ local buffer = {}
buffer.cache = {} buffer.cache = {}
---@return number buf ---@return integer buf
buffer.get = function(name) buffer.get = function(name)
local buf = buffer.cache[name] local buf = buffer.cache[name]
if buf and vim.api.nvim_buf_is_valid(buf) then if buf and vim.api.nvim_buf_is_valid(buf) then
@@ -12,7 +12,7 @@ buffer.get = function(name)
end end
end end
---@return number buf ---@return integer buf
---@return boolean created_new ---@return boolean created_new
buffer.ensure = function(name) buffer.ensure = function(name)
local created_new = false local created_new = false

View File

@@ -9,7 +9,7 @@ cache.new = function()
end end
---Get cache value ---Get cache value
---@param key string ---@param key string|string[]
---@return any|nil ---@return any|nil
cache.get = function(self, key) cache.get = function(self, key)
key = self:key(key) key = self:key(key)
@@ -20,7 +20,7 @@ cache.get = function(self, key)
end end
---Set cache value explicitly ---Set cache value explicitly
---@param key string ---@param key string|string[]
---@vararg any ---@vararg any
cache.set = function(self, key, value) cache.set = function(self, key, value)
key = self:key(key) key = self:key(key)
@@ -28,8 +28,10 @@ cache.set = function(self, key, value)
end end
---Ensure value by callback ---Ensure value by callback
---@param key string ---@generic T
---@param callback fun(): any ---@param key string|string[]
---@param callback fun(): T
---@return T
cache.ensure = function(self, key, callback) cache.ensure = function(self, key, callback)
local value = self:get(key) local value = self:get(key)
if value == nil then if value == nil then
@@ -46,7 +48,7 @@ cache.clear = function(self)
end end
---Create key ---Create key
---@param key string|table ---@param key string|string[]
---@return string ---@return string
cache.key = function(_, key) cache.key = function(_, key)
if type(key) == 'table' then if type(key) == 'table' then

View File

@@ -22,50 +22,50 @@ end)
local char = {} local char = {}
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_upper = function(byte) char.is_upper = function(byte)
return ALPHA[byte] return ALPHA[byte]
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_alpha = function(byte) char.is_alpha = function(byte)
return alpha[byte] or ALPHA[byte] return alpha[byte] or ALPHA[byte]
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_digit = function(byte) char.is_digit = function(byte)
return digit[byte] return digit[byte]
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_white = function(byte) char.is_white = function(byte)
return white[byte] return white[byte]
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_symbol = function(byte) char.is_symbol = function(byte)
return not (char.is_alnum(byte) or char.is_white(byte)) return not (char.is_alnum(byte) or char.is_white(byte))
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_printable = function(byte) char.is_printable = function(byte)
return string.match(string.char(byte), '^%c$') == nil return string.match(string.char(byte), '^%c$') == nil
end end
---@param byte number ---@param byte integer
---@return boolean ---@return boolean
char.is_alnum = function(byte) char.is_alnum = function(byte)
return char.is_alpha(byte) or char.is_digit(byte) return char.is_alpha(byte) or char.is_digit(byte)
end end
---@param text string ---@param text string
---@param index number ---@param index integer
---@return boolean ---@return boolean
char.is_semantic_index = function(text, index) char.is_semantic_index = function(text, index)
if index <= 1 then if index <= 1 then
@@ -91,8 +91,8 @@ char.is_semantic_index = function(text, index)
end end
---@param text string ---@param text string
---@param current_index number ---@param current_index integer
---@return boolean ---@return integer
char.get_next_semantic_index = function(text, current_index) char.get_next_semantic_index = function(text, current_index)
for i = current_index + 1, #text do for i = current_index + 1, #text do
if char.is_semantic_index(text, i) then if char.is_semantic_index(text, i) then
@@ -103,8 +103,8 @@ char.get_next_semantic_index = function(text, current_index)
end end
---Ignore case match ---Ignore case match
---@param byte1 number ---@param byte1 integer
---@param byte2 number ---@param byte2 integer
---@return boolean ---@return boolean
char.match = function(byte1, byte2) char.match = function(byte1, byte2)
if not char.is_alpha(byte1) or not char.is_alpha(byte2) then if not char.is_alpha(byte1) or not char.is_alpha(byte2) then

View File

@@ -68,7 +68,7 @@ keymap.undojoin = function()
end end
---Create backspace keys. ---Create backspace keys.
---@param count number ---@param count integer
---@return string ---@return string
keymap.backspace = function(count) keymap.backspace = function(count)
if type(count) == 'string' then if type(count) == 'string' then

View File

@@ -32,7 +32,7 @@ end
---Repeat values ---Repeat values
---@generic T ---@generic T
---@param str_or_tbl T ---@param str_or_tbl T
---@param count number ---@param count integer
---@return T ---@return T
misc.rep = function(str_or_tbl, count) misc.rep = function(str_or_tbl, count)
if type(str_or_tbl) == 'string' then if type(str_or_tbl) == 'string' then
@@ -124,8 +124,9 @@ misc.id = setmetatable({
}) })
---Check the value is nil or not. ---Check the value is nil or not.
---@param v boolean ---@generic T|nil|vim.NIL
---@return boolean ---@param v T
---@return T|nil
misc.safe = function(v) misc.safe = function(v)
if v == nil or v == vim.NIL then if v == nil or v == vim.NIL then
return nil return nil
@@ -184,8 +185,8 @@ end
---Safe version of vim.str_utfindex ---Safe version of vim.str_utfindex
---@param text string ---@param text string
---@param vimindex number|nil ---@param vimindex integer|nil
---@return number ---@return integer
misc.to_utfindex = function(text, vimindex) misc.to_utfindex = function(text, vimindex)
vimindex = vimindex or #text + 1 vimindex = vimindex or #text + 1
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text))) return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
@@ -193,8 +194,8 @@ end
---Safe version of vim.str_byteindex ---Safe version of vim.str_byteindex
---@param text string ---@param text string
---@param utfindex number ---@param utfindex integer
---@return number ---@return integer
misc.to_vimindex = function(text, utfindex) misc.to_vimindex = function(text, utfindex)
utfindex = utfindex or #text utfindex = utfindex or #text
for i = utfindex, 1, -1 do for i = utfindex, 1, -1 do

View File

@@ -99,8 +99,8 @@ end
---get_word ---get_word
---@param text string ---@param text string
---@param stop_char number ---@param stop_char integer
---@param min_length number ---@param min_length integer
---@return string ---@return string
str.get_word = function(text, stop_char, min_length) str.get_word = function(text, stop_char, min_length)
min_length = min_length or 0 min_length = min_length or 0

View File

@@ -5,18 +5,18 @@ local api = require('cmp.utils.api')
---@class cmp.WindowStyle ---@class cmp.WindowStyle
---@field public relative string ---@field public relative string
---@field public row number ---@field public row integer
---@field public col number ---@field public col integer
---@field public width number ---@field public width integer
---@field public height number ---@field public height integer
---@field public border string|string[]|nil ---@field public border string|string[]|nil
---@field public zindex number|nil ---@field public zindex integer|nil
---@class cmp.Window ---@class cmp.Window
---@field public name string ---@field public name string
---@field public win number|nil ---@field public win integer|nil
---@field public thumb_win number|nil ---@field public thumb_win integer|nil
---@field public sbar_win number|nil ---@field public sbar_win integer|nil
---@field public style cmp.WindowStyle ---@field public style cmp.WindowStyle
---@field public opt table<string, any> ---@field public opt table<string, any>
---@field public buffer_opt table<string, any> ---@field public buffer_opt table<string, any>
@@ -91,7 +91,7 @@ window.set_style = function(self, style)
end end
---Return buffer id. ---Return buffer id.
---@return number ---@return integer
window.get_buffer = function(self) window.get_buffer = function(self)
local buf, created_new = buffer.ensure(self.name) local buf, created_new = buffer.ensure(self.name)
if created_new then if created_new then
@@ -240,7 +240,7 @@ window.info = function(self)
end end
---Return border information. ---Return border information.
---@return { top: number, left: number, right: number, bottom: number, vert: number, horiz: number, visible: boolean } ---@return { top: integer, left: integer, right: integer, bottom: integer, vert: integer, horiz: integer, visible: boolean }
window.get_border_info = function(self) window.get_border_info = function(self)
local border = self.style.border local border = self.style.border
if not border or border == 'none' then if not border or border == 'none' then
@@ -296,7 +296,7 @@ end
---Get scroll height. ---Get scroll height.
---NOTE: The result of vim.fn.strdisplaywidth depends on the buffer it was called in (see comment in cmp.Entry.get_view). ---NOTE: The result of vim.fn.strdisplaywidth depends on the buffer it was called in (see comment in cmp.Entry.get_view).
---@return number ---@return integer
window.get_content_height = function(self) window.get_content_height = function(self)
if not self:option('wrap') then if not self:option('wrap') then
return vim.api.nvim_buf_line_count(self:get_buffer()) return vim.api.nvim_buf_line_count(self:get_buffer())

View File

@@ -153,7 +153,7 @@ view.visible = function(self)
end end
---Scroll documentation window if possible. ---Scroll documentation window if possible.
---@param delta number ---@param delta integer
view.scroll_docs = function(self, delta) view.scroll_docs = function(self, delta)
self.docs_view:scroll(delta) self.docs_view:scroll(delta)
end end

View File

@@ -12,7 +12,7 @@ local DEFAULT_HEIGHT = 10 -- @see https://github.com/vim/vim/blob/master/src/pop
---@class cmp.CustomEntriesView ---@class cmp.CustomEntriesView
---@field private entries_win cmp.Window ---@field private entries_win cmp.Window
---@field private offset number ---@field private offset integer
---@field private active boolean ---@field private active boolean
---@field private entries cmp.Entry[] ---@field private entries cmp.Entry[]
---@field private column_width any ---@field private column_width any

View File

@@ -7,10 +7,10 @@ local config = require('cmp.config')
local api = require('cmp.utils.api') local api = require('cmp.utils.api')
---@class cmp.NativeEntriesView ---@class cmp.NativeEntriesView
---@field private offset number ---@field private offset integer
---@field private items vim.CompletedItem ---@field private items vim.CompletedItem
---@field private entries cmp.Entry[] ---@field private entries cmp.Entry[]
---@field private preselect_index number ---@field private preselect_index integer
---@field public event cmp.Event ---@field public event cmp.Event
local native_entries_view = {} local native_entries_view = {}

View File

@@ -9,7 +9,7 @@ local misc = require('cmp.utils.misc')
local api = require('cmp.utils.api') local api = require('cmp.utils.api')
---@class cmp.CustomEntriesView ---@class cmp.CustomEntriesView
---@field private offset number ---@field private offset integer
---@field private entries_win cmp.Window ---@field private entries_win cmp.Window
---@field private active boolean ---@field private active boolean
---@field private entries cmp.Entry[] ---@field private entries cmp.Entry[]

View File

@@ -2,7 +2,7 @@ local misc = require('cmp.utils.misc')
local vim_source = {} local vim_source = {}
---@param id number ---@param id integer
---@param args any[] ---@param args any[]
vim_source.on_callback = function(id, args) vim_source.on_callback = function(id, args)
if vim_source.to_callback.callbacks[id] then if vim_source.to_callback.callbacks[id] then
@@ -11,7 +11,7 @@ vim_source.on_callback = function(id, args)
end end
---@param callback function ---@param callback function
---@return number ---@return integer
vim_source.to_callback = setmetatable({ vim_source.to_callback = setmetatable({
callbacks = {}, callbacks = {},
}, { }, {
@@ -36,7 +36,7 @@ vim_source.to_args = function(args)
return args return args
end end
---@param bridge_id number ---@param bridge_id integer
---@param methods string[] ---@param methods string[]
vim_source.new = function(bridge_id, methods) vim_source.new = function(bridge_id, methods)
local self = {} local self = {}