Skip to content

Commit

Permalink
feat: Adds anchorpadding(nvim-telescope#2851)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weyaaron committed Apr 8, 2024
1 parent d26b666 commit f8825b5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
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
7 changes: 4 additions & 3 deletions lua/telescope/pickers/layout_strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ 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 = { "Additional padding around borders", "Values should be a positive integer" },
}

-- Used for generating vim help documentation.
Expand Down Expand Up @@ -375,7 +376,7 @@ 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_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines, layout_config.anchor_padding or 1 )
adjust_pos(anchor_pos, prompt, results, preview)

if tbln then
Expand Down Expand Up @@ -486,7 +487,7 @@ 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_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, layout_config.anchor_padding or 1)
adjust_pos(anchor_pos, prompt, results, preview)

-- Vertical anchoring (S or N variations) ignores layout_config.mirror
Expand Down Expand Up @@ -740,7 +741,7 @@ 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_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines,layout_config.anchor_padding or 1)
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

0 comments on commit f8825b5

Please sign in to comment.