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> --- - Compass directions:<br>
--- the picker will move to the corresponding edge/corner --- the picker will move to the corresponding edge/corner
--- e.g. "NW" -> "top left corner", "E" -> "right edge", "S" -> "bottom edge" --- 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() anchor = anchor:upper()
local pos = { 0, 0 } local pos = { 0, 0 }
if anchor == "CENTER" then if anchor == "CENTER" then
return pos return pos
end end
if anchor:find "W" then 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 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 end
if anchor:find "N" then 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 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 end
return pos return pos
end end

View File

@@ -194,6 +194,10 @@ local shared_options = {
scroll_speed = "The number of lines to scroll through the previewer", scroll_speed = "The number of lines to scroll through the previewer",
prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" }, 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 = { "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. -- 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))) error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
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) adjust_pos(anchor_pos, prompt, results, preview)
if tbln then 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 results.col, preview.col, prompt.col = width_padding, width_padding, width_padding
local anchor = layout_config.anchor or "" 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) adjust_pos(anchor_pos, prompt, results, preview)
-- Vertical anchoring (S or N variations) ignores layout_config.mirror -- Vertical anchoring (S or N variations) ignores layout_config.mirror
@@ -740,7 +749,10 @@ layout_strategies.vertical = make_documented_layout(
end end
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) adjust_pos(anchor_pos, prompt, results, preview)
if tbln then if tbln then

View File

@@ -127,12 +127,12 @@ describe("telescope.config.resolve", function()
describe("resolve_anchor_pos", function() describe("resolve_anchor_pos", function()
local test_sizes = { local test_sizes = {
{ 6, 7, 8, 9 }, { 6, 7, 8, 9, 1 },
{ 10, 20, 30, 40 }, { 10, 20, 30, 40, 1 },
{ 15, 15, 16, 16 }, { 15, 15, 16, 16, 1 },
{ 17, 19, 23, 31 }, { 17, 19, 23, 31, 1 },
{ 21, 18, 26, 24 }, { 21, 18, 26, 24, 1 },
{ 50, 100, 150, 200 }, { 50, 100, 150, 200, 1 },
} }
it([[should not adjust when "CENTER" or "" is the anchor]], function() it([[should not adjust when "CENTER" or "" is the anchor]], function()
@@ -145,7 +145,7 @@ describe("telescope.config.resolve", function()
it([[should end up at top when "N" in the anchor]], function() it([[should end up at top when "N" in the anchor]], function()
local top_test = function(anchor, p_width, p_height, max_columns, max_lines) local top_test = function(anchor, p_width, p_height, max_columns, max_lines)
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines) local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
eq(1, pos[2] + math.floor((max_lines - p_height) / 2)) eq(1, pos[2] + math.floor((max_lines - p_height) / 2))
end end
for _, s in ipairs(test_sizes) do for _, s in ipairs(test_sizes) do
@@ -157,7 +157,7 @@ describe("telescope.config.resolve", function()
it([[should end up at left when "W" in the anchor]], function() it([[should end up at left when "W" in the anchor]], function()
local left_test = function(anchor, p_width, p_height, max_columns, max_lines) local left_test = function(anchor, p_width, p_height, max_columns, max_lines)
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines) local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
eq(1, pos[1] + math.floor((max_columns - p_width) / 2)) eq(1, pos[1] + math.floor((max_columns - p_width) / 2))
end end
for _, s in ipairs(test_sizes) do for _, s in ipairs(test_sizes) do
@@ -169,7 +169,7 @@ describe("telescope.config.resolve", function()
it([[should end up at bottom when "S" in the anchor]], function() it([[should end up at bottom when "S" in the anchor]], function()
local bot_test = function(anchor, p_width, p_height, max_columns, max_lines) local bot_test = function(anchor, p_width, p_height, max_columns, max_lines)
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines) local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
eq(max_lines - 1, pos[2] + p_height + math.floor((max_lines - p_height) / 2)) eq(max_lines - 1, pos[2] + p_height + math.floor((max_lines - p_height) / 2))
end end
for _, s in ipairs(test_sizes) do for _, s in ipairs(test_sizes) do
@@ -181,7 +181,7 @@ describe("telescope.config.resolve", function()
it([[should end up at right when "E" in the anchor]], function() it([[should end up at right when "E" in the anchor]], function()
local right_test = function(anchor, p_width, p_height, max_columns, max_lines) local right_test = function(anchor, p_width, p_height, max_columns, max_lines)
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines) local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
eq(max_columns - 1, pos[1] + p_width + math.floor((max_columns - p_width) / 2)) eq(max_columns - 1, pos[1] + p_width + math.floor((max_columns - p_width) / 2))
end end
for _, s in ipairs(test_sizes) do for _, s in ipairs(test_sizes) do
@@ -193,8 +193,8 @@ describe("telescope.config.resolve", function()
it([[should ignore casing of the anchor]], function() it([[should ignore casing of the anchor]], function()
local case_test = function(a1, a2, p_width, p_height, max_columns, max_lines) local case_test = function(a1, a2, p_width, p_height, max_columns, max_lines)
local pos1 = resolve.resolve_anchor_pos(a1, p_width, p_height, max_columns, max_lines) local pos1 = resolve.resolve_anchor_pos(a1, p_width, p_height, max_columns, max_lines, 1)
local pos2 = resolve.resolve_anchor_pos(a2, p_width, p_height, max_columns, max_lines) local pos2 = resolve.resolve_anchor_pos(a2, p_width, p_height, max_columns, max_lines, 1)
eq(pos1, pos2) eq(pos1, pos2)
end end
for _, s in ipairs(test_sizes) do for _, s in ipairs(test_sizes) do