Skip to content

Commit 816dff9

Browse files
authored
feat(layout_config): add anchor_padding option (#3035)
1 parent 61a4a61 commit 816dff9

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

lua/telescope/config/resolve.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,21 @@ end
251251
--- - Compass directions:<br>
252252
--- the picker will move to the corresponding edge/corner
253253
--- e.g. "NW" -> "top left corner", "E" -> "right edge", "S" -> "bottom edge"
254-
resolver.resolve_anchor_pos = function(anchor, p_width, p_height, max_columns, max_lines)
254+
resolver.resolve_anchor_pos = function(anchor, p_width, p_height, max_columns, max_lines, anchor_padding)
255255
anchor = anchor:upper()
256256
local pos = { 0, 0 }
257257
if anchor == "CENTER" then
258258
return pos
259259
end
260260
if anchor:find "W" then
261-
pos[1] = math.ceil((p_width - max_columns) / 2) + 1
261+
pos[1] = math.ceil((p_width - max_columns) / 2) + anchor_padding
262262
elseif anchor:find "E" then
263-
pos[1] = math.ceil((max_columns - p_width) / 2) - 1
263+
pos[1] = math.ceil((max_columns - p_width) / 2) - anchor_padding
264264
end
265265
if anchor:find "N" then
266-
pos[2] = math.ceil((p_height - max_lines) / 2) + 1
266+
pos[2] = math.ceil((p_height - max_lines) / 2) + anchor_padding
267267
elseif anchor:find "S" then
268-
pos[2] = math.ceil((max_lines - p_height) / 2) - 1
268+
pos[2] = math.ceil((max_lines - p_height) / 2) - anchor_padding
269269
end
270270
return pos
271271
end

lua/telescope/pickers/layout_strategies.lua

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ local shared_options = {
194194
scroll_speed = "The number of lines to scroll through the previewer",
195195
prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
196196
anchor = { "Which edge/corner to pin the picker to", "See |resolver.resolve_anchor_pos()|" },
197+
anchor_padding = {
198+
"Specifies an amount of additional padding around the anchor",
199+
"Values should be a positive integer",
200+
},
197201
}
198202

199203
-- Used for generating vim help documentation.
@@ -375,7 +379,10 @@ layout_strategies.horizontal = make_documented_layout(
375379
error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
376380
end
377381

378-
local anchor_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines)
382+
local anchor = layout_config.anchor or ""
383+
local anchor_padding = layout_config.anchor_padding or 1
384+
385+
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
379386
adjust_pos(anchor_pos, prompt, results, preview)
380387

381388
if tbln then
@@ -486,7 +493,9 @@ layout_strategies.center = make_documented_layout(
486493
results.col, preview.col, prompt.col = width_padding, width_padding, width_padding
487494

488495
local anchor = layout_config.anchor or ""
489-
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines)
496+
local anchor_padding = layout_config.anchor_padding or 1
497+
498+
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
490499
adjust_pos(anchor_pos, prompt, results, preview)
491500

492501
-- Vertical anchoring (S or N variations) ignores layout_config.mirror
@@ -740,7 +749,10 @@ layout_strategies.vertical = make_documented_layout(
740749
end
741750
end
742751

743-
local anchor_pos = resolve.resolve_anchor_pos(layout_config.anchor or "", width, height, max_columns, max_lines)
752+
local anchor = layout_config.anchor or ""
753+
local anchor_padding = layout_config.anchor_padding or 1
754+
755+
local anchor_pos = resolve.resolve_anchor_pos(anchor, width, height, max_columns, max_lines, anchor_padding)
744756
adjust_pos(anchor_pos, prompt, results, preview)
745757

746758
if tbln then

lua/tests/automated/resolver_spec.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ describe("telescope.config.resolve", function()
127127

128128
describe("resolve_anchor_pos", function()
129129
local test_sizes = {
130-
{ 6, 7, 8, 9 },
131-
{ 10, 20, 30, 40 },
132-
{ 15, 15, 16, 16 },
133-
{ 17, 19, 23, 31 },
134-
{ 21, 18, 26, 24 },
135-
{ 50, 100, 150, 200 },
130+
{ 6, 7, 8, 9, 1 },
131+
{ 10, 20, 30, 40, 1 },
132+
{ 15, 15, 16, 16, 1 },
133+
{ 17, 19, 23, 31, 1 },
134+
{ 21, 18, 26, 24, 1 },
135+
{ 50, 100, 150, 200, 1 },
136136
}
137137

138138
it([[should not adjust when "CENTER" or "" is the anchor]], function()
@@ -145,7 +145,7 @@ describe("telescope.config.resolve", function()
145145

146146
it([[should end up at top when "N" in the anchor]], function()
147147
local top_test = function(anchor, p_width, p_height, max_columns, max_lines)
148-
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines)
148+
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
149149
eq(1, pos[2] + math.floor((max_lines - p_height) / 2))
150150
end
151151
for _, s in ipairs(test_sizes) do
@@ -157,7 +157,7 @@ describe("telescope.config.resolve", function()
157157

158158
it([[should end up at left when "W" in the anchor]], function()
159159
local left_test = function(anchor, p_width, p_height, max_columns, max_lines)
160-
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines)
160+
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
161161
eq(1, pos[1] + math.floor((max_columns - p_width) / 2))
162162
end
163163
for _, s in ipairs(test_sizes) do
@@ -169,7 +169,7 @@ describe("telescope.config.resolve", function()
169169

170170
it([[should end up at bottom when "S" in the anchor]], function()
171171
local bot_test = function(anchor, p_width, p_height, max_columns, max_lines)
172-
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines)
172+
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
173173
eq(max_lines - 1, pos[2] + p_height + math.floor((max_lines - p_height) / 2))
174174
end
175175
for _, s in ipairs(test_sizes) do
@@ -181,7 +181,7 @@ describe("telescope.config.resolve", function()
181181

182182
it([[should end up at right when "E" in the anchor]], function()
183183
local right_test = function(anchor, p_width, p_height, max_columns, max_lines)
184-
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines)
184+
local pos = resolve.resolve_anchor_pos(anchor, p_width, p_height, max_columns, max_lines, 1)
185185
eq(max_columns - 1, pos[1] + p_width + math.floor((max_columns - p_width) / 2))
186186
end
187187
for _, s in ipairs(test_sizes) do
@@ -193,8 +193,8 @@ describe("telescope.config.resolve", function()
193193

194194
it([[should ignore casing of the anchor]], function()
195195
local case_test = function(a1, a2, p_width, p_height, max_columns, max_lines)
196-
local pos1 = resolve.resolve_anchor_pos(a1, p_width, p_height, max_columns, max_lines)
197-
local pos2 = resolve.resolve_anchor_pos(a2, p_width, p_height, max_columns, max_lines)
196+
local pos1 = resolve.resolve_anchor_pos(a1, p_width, p_height, max_columns, max_lines, 1)
197+
local pos2 = resolve.resolve_anchor_pos(a2, p_width, p_height, max_columns, max_lines, 1)
198198
eq(pos1, pos2)
199199
end
200200
for _, s in ipairs(test_sizes) do

0 commit comments

Comments
 (0)