Skip to content

Commit 3a1ce1f

Browse files
committed
Resolve preview_cutoff (first attempt)
1 parent 28f3b27 commit 3a1ce1f

File tree

4 files changed

+113
-35
lines changed

4 files changed

+113
-35
lines changed

lua/telescope/config.lua

+10-2
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,18 @@ local layout_config_defaults = {
161161

162162
horizontal = {
163163
prompt_position = "bottom",
164-
preview_cutoff = 120,
164+
preview_cutoff = {columns = 120},
165165
},
166166

167-
-- vertical = {},
167+
vertical = {
168+
prompt_position = "bottom",
169+
preview_cutoff = {lines = 40},
170+
},
171+
172+
center = {
173+
preview_cutoff = {lines = 40},
174+
height = 0.5
175+
},
168176
}
169177

170178
local layout_config_description = string.format([[

lua/telescope/config/resolve.lua

+72-12
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,25 @@ That's the next step to scrolling.
9393
local get_default = require('telescope.utils').get_default
9494

9595
local resolver = {}
96-
local _resolve_map = {}
96+
local _resolve_len = {}
9797

9898
-- Booleans
99-
_resolve_map[function(val) return val == false end] = function(_, val)
99+
_resolve_len[function(val) return val == false end] = function(_, val)
100100
return function(...)
101101
return val
102102
end
103103
end
104104

105105
-- Percentages
106-
_resolve_map[function(val) return type(val) == 'number' and val >= 0 and val < 1 end] = function(selector, val)
106+
_resolve_len[function(val) return type(val) == 'number' and val >= 0 and val < 1 end] = function(selector, val)
107107
return function(...)
108108
local selected = select(selector, ...)
109109
return math.floor(val * selected)
110110
end
111111
end
112112

113113
-- Numbers
114-
_resolve_map[function(val) return type(val) == 'number' and val >= 1 end] = function(selector, val)
114+
_resolve_len[function(val) return type(val) == 'number' and val >= 1 end] = function(selector, val)
115115
return function(...)
116116
local selected = select(selector, ...)
117117
return math.min(val, selected)
@@ -126,20 +126,20 @@ end
126126
-- function(self, max_columns, max_lines): number
127127
--
128128
-- Resulting number is used for this configuration value.
129-
_resolve_map[function(val) return type(val) == 'function' end] = function(_, val)
129+
_resolve_len[function(val) return type(val) == 'function' end] = function(_, val)
130130
return val
131131
end
132132

133133
-- Add padding option
134-
_resolve_map[function(val) return type(val) == 'table' and val['padding'] ~= nil end] = function(selector, val)
134+
_resolve_len[function(val) return type(val) == 'table' and val['padding'] ~= nil end] = function(selector, val)
135135
local resolve_pad = function(value)
136-
for k, v in pairs(_resolve_map) do
136+
for k, v in pairs(_resolve_len) do
137137
if k(value) then
138138
return v(selector, value)
139139
end
140140
end
141141

142-
error('invalid configuration option for padding:' .. tostring(value))
142+
error('invalid configuration option for padding: ' .. tostring(value))
143143
end
144144

145145
return function(...)
@@ -169,13 +169,13 @@ end
169169
--- The returned function will have signature:
170170
--- function(self, max_columns, max_lines): number
171171
resolver.resolve_height = function(val)
172-
for k, v in pairs(_resolve_map) do
172+
for k, v in pairs(_resolve_len) do
173173
if k(val) then
174174
return v(3, val)
175175
end
176176
end
177177

178-
error('invalid configuration option for height:' .. tostring(val))
178+
error('invalid configuration option for height: ' .. tostring(val))
179179
end
180180

181181
--- Converts input to a function that returns the width.
@@ -197,13 +197,73 @@ end
197197
--- The returned function will have signature:
198198
--- function(self, max_columns, max_lines): number
199199
resolver.resolve_width = function(val)
200-
for k, v in pairs(_resolve_map) do
200+
for k, v in pairs(_resolve_len) do
201201
if k(val) then
202202
return v(2, val)
203203
end
204204
end
205205

206-
error('invalid configuration option for width:' .. tostring(val))
206+
error('invalid configuration option for width: ' .. tostring(val))
207+
end
208+
209+
210+
211+
local _resolve_cut = {}
212+
213+
-- Booleans
214+
_resolve_cut[function(val) return val == false or val == true end] = function(val)
215+
return function()
216+
return val
217+
end
218+
end
219+
220+
-- Columns
221+
_resolve_cut[function(val) return type(val) == 'table' and val['columns'] ~= nil end] = function(val)
222+
return function(_,max_columns,_)
223+
return (val.columns <= max_columns)
224+
end
225+
end
226+
227+
-- Lines
228+
_resolve_cut[function(val) return type(val) == 'table' and val['lines'] ~= nil end] = function(val)
229+
return function(_,_,max_lines)
230+
return (val.lines <= max_lines)
231+
end
232+
end
233+
234+
--- Function
235+
_resolve_cut[function(val) return type(val) == 'function' end] = function(_, val)
236+
return val
237+
end
238+
239+
--- Converts input to a function that returns a boolean.
240+
--- The input must take one of four forms:
241+
--- 1. boolean:
242+
--- The cutoff is then a function that always returns the given boolean.
243+
--- 2. table of the form:
244+
--- {columns = `foo`} <br>
245+
--- where `foo` is a number >= 1. <br>
246+
--- The cutoff is then a function that is true when `max_columns` is greater than
247+
--- or equal to `foo` and is false otherwise.
248+
--- 3. table of the form:
249+
--- {lines = `foo`} <br>
250+
--- where `foo` is a number >= 1. <br>
251+
--- The cutoff is then a function that is true when `max_lines` is greater than
252+
--- or equal to `foo` and is false otherwise.
253+
--- 4. function <br>
254+
--- Must have signature:
255+
--- function(self, max_columns, max_lines): boolean
256+
---
257+
--- The returned function will have signature:
258+
--- function(self, max_columns, max_lines): boolean
259+
resolver.resolve_cutoff = function(val)
260+
for k, v in pairs(_resolve_cut) do
261+
if k(val) then
262+
return v(val)
263+
end
264+
end
265+
266+
error('invalid configuration option for cutoff: ' .. tostring(val))
207267
end
208268

209269
--- Win option always returns a table with preview, results, and prompt.

lua/telescope/pickers/layout_strategies.lua

+30-20
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ end
241241
---
242242
layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_extend("error", shared_options, {
243243
preview_width = { "Change the width of Telescope's preview window", "See |resolver.resolve_width()|", },
244-
preview_cutoff = "When columns are less than this value, the preview will be disabled",
244+
preview_cutoff = {"Decides whether to display preview based on size of window.",
245+
"See |resolver.resolve_cutoff|"},
245246
prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
246247
}), function(self, max_columns, max_lines, layout_config)
247248
local initial_options = p_window.get_initial_window_options(self)
@@ -257,11 +258,11 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte
257258
local picker_height = resolve.resolve_height(height_opt)(self, max_columns, max_lines)
258259
local height_padding = math.floor((max_lines - picker_height)/2)
259260

260-
if self.previewer then
261+
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
262+
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
263+
if has_preview then
261264
preview.width = resolve.resolve_width(layout_config.preview_width or function(_, cols)
262-
if not self.previewer or cols < layout_config.preview_cutoff then
263-
return 0
264-
elseif cols < 150 then
265+
if cols < 150 then
265266
return math.floor(cols * 0.4)
266267
elseif cols < 200 then
267268
return 80
@@ -279,7 +280,7 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte
279280
prompt.height = 1
280281
results.height = picker_height - prompt.height - 2
281282

282-
if self.previewer then
283+
if has_preview then
283284
preview.height = picker_height
284285
else
285286
preview.height = 0
@@ -342,7 +343,8 @@ end)
342343
---
343344
layout_strategies.center = make_documented_layout("center", vim.tbl_extend("error", shared_options, {
344345
-- TODO: Should this be based on rows here?
345-
preview_cutoff = "When columns are less than this value, the preview will be disabled",
346+
preview_cutoff = {"Decides whether to display preview based on size of window.",
347+
"See |resolver.resolve_cutoff|"},
346348
}), function(self, max_columns, max_lines,layout_config)
347349
local initial_options = p_window.get_initial_window_options(self)
348350
local preview = initial_options.preview
@@ -375,9 +377,12 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro
375377
results.line = prompt.line + 1 + (bs)
376378

377379
preview.line = 1
378-
preview.height = math.floor(prompt.line - (2 + bs))
379380

380-
if not self.previewer or max_columns < layout_config.preview_cutoff then
381+
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
382+
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
383+
if has_preview then
384+
preview.height = math.floor(prompt.line - (2 + bs))
385+
else
381386
preview.height = 0
382387
end
383388

@@ -386,7 +391,7 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro
386391
preview.col = results.col
387392

388393
return {
389-
preview = self.previewer and preview.width > 0 and preview,
394+
preview = self.previewer and preview.height > 0 and preview,
390395
results = results,
391396
prompt = prompt
392397
}
@@ -416,7 +421,10 @@ end)
416421
---@eval { ["description"] = require("telescope.pickers.layout_strategies")._format("vertical") }
417422
---
418423
layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("error", shared_options, {
419-
preview_height = { "Change the height of Telescope's preview window", "See |resolver.resolve_height()|" },
424+
preview_height = { "Change the height of Telescope's preview window",
425+
"See |resolver.resolve_height()|" },
426+
preview_cutoff = {"Decides whether to display preview based on size of window.",
427+
"See |resolver.resolve_cutoff|"},
420428
}), function(self, max_columns, max_lines, layout_config)
421429

422430
local initial_options = p_window.get_initial_window_options(self)
@@ -425,24 +433,26 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("
425433
local prompt = initial_options.prompt
426434

427435
local width_opt = layout_config.width
428-
local picker_width = resolve.resolve_width(width_opt)(self,max_columns,max_lines)
436+
local picker_width = resolve.resolve_width(width_opt)(self, max_columns, max_lines)
429437
local width_padding = math.floor((max_columns - picker_width)/2)
430438

431439
local height_opt = layout_config.height
432-
local picker_height = resolve.resolve_height(height_opt)(self,max_columns,max_lines)
440+
local picker_height = resolve.resolve_height(height_opt)(self, max_columns, max_lines)
433441
local height_padding = math.floor((max_lines - picker_height)/2)
434442

435-
if not self.previewer then
436-
preview.width = 0
437-
else
443+
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
444+
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
445+
if has_preview then
438446
preview.width = picker_width
447+
else
448+
preview.width = 0
439449
end
440450
results.width = picker_width
441451
prompt.width = picker_width
442452

443453
local preview_total = 0
444454
preview.height = 0
445-
if self.previewer then
455+
if has_preview then
446456
preview.height = resolve.resolve_height(
447457
layout_config.preview_height or 0.5
448458
)(self, max_columns, picker_height)
@@ -455,7 +465,7 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("
455465

456466
results.col, preview.col, prompt.col = width_padding, width_padding, width_padding
457467

458-
if self.previewer then
468+
if has_preview then
459469
if not layout_config.mirror then
460470
preview.line = height_padding
461471
results.line = preview.line + preview.height + 2
@@ -471,7 +481,7 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("
471481
end
472482

473483
return {
474-
preview = self.previewer and preview.width > 0 and preview,
484+
preview = self.previewer and preview.height > 0 and preview,
475485
results = results,
476486
prompt = prompt
477487
}
@@ -568,7 +578,7 @@ layout_strategies.bottom_pane = make_documented_layout('bottom_pane', vim.tbl_ex
568578
local prompt = initial_options.prompt
569579
local preview = initial_options.preview
570580

571-
local result_height = resolve.resolve_height(layout_config.height)(self,max_columns,max_lines) or 25
581+
local result_height = resolve.resolve_height(layout_config.height)(self, max_columns, max_lines) or 25
572582

573583
local prompt_width = max_columns
574584
local col = 0

lua/telescope/themes.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function themes.get_dropdown(opts)
3535
sorting_strategy = "ascending",
3636
layout_strategy = "center",
3737
layout_config = {
38-
preview_cutoff = 1, -- Preview should always show (unless previewer = false)
38+
preview_cutoff = true, -- Preview should always show (unless previewer = false)
3939

4040
width = function(_, max_columns, _)
4141
return math.min(max_columns - 3, 80)

0 commit comments

Comments
 (0)