Skip to content

Commit 5d4a65a

Browse files
authored
fix: remove edge of Border when drawn at the boundary (#242)
1 parent 1c31adb commit 5d4a65a

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

lua/plenary/window/border.lua

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,41 +55,37 @@ local create_horizontal_line = function(title, pos, width, left_char, mid_char,
5555
return horizontal_line, ranges
5656
end
5757

58-
function Border._create_lines(content_win_options, border_win_options)
58+
function Border._create_lines(content_win_id, content_win_options, border_win_options)
59+
local content_pos = vim.api.nvim_win_get_position(content_win_id)
60+
local content_height = vim.api.nvim_win_get_height(content_win_id)
61+
local content_width = vim.api.nvim_win_get_width(content_win_id)
62+
5963
-- TODO: Handle border width, which I haven't right here.
6064
local thickness = border_win_options.border_thickness
6165

6266
local top_enabled = thickness.top == 1
63-
local right_enabled = thickness.right == 1
67+
local right_enabled = thickness.right == 1 and content_pos[2] + content_width < vim.o.columns
6468
local bot_enabled = thickness.bot == 1
65-
local left_enabled = thickness.left == 1
69+
local left_enabled = thickness.left == 1 and content_pos[2] > 0
70+
71+
border_win_options.border_thickness.left = left_enabled and 1 or 0
72+
border_win_options.border_thickness.right = right_enabled and 1 or 0
6673

6774
local border_lines = {}
6875
local ranges = {}
6976

70-
local topline = nil
71-
72-
local topleft = (left_enabled and border_win_options.topleft) or ""
73-
local topright = (right_enabled and border_win_options.topright) or ""
74-
7577
-- border_win_options.title should have be a list with entries of the
7678
-- form: { pos = foo, text = bar }.
7779
-- pos can take values in { "NW", "N", "NE", "SW", "S", "SE" }
7880
local titles = type(border_win_options.title) == "string" and { { pos = "N", text = border_win_options.title } }
7981
or border_win_options.title
8082
or {}
8183

82-
--[[
83-
-- Ensure that the topline is drawn only if the row is positive (for an absolute position) or if when added to the current
84-
-- cursor line (for a cursor relative position) it is also a positive value.
85-
--]]
86-
if
87-
content_win_options.row > 0
88-
or (
89-
content_win_options.relative == "cursor"
90-
and content_win_options.row + vim.api.nvim_win_get_cursor(0)[1] + vim.api.nvim_win_get_position(0)[1] > 1
91-
)
92-
then
84+
local topline = nil
85+
local topleft = (left_enabled and border_win_options.topleft) or ""
86+
local topright = (right_enabled and border_win_options.topright) or ""
87+
-- Only calculate the topline if there is space above the first content row (relative to the editor)
88+
if content_pos[1] > 0 then
9389
for _, title in ipairs(titles) do
9490
if string.find(title.pos, "N") then
9591
local top_ranges
@@ -131,10 +127,10 @@ function Border._create_lines(content_win_options, border_win_options)
131127
table.insert(border_lines, middle_line)
132128
end
133129

134-
if bot_enabled then
135-
local botline = nil
136-
local botleft = (left_enabled and border_win_options.botleft) or ""
137-
local botright = (right_enabled and border_win_options.botright) or ""
130+
local botline = nil
131+
local botleft = (left_enabled and border_win_options.botleft) or ""
132+
local botright = (right_enabled and border_win_options.botright) or ""
133+
if content_pos[1] + content_height < vim.o.lines then
138134
for _, title in ipairs(titles) do
139135
if string.find(title.pos, "S") then
140136
local bot_ranges
@@ -153,10 +149,15 @@ function Border._create_lines(content_win_options, border_win_options)
153149
end
154150
end
155151
if botline == nil then
156-
if top_enabled then
152+
if bot_enabled then
157153
botline = botleft .. string.rep(border_win_options.bot, content_win_options.width) .. botright
158154
end
159155
end
156+
else
157+
border_win_options.border_thickness.bot = 0
158+
end
159+
160+
if botline then
160161
table.insert(border_lines, botline)
161162
end
162163

@@ -178,7 +179,11 @@ function Border:change_title(new_title)
178179
end
179180

180181
self._border_win_options.title = new_title
181-
self.contents, self.title_ranges = Border._create_lines(self.content_win_options, self._border_win_options)
182+
self.contents, self.title_ranges = Border._create_lines(
183+
self.content_win_id,
184+
self.content_win_options,
185+
self._border_win_options
186+
)
182187
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, self.contents)
183188

184189
set_title_highlights(self.bufnr, self.title_ranges, self._border_win_options.titlehighlight)
@@ -201,6 +206,14 @@ function Border:__align_calc_config(content_win_options, border_win_options)
201206
bot = "",
202207
})
203208

209+
-- Ensure the relevant contents and border win_options are set
210+
self._border_win_options = border_win_options
211+
self.content_win_options = content_win_options
212+
-- Update border characters and title_ranges
213+
self.contents, self.title_ranges = Border._create_lines(self.content_win_id, content_win_options, border_win_options)
214+
215+
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, self.contents)
216+
204217
local thickness = border_win_options.border_thickness
205218
local nvim_win_config = {
206219
anchor = content_win_options.anchor,
@@ -215,14 +228,6 @@ function Border:__align_calc_config(content_win_options, border_win_options)
215228
focusable = vim.F.if_nil(border_win_options.focusable, false),
216229
}
217230

218-
-- Ensure the relevant contests and border win_options are set
219-
self._border_win_options = border_win_options
220-
self.content_win_options = content_win_options
221-
-- Update border characters and title_ranges
222-
self.contents, self.title_ranges = Border._create_lines(content_win_options, border_win_options)
223-
224-
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, self.contents)
225-
226231
return nvim_win_config
227232
end
228233

0 commit comments

Comments
 (0)