1
1
local path = require (" plenary.path" ).path
2
2
3
+ --- @class PlenaryStrings
3
4
local M = {}
4
5
6
+ --- @type fun ( str : string , col ?: integer ): integer
5
7
M .strdisplaywidth = (function ()
8
+ --- @param str string
9
+ --- @param col ? integer
10
+ --- @return integer
6
11
local fallback = function (str , col )
7
12
str = tostring (str )
8
13
if vim .in_fast_event () then
@@ -18,6 +23,9 @@ M.strdisplaywidth = (function()
18
23
int linetabsize_col (int startcol , char_u * s );
19
24
]]
20
25
26
+ --- @param str string
27
+ --- @param col ? integer
28
+ --- @return integer
21
29
local ffi_func = function (str , col )
22
30
str = tostring (str )
23
31
local startcol = col or 0
@@ -37,7 +45,13 @@ M.strdisplaywidth = (function()
37
45
end
38
46
end )()
39
47
48
+ --- TODO: deal with the 4th param: `skipcc`
49
+ --- @type fun ( str : string , start : integer , len ?: integer ): string
40
50
M .strcharpart = (function ()
51
+ --- @param str string
52
+ --- @param nchar integer
53
+ --- @param charlen ? integer
54
+ --- @return string
41
55
local fallback = function (str , nchar , charlen )
42
56
if vim .in_fast_event () then
43
57
return str :sub (nchar + 1 , charlen )
@@ -52,6 +66,8 @@ M.strcharpart = (function()
52
66
int utf_ptr2len (const char_u * const p );
53
67
]]
54
68
69
+ --- @param str string
70
+ --- @return integer
55
71
local function utf_ptr2len (str )
56
72
local c_str = ffi .new (" char[?]" , # str + 1 )
57
73
ffi .copy (c_str , str )
@@ -63,6 +79,10 @@ M.strcharpart = (function()
63
79
return fallback
64
80
end
65
81
82
+ --- @param str string
83
+ --- @param nchar integer
84
+ --- @param charlen ? integer
85
+ --- @return string
66
86
return function (str , nchar , charlen )
67
87
local nbyte = 0
68
88
if nchar > 0 then
@@ -108,6 +128,11 @@ M.strcharpart = (function()
108
128
end
109
129
end )()
110
130
131
+ --- @param str string
132
+ --- @param len integer
133
+ --- @param dots string
134
+ --- @param direction -1| 1
135
+ --- @return string
111
136
local truncate = function (str , len , dots , direction )
112
137
if M .strdisplaywidth (str ) <= len then
113
138
return str
@@ -116,6 +141,10 @@ local truncate = function(str, len, dots, direction)
116
141
local current = 0
117
142
local result = " "
118
143
local len_of_dots = M .strdisplaywidth (dots )
144
+ --- @param a string
145
+ --- @param b string
146
+ --- @param dir -1| 1
147
+ --- @return string
119
148
local concat = function (a , b , dir )
120
149
if dir > 0 then
121
150
return a .. b
@@ -136,6 +165,11 @@ local truncate = function(str, len, dots, direction)
136
165
return result
137
166
end
138
167
168
+ --- @param str string
169
+ --- @param len integer
170
+ --- @param dots ? string default : ` " …" `
171
+ --- @param direction ? -1| 1 default : ` 1`
172
+ --- @return string
139
173
M .truncate = function (str , len , dots , direction )
140
174
str = tostring (str ) -- We need to make sure its an actually a string and not a number
141
175
dots = dots or " …"
@@ -154,18 +188,28 @@ M.truncate = function(str, len, dots, direction)
154
188
end
155
189
end
156
190
191
+ --- @param string string
192
+ --- @param width integer
193
+ --- @param right_justify ? boolean
194
+ --- @return string
157
195
M .align_str = function (string , width , right_justify )
158
196
local str_len = M .strdisplaywidth (string )
159
197
return right_justify and string.rep (" " , width - str_len ) .. string or string .. string.rep (" " , width - str_len )
160
198
end
161
199
200
+ --- @param str string
201
+ --- @param leave_indent ? integer
202
+ --- @return string
162
203
M .dedent = function (str , leave_indent )
163
204
-- Check each line and detect the minimum indent.
205
+ --- @type integer
164
206
local indent
207
+ --- @type { line : string , chars ?: integer , width ?: integer } []
165
208
local info = {}
166
209
for line in str :gmatch " [^\n ]*\n ?" do
167
210
-- It matches '' for the last line.
168
211
if line ~= " " then
212
+ --- @type integer , integer
169
213
local chars , width
170
214
local line_indent = line :match " ^[ \t ]+"
171
215
if line_indent then
@@ -184,6 +228,7 @@ M.dedent = function(str, leave_indent)
184
228
185
229
-- Build up the result
186
230
leave_indent = leave_indent or 0
231
+ --- @type string[]
187
232
local result = {}
188
233
for _ , i in ipairs (info ) do
189
234
local line
0 commit comments