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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,18 +5,18 @@ local api = require('cmp.utils.api')
---@class cmp.WindowStyle
---@field public relative string
---@field public row number
---@field public col number
---@field public width number
---@field public height number
---@field public row integer
---@field public col integer
---@field public width integer
---@field public height integer
---@field public border string|string[]|nil
---@field public zindex number|nil
---@field public zindex integer|nil
---@class cmp.Window
---@field public name string
---@field public win number|nil
---@field public thumb_win number|nil
---@field public sbar_win number|nil
---@field public win integer|nil
---@field public thumb_win integer|nil
---@field public sbar_win integer|nil
---@field public style cmp.WindowStyle
---@field public opt table<string, any>
---@field public buffer_opt table<string, any>
@@ -91,7 +91,7 @@ window.set_style = function(self, style)
end
---Return buffer id.
---@return number
---@return integer
window.get_buffer = function(self)
local buf, created_new = buffer.ensure(self.name)
if created_new then
@@ -240,7 +240,7 @@ window.info = function(self)
end
---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)
local border = self.style.border
if not border or border == 'none' then
@@ -296,7 +296,7 @@ end
---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).
---@return number
---@return integer
window.get_content_height = function(self)
if not self:option('wrap') then
return vim.api.nvim_buf_line_count(self:get_buffer())