Skip to content

Commit bc87fa6

Browse files
committed
feat: add type annotations for plenary.strings
1 parent 5037349 commit bc87fa6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

lua/plenary/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
---@field log PlenaryLog
1111
---@field path PlenaryPath
1212
---@field scandir PlenaryScandir
13+
---@field strings PlenaryStrings
1314
---@field tbl PlenaryTbl
1415
local plenary = setmetatable({}, {
1516
__index = function(t, k)

lua/plenary/strings.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
local path = require("plenary.path").path
22

3+
---@class PlenaryStrings
34
local M = {}
45

6+
---@type fun(str: string, col?: integer): integer
57
M.strdisplaywidth = (function()
8+
---@param str string
9+
---@param col? integer
10+
---@return integer
611
local fallback = function(str, col)
712
str = tostring(str)
813
if vim.in_fast_event() then
@@ -18,6 +23,9 @@ M.strdisplaywidth = (function()
1823
int linetabsize_col(int startcol, char_u *s);
1924
]]
2025

26+
---@param str string
27+
---@param col? integer
28+
---@return integer
2129
local ffi_func = function(str, col)
2230
str = tostring(str)
2331
local startcol = col or 0
@@ -37,7 +45,13 @@ M.strdisplaywidth = (function()
3745
end
3846
end)()
3947

48+
--- TODO: deal with the 4th param: `skipcc`
49+
---@type fun(str: string, start: integer, len?: integer): string
4050
M.strcharpart = (function()
51+
---@param str string
52+
---@param nchar integer
53+
---@param charlen? integer
54+
---@return string
4155
local fallback = function(str, nchar, charlen)
4256
if vim.in_fast_event() then
4357
return str:sub(nchar + 1, charlen)
@@ -52,6 +66,8 @@ M.strcharpart = (function()
5266
int utf_ptr2len(const char_u *const p);
5367
]]
5468

69+
---@param str string
70+
---@return integer
5571
local function utf_ptr2len(str)
5672
local c_str = ffi.new("char[?]", #str + 1)
5773
ffi.copy(c_str, str)
@@ -63,6 +79,10 @@ M.strcharpart = (function()
6379
return fallback
6480
end
6581

82+
---@param str string
83+
---@param nchar integer
84+
---@param charlen? integer
85+
---@return string
6686
return function(str, nchar, charlen)
6787
local nbyte = 0
6888
if nchar > 0 then
@@ -108,6 +128,11 @@ M.strcharpart = (function()
108128
end
109129
end)()
110130

131+
---@param str string
132+
---@param len integer
133+
---@param dots string
134+
---@param direction -1|1
135+
---@return string
111136
local truncate = function(str, len, dots, direction)
112137
if M.strdisplaywidth(str) <= len then
113138
return str
@@ -116,6 +141,10 @@ local truncate = function(str, len, dots, direction)
116141
local current = 0
117142
local result = ""
118143
local len_of_dots = M.strdisplaywidth(dots)
144+
---@param a string
145+
---@param b string
146+
---@param dir -1|1
147+
---@return string
119148
local concat = function(a, b, dir)
120149
if dir > 0 then
121150
return a .. b
@@ -136,6 +165,11 @@ local truncate = function(str, len, dots, direction)
136165
return result
137166
end
138167

168+
---@param str string
169+
---@param len integer
170+
---@param dots? string default: `""`
171+
---@param direction? -1|1 default: `1`
172+
---@return string
139173
M.truncate = function(str, len, dots, direction)
140174
str = tostring(str) -- We need to make sure its an actually a string and not a number
141175
dots = dots or ""
@@ -154,18 +188,28 @@ M.truncate = function(str, len, dots, direction)
154188
end
155189
end
156190

191+
---@param string string
192+
---@param width integer
193+
---@param right_justify? boolean
194+
---@return string
157195
M.align_str = function(string, width, right_justify)
158196
local str_len = M.strdisplaywidth(string)
159197
return right_justify and string.rep(" ", width - str_len) .. string or string .. string.rep(" ", width - str_len)
160198
end
161199

200+
---@param str string
201+
---@param leave_indent? integer
202+
---@return string
162203
M.dedent = function(str, leave_indent)
163204
-- Check each line and detect the minimum indent.
205+
---@type integer
164206
local indent
207+
---@type { line: string, chars?: integer, width?: integer }[]
165208
local info = {}
166209
for line in str:gmatch "[^\n]*\n?" do
167210
-- It matches '' for the last line.
168211
if line ~= "" then
212+
---@type integer, integer
169213
local chars, width
170214
local line_indent = line:match "^[ \t]+"
171215
if line_indent then
@@ -184,6 +228,7 @@ M.dedent = function(str, leave_indent)
184228

185229
-- Build up the result
186230
leave_indent = leave_indent or 0
231+
---@type string[]
187232
local result = {}
188233
for _, i in ipairs(info) do
189234
local line

0 commit comments

Comments
 (0)