feat(layout_config): add anchor_padding option (#3035)

This commit is contained in:
Aaron
2024-06-27 02:47:23 +02:00
committed by GitHub
parent 61a4a61536
commit 816dff9bbf
3 changed files with 32 additions and 20 deletions

View File

@@ -251,21 +251,21 @@ end
--- - Compass directions:<br>
--- the picker will move to the corresponding edge/corner
--- e.g. "NW" -> "top left corner", "E" -> "right edge", "S" -> "bottom edge"
resolver.resolve_anchor_pos = function(anchor, p_width, p_height, max_columns, max_lines)
resolver.resolve_anchor_pos = function(anchor, p_width, p_height, max_columns, max_lines, anchor_padding)
anchor = anchor:upper()
local pos = { 0, 0 }
if anchor == "CENTER" then
return pos
end
if anchor:find "W" then
pos[1] = math.ceil((p_width - max_columns) / 2) + 1
pos[1] = math.ceil((p_width - max_columns) / 2) + anchor_padding
elseif anchor:find "E" then
pos[1] = math.ceil((max_columns - p_width) / 2) - 1
pos[1] = math.ceil((max_columns - p_width) / 2) - anchor_padding
end
if anchor:find "N" then
pos[2] = math.ceil((p_height - max_lines) / 2) + 1
pos[2] = math.ceil((p_height - max_lines) / 2) + anchor_padding
elseif anchor:find "S" then
pos[2] = math.ceil((max_lines - p_height) / 2) - 1
pos[2] = math.ceil((max_lines - p_height) / 2) - anchor_padding
end
return pos
end

View File

@@ -194,6 +194,10 @@ local shared_options = {
scroll_speed = "The number of lines to scroll through the previewer",
prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
anchor = { "Which edge/corner to pin the picker to", "See |resolver.resolve_anchor_pos()|" },
anchor_padding = {
"Specifies an amount of additional padding around the anchor",
"Values should be a positive integer",
},
}
-- Used for generating vim help documentation.
@@ -375,7 +379,10 @@ layout_strategies.horizontal = make_documented_layout(
error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
end
local anchor_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines)
local anchor = layout_config.anchor or ""
local anchor_padding = layout_config.anchor_padding or 1
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
adjust_pos(anchor_pos, prompt, results, preview)
if tbln then
@@ -486,7 +493,9 @@ layout_strategies.center = make_documented_layout(
results.col, preview.col, prompt.col = width_padding, width_padding, width_padding
local anchor = layout_config.anchor or ""
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines)
local anchor_padding = layout_config.anchor_padding or 1
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
adjust_pos(anchor_pos, prompt, results, preview)
-- Vertical anchoring (S or N variations) ignores layout_config.mirror
@@ -740,7 +749,10 @@ layout_strategies.vertical = make_documented_layout(
end
end
local anchor_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines)
local anchor = layout_config.anchor or ""
local anchor_padding = layout_config.anchor_padding or 1
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
adjust_pos(anchor_pos, prompt, results, preview)
if tbln then