1
1
local strings = require " plenary.strings"
2
2
3
+ --- @class PlenaryWindowBorder
4
+ --- @field bufnr integer
5
+ --- @field content_win_id integer
6
+ --- @field content_win_options vim.api.keyset.win_config
7
+ --- @field contents string[]
8
+ --- @field win_id integer
9
+ --- @field private _border_win_options PlenaryWindowBorderBorderOptions
3
10
local Border = {}
4
11
12
+ --- @class PlenaryWindowBorderBorderOptions
13
+ --- @field border_thickness PlenaryWindowBorderBorderThickness
14
+ --- @field topleft string
15
+ --- @field topright string
16
+ --- @field top string
17
+ --- @field left string
18
+ --- @field right string
19
+ --- @field botleft string
20
+ --- @field botright string
21
+ --- @field bot string
22
+ --- @field focusable ? boolean
23
+ --- @field highlight ? string
24
+ --- @field title ? string | PlenaryWindowBorderTitles
25
+ --- @field titlehighlight ? string
26
+
27
+ --- @class PlenaryWindowBorderBorderThickness
28
+ --- @field top integer
29
+ --- @field right integer
30
+ --- @field bot integer
31
+ --- @field left integer
32
+
33
+ --- @alias PlenaryWindowBorderPos " NW" | " N" | " NE" | " SW" | " S" | " SE"
34
+ --- @alias PlenaryWindowBorderRanges { [1] : integer , [2] : integer , [3] : integer ? } []
35
+ --- @alias PlenaryWindowBorderTitles { text : string , pos : PlenaryWindowBorderPos } []
36
+
5
37
Border .__index = Border
6
38
39
+ --- @type PlenaryWindowBorderBorderThickness
7
40
Border ._default_thickness = {
8
41
top = 1 ,
9
42
right = 1 ,
10
43
bot = 1 ,
11
44
left = 1 ,
12
45
}
13
46
47
+ --- @param title_pos string
48
+ --- @param title_len integer
49
+ --- @param total_width integer
50
+ --- @return integer
14
51
local calc_left_start = function (title_pos , title_len , total_width )
15
52
if string.find (title_pos , " W" ) then
16
53
return 0
@@ -21,6 +58,15 @@ local calc_left_start = function(title_pos, title_len, total_width)
21
58
end
22
59
end
23
60
61
+ --- comment
62
+ --- @param title string
63
+ --- @param pos PlenaryWindowBorderPos
64
+ --- @param width integer
65
+ --- @param left_char string
66
+ --- @param mid_char string
67
+ --- @param right_char string
68
+ --- @return string horizontal_line
69
+ --- @return PlenaryWindowBorderRanges ranges
24
70
local create_horizontal_line = function (title , pos , width , left_char , mid_char , right_char )
25
71
local title_len
26
72
if title == " " then
@@ -46,6 +92,7 @@ local create_horizontal_line = function(title, pos, width, left_char, mid_char,
46
92
string.rep (mid_char , width - title_len - left_start ),
47
93
right_char
48
94
)
95
+ --- @type PlenaryWindowBorderRanges
49
96
local ranges = {}
50
97
if title_len ~= 0 then
51
98
-- Need to calculate again due to multi-byte characters
@@ -55,6 +102,11 @@ local create_horizontal_line = function(title, pos, width, left_char, mid_char,
55
102
return horizontal_line , ranges
56
103
end
57
104
105
+ --- @param content_win_id integer
106
+ --- @param content_win_options vim.api.keyset.win_config
107
+ --- @param border_win_options PlenaryWindowBorderBorderOptions
108
+ --- @return string[] border_lines
109
+ --- @return PlenaryWindowBorderRanges ranges
58
110
function Border ._create_lines (content_win_id , content_win_options , border_win_options )
59
111
local content_pos = vim .api .nvim_win_get_position (content_win_id )
60
112
local content_height = vim .api .nvim_win_get_height (content_win_id )
@@ -71,15 +123,17 @@ function Border._create_lines(content_win_id, content_win_options, border_win_op
71
123
border_win_options .border_thickness .left = left_enabled and 1 or 0
72
124
border_win_options .border_thickness .right = right_enabled and 1 or 0
73
125
126
+ --- @type string[]
74
127
local border_lines = {}
128
+ --- @type PlenaryWindowBorderRanges
75
129
local ranges = {}
76
130
77
131
-- border_win_options.title should have be a list with entries of the
78
132
-- form: { pos = foo, text = bar }.
79
133
-- pos can take values in { "NW", "N", "NE", "SW", "S", "SE" }
80
134
local titles = type (border_win_options .title ) == " string" and { { pos = " N" , text = border_win_options .title } }
81
135
or border_win_options .title
82
- or {}
136
+ or {} --[[ @as PlenaryWindowBorderTitles ]]
83
137
84
138
local topline = nil
85
139
local topleft = (left_enabled and border_win_options .topleft ) or " "
@@ -164,6 +218,9 @@ function Border._create_lines(content_win_id, content_win_options, border_win_op
164
218
return border_lines , ranges
165
219
end
166
220
221
+ --- @param bufnr integer
222
+ --- @param ranges ? PlenaryWindowBorderRanges
223
+ --- @param hl ? string
167
224
local set_title_highlights = function (bufnr , ranges , hl )
168
225
-- Check if both `hl` and `ranges` are provided, and `ranges` is not the empty table.
169
226
if hl and ranges and next (ranges ) then
@@ -173,6 +230,8 @@ local set_title_highlights = function(bufnr, ranges, hl)
173
230
end
174
231
end
175
232
233
+ --- @param new_title string
234
+ --- @param pos string
176
235
function Border :change_title (new_title , pos )
177
236
if self ._border_win_options .title == new_title then
178
237
return
195
254
196
255
-- Updates characters for border lines, and returns nvim_win_config
197
256
-- (generally used in conjunction with `move` or `new`)
257
+ --- @param content_win_options vim.api.keyset.win_config
258
+ --- @param border_win_options PlenaryWindowBorderBorderOptions
259
+ --- @return vim.api.keyset.win_config
198
260
function Border :__align_calc_config (content_win_options , border_win_options )
199
261
border_win_options = vim .tbl_deep_extend (" keep" , border_win_options , {
200
262
border_thickness = Border ._default_thickness ,
239
301
-- Sets the size and position of the given Border.
240
302
-- Can be used to create a new window (with `create_window = true`)
241
303
-- or change an existing one
304
+ --- @param content_win_options vim.api.keyset.win_config
305
+ --- @param border_win_options PlenaryWindowBorderBorderOptions
242
306
function Border :move (content_win_options , border_win_options )
243
307
-- Update lines in border buffer, and get config for border window
244
308
local nvim_win_config = self :__align_calc_config (content_win_options , border_win_options )
@@ -249,6 +313,11 @@ function Border:move(content_win_options, border_win_options)
249
313
set_title_highlights (self .bufnr , self .title_ranges , self ._border_win_options .titlehighlight )
250
314
end
251
315
316
+ --- @param content_bufnr integer
317
+ --- @param content_win_id integer
318
+ --- @param content_win_options vim.api.keyset.win_config
319
+ --- @param border_win_options PlenaryWindowBorderBorderOptions
320
+ --- @return PlenaryWindowBorder
252
321
function Border :new (content_bufnr , content_win_id , content_win_options , border_win_options )
253
322
assert (type (content_win_id ) == " number" , " Must supply a valid win_id. It's possible you forgot to call with ':'" )
254
323
0 commit comments