mirror of
https://github.com/neovim/neovim.git
synced 2024-12-24 21:25:04 -07:00
feat(lsp.util): refactor get_border_size()
This commit is contained in:
parent
d44d36b8ff
commit
1944c0d610
@ -21,85 +21,73 @@ local default_border = {
|
|||||||
{ ' ', 'NormalFloat' },
|
{ ' ', 'NormalFloat' },
|
||||||
}
|
}
|
||||||
|
|
||||||
--- Check the border given by opts or the default border for the additional
|
--- @param border string|(string|[string,string])[]
|
||||||
--- size it adds to a float.
|
local function border_error(border)
|
||||||
---@param opts? {border:string|(string|[string,string])[]}
|
error(
|
||||||
---@return {height:integer,width:integer} # size of border in the form of { height = height, width = width }
|
string.format(
|
||||||
local function get_border_size(opts)
|
'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
|
||||||
local border = opts and opts.border or default_border
|
vim.inspect(border)
|
||||||
local height = 0
|
),
|
||||||
local width = 0
|
2
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
if type(border) == 'string' then
|
local border_size = {
|
||||||
local border_size = {
|
|
||||||
none = { 0, 0 },
|
none = { 0, 0 },
|
||||||
single = { 2, 2 },
|
single = { 2, 2 },
|
||||||
double = { 2, 2 },
|
double = { 2, 2 },
|
||||||
rounded = { 2, 2 },
|
rounded = { 2, 2 },
|
||||||
solid = { 2, 2 },
|
solid = { 2, 2 },
|
||||||
shadow = { 1, 1 },
|
shadow = { 1, 1 },
|
||||||
}
|
}
|
||||||
if border_size[border] == nil then
|
|
||||||
error(
|
--- Check the border given by opts or the default border for the additional
|
||||||
string.format(
|
--- size it adds to a float.
|
||||||
'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
|
--- @param opts? {border:string|(string|[string,string])[]}
|
||||||
vim.inspect(border)
|
--- @return integer height
|
||||||
)
|
--- @return integer width
|
||||||
)
|
local function get_border_size(opts)
|
||||||
|
local border = opts and opts.border or default_border
|
||||||
|
|
||||||
|
if type(border) == 'string' then
|
||||||
|
if not border_size[border] then
|
||||||
|
border_error(border)
|
||||||
end
|
end
|
||||||
height, width = unpack(border_size[border])
|
return unpack(border_size[border])
|
||||||
else
|
|
||||||
if 8 % #border ~= 0 then
|
|
||||||
error(
|
|
||||||
string.format(
|
|
||||||
'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
|
|
||||||
vim.inspect(border)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
--- @param id integer
|
|
||||||
local function border_width(id)
|
|
||||||
id = (id - 1) % #border + 1
|
|
||||||
local e = border[id]
|
|
||||||
if type(e) == 'table' then
|
|
||||||
-- border specified as a table of <character, highlight group>
|
|
||||||
return vim.fn.strdisplaywidth(e[1])
|
|
||||||
elseif type(e) == 'string' then
|
|
||||||
-- border specified as a list of border characters
|
|
||||||
return vim.fn.strdisplaywidth(e)
|
|
||||||
end
|
|
||||||
error(
|
|
||||||
string.format(
|
|
||||||
'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
|
|
||||||
vim.inspect(border)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
--- @param id integer
|
|
||||||
local function border_height(id)
|
|
||||||
id = (id - 1) % #border + 1
|
|
||||||
local e = border[id]
|
|
||||||
if type(e) == 'table' then
|
|
||||||
-- border specified as a table of <character, highlight group>
|
|
||||||
return #e[1] > 0 and 1 or 0
|
|
||||||
elseif type(e) == 'string' then
|
|
||||||
-- border specified as a list of border characters
|
|
||||||
return #e > 0 and 1 or 0
|
|
||||||
end
|
|
||||||
error(
|
|
||||||
string.format(
|
|
||||||
'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
|
|
||||||
vim.inspect(border)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
height = height + border_height(2) -- top
|
|
||||||
height = height + border_height(6) -- bottom
|
|
||||||
width = width + border_width(4) -- right
|
|
||||||
width = width + border_width(8) -- left
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return { height = height, width = width }
|
if 8 % #border ~= 0 then
|
||||||
|
border_error(border)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param id integer
|
||||||
|
--- @return string
|
||||||
|
local function elem(id)
|
||||||
|
id = (id - 1) % #border + 1
|
||||||
|
local e = border[id]
|
||||||
|
if type(e) == 'table' then
|
||||||
|
-- border specified as a table of <character, highlight group>
|
||||||
|
return e[1]
|
||||||
|
elseif type(e) == 'string' then
|
||||||
|
-- border specified as a list of border characters
|
||||||
|
return e
|
||||||
|
end
|
||||||
|
--- @diagnostic disable-next-line:missing-return
|
||||||
|
border_error(border)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- @param e string
|
||||||
|
local function border_height(e)
|
||||||
|
return #e > 0 and 1 or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
local top, bottom = elem(2), elem(6)
|
||||||
|
local height = border_height(top) + border_height(bottom)
|
||||||
|
|
||||||
|
local right, left = elem(4), elem(8)
|
||||||
|
local width = vim.fn.strdisplaywidth(right) + vim.fn.strdisplaywidth(left)
|
||||||
|
|
||||||
|
return height, width
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Splits string at newlines, optionally removing unwanted blank lines.
|
--- Splits string at newlines, optionally removing unwanted blank lines.
|
||||||
@ -930,7 +918,7 @@ function M.make_floating_popup_options(width, height, opts)
|
|||||||
anchor_below = lines_below > lines_above
|
anchor_below = lines_below > lines_above
|
||||||
end
|
end
|
||||||
|
|
||||||
local border_height = get_border_size(opts).height
|
local border_height = get_border_size(opts)
|
||||||
local row, col --- @type integer?, integer?
|
local row, col --- @type integer?, integer?
|
||||||
if anchor_below then
|
if anchor_below then
|
||||||
anchor = anchor .. 'N'
|
anchor = anchor .. 'N'
|
||||||
@ -1495,7 +1483,7 @@ function M._make_floating_popup_size(contents, opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local border_width = get_border_size(opts).width
|
local _, border_width = get_border_size(opts)
|
||||||
local screen_width = api.nvim_win_get_width(0)
|
local screen_width = api.nvim_win_get_width(0)
|
||||||
width = math.min(width, screen_width)
|
width = math.min(width, screen_width)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user