11local strings = require " plenary.strings"
22
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
310local Border = {}
411
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+
537Border .__index = Border
638
39+ --- @type PlenaryWindowBorderBorderThickness
740Border ._default_thickness = {
841 top = 1 ,
942 right = 1 ,
1043 bot = 1 ,
1144 left = 1 ,
1245}
1346
47+ --- @param title_pos string
48+ --- @param title_len integer
49+ --- @param total_width integer
50+ --- @return integer
1451local calc_left_start = function (title_pos , title_len , total_width )
1552 if string.find (title_pos , " W" ) then
1653 return 0
@@ -21,6 +58,15 @@ local calc_left_start = function(title_pos, title_len, total_width)
2158 end
2259end
2360
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
2470local create_horizontal_line = function (title , pos , width , left_char , mid_char , right_char )
2571 local title_len
2672 if title == " " then
@@ -46,6 +92,7 @@ local create_horizontal_line = function(title, pos, width, left_char, mid_char,
4692 string.rep (mid_char , width - title_len - left_start ),
4793 right_char
4894 )
95+ --- @type PlenaryWindowBorderRanges
4996 local ranges = {}
5097 if title_len ~= 0 then
5198 -- 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,
55102 return horizontal_line , ranges
56103end
57104
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
58110function Border ._create_lines (content_win_id , content_win_options , border_win_options )
59111 local content_pos = vim .api .nvim_win_get_position (content_win_id )
60112 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
71123 border_win_options .border_thickness .left = left_enabled and 1 or 0
72124 border_win_options .border_thickness .right = right_enabled and 1 or 0
73125
126+ --- @type string[]
74127 local border_lines = {}
128+ --- @type PlenaryWindowBorderRanges
75129 local ranges = {}
76130
77131 -- border_win_options.title should have be a list with entries of the
78132 -- form: { pos = foo, text = bar }.
79133 -- pos can take values in { "NW", "N", "NE", "SW", "S", "SE" }
80134 local titles = type (border_win_options .title ) == " string" and { { pos = " N" , text = border_win_options .title } }
81135 or border_win_options .title
82- or {}
136+ or {} --[[ @as PlenaryWindowBorderTitles ]]
83137
84138 local topline = nil
85139 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
164218 return border_lines , ranges
165219end
166220
221+ --- @param bufnr integer
222+ --- @param ranges ? PlenaryWindowBorderRanges
223+ --- @param hl ? string
167224local set_title_highlights = function (bufnr , ranges , hl )
168225 -- Check if both `hl` and `ranges` are provided, and `ranges` is not the empty table.
169226 if hl and ranges and next (ranges ) then
@@ -173,6 +230,8 @@ local set_title_highlights = function(bufnr, ranges, hl)
173230 end
174231end
175232
233+ --- @param new_title string
234+ --- @param pos string
176235function Border :change_title (new_title , pos )
177236 if self ._border_win_options .title == new_title then
178237 return
195254
196255-- Updates characters for border lines, and returns nvim_win_config
197256-- (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
198260function Border :__align_calc_config (content_win_options , border_win_options )
199261 border_win_options = vim .tbl_deep_extend (" keep" , border_win_options , {
200262 border_thickness = Border ._default_thickness ,
239301-- Sets the size and position of the given Border.
240302-- Can be used to create a new window (with `create_window = true`)
241303-- or change an existing one
304+ --- @param content_win_options vim.api.keyset.win_config
305+ --- @param border_win_options PlenaryWindowBorderBorderOptions
242306function Border :move (content_win_options , border_win_options )
243307 -- Update lines in border buffer, and get config for border window
244308 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)
249313 set_title_highlights (self .bufnr , self .title_ranges , self ._border_win_options .titlehighlight )
250314end
251315
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
252321function Border :new (content_bufnr , content_win_id , content_win_options , border_win_options )
253322 assert (type (content_win_id ) == " number" , " Must supply a valid win_id. It's possible you forgot to call with ':'" )
254323
0 commit comments