aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-10-16 12:49:14 +0100
committerLewis Russell <lewis6991@gmail.com>2024-10-17 12:52:45 +0100
commit1944c0d610ce1616f0e4c93fca22b614361224e7 (patch)
treed2d28e9ebefc9d3e56b8d5e4aed1aed3e0032089 /runtime/lua/vim/lsp/util.lua
parentd44d36b8ff6cbd966daefc4ea9b775236175c887 (diff)
downloadrneovim-1944c0d610ce1616f0e4c93fca22b614361224e7.tar.gz
rneovim-1944c0d610ce1616f0e4c93fca22b614361224e7.tar.bz2
rneovim-1944c0d610ce1616f0e4c93fca22b614361224e7.zip
feat(lsp.util): refactor get_border_size()
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua130
1 files changed, 59 insertions, 71 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index f77597e40d..3ae9e6669f 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -21,85 +21,73 @@ local default_border = {
{ ' ', 'NormalFloat' },
}
+--- @param border string|(string|[string,string])[]
+local function border_error(border)
+ error(
+ string.format(
+ 'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
+ vim.inspect(border)
+ ),
+ 2
+ )
+end
+
+local border_size = {
+ none = { 0, 0 },
+ single = { 2, 2 },
+ double = { 2, 2 },
+ rounded = { 2, 2 },
+ solid = { 2, 2 },
+ shadow = { 1, 1 },
+}
+
--- Check the border given by opts or the default border for the additional
--- size it adds to a float.
----@param opts? {border:string|(string|[string,string])[]}
----@return {height:integer,width:integer} # size of border in the form of { height = height, width = width }
+--- @param opts? {border:string|(string|[string,string])[]}
+--- @return integer height
+--- @return integer width
local function get_border_size(opts)
local border = opts and opts.border or default_border
- local height = 0
- local width = 0
if type(border) == 'string' then
- local border_size = {
- none = { 0, 0 },
- single = { 2, 2 },
- double = { 2, 2 },
- rounded = { 2, 2 },
- solid = { 2, 2 },
- shadow = { 1, 1 },
- }
- if border_size[border] == nil then
- error(
- string.format(
- 'invalid floating preview border: %s. :help vim.api.nvim_open_win()',
- vim.inspect(border)
- )
- )
- end
- height, width = 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)
- )
- )
+ if not border_size[border] then
+ border_error(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)
- )
- )
+ return unpack(border_size[border])
+ end
+
+ 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
- height = height + border_height(2) -- top
- height = height + border_height(6) -- bottom
- width = width + border_width(4) -- right
- width = width + border_width(8) -- left
+ --- @diagnostic disable-next-line:missing-return
+ border_error(border)
end
- return { height = height, width = width }
+ --- @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
--- 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
end
- local border_height = get_border_size(opts).height
+ local border_height = get_border_size(opts)
local row, col --- @type integer?, integer?
if anchor_below then
anchor = anchor .. 'N'
@@ -1495,7 +1483,7 @@ function M._make_floating_popup_size(contents, opts)
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)
width = math.min(width, screen_width)