Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(layout_config) Adds the option to configure additional lines of padding around the anchor of the layout (#2851) #3035

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lua/telescope/config/resolve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lua/telescope/pickers/layout_strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions lua/tests/automated/resolver_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ describe("telescope.config.resolve", function()

describe("resolve_anchor_pos", function()
local test_sizes = {
{ 6, 7, 8, 9 },
{ 10, 20, 30, 40 },
{ 15, 15, 16, 16 },
{ 17, 19, 23, 31 },
{ 21, 18, 26, 24 },
{ 50, 100, 150, 200 },
{ 6, 7, 8, 9, 1 },
{ 10, 20, 30, 40, 1 },
{ 15, 15, 16, 16, 1 },
{ 17, 19, 23, 31, 1 },
{ 21, 18, 26, 24, 1 },
{ 50, 100, 150, 200, 1 },
}

it([[should not adjust when "CENTER" or "" is the anchor]], function()
Expand All @@ -145,7 +145,7 @@ describe("telescope.config.resolve", 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 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))
end
for _, s in ipairs(test_sizes) do
Expand All @@ -157,7 +157,7 @@ describe("telescope.config.resolve", 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 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))
end
for _, s in ipairs(test_sizes) do
Expand All @@ -169,7 +169,7 @@ describe("telescope.config.resolve", 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 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))
end
for _, s in ipairs(test_sizes) do
Expand All @@ -181,7 +181,7 @@ describe("telescope.config.resolve", 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 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))
end
for _, s in ipairs(test_sizes) do
Expand All @@ -193,8 +193,8 @@ describe("telescope.config.resolve", function()

it([[should ignore casing of the anchor]], function()
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 pos2 = resolve.resolve_anchor_pos(a2, 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, 1)
eq(pos1, pos2)
end
for _, s in ipairs(test_sizes) do
Expand Down