Skip to content

Commit

Permalink
Resolve preview_cutoff (first attempt)
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kershaw committed Jun 9, 2021
1 parent 28f3b27 commit 3a1ce1f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 35 deletions.
12 changes: 10 additions & 2 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,18 @@ local layout_config_defaults = {

horizontal = {
prompt_position = "bottom",
preview_cutoff = 120,
preview_cutoff = {columns = 120},
},

-- vertical = {},
vertical = {
prompt_position = "bottom",
preview_cutoff = {lines = 40},
},

center = {
preview_cutoff = {lines = 40},
height = 0.5
},
}

local layout_config_description = string.format([[
Expand Down
84 changes: 72 additions & 12 deletions lua/telescope/config/resolve.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,25 @@ That's the next step to scrolling.
local get_default = require('telescope.utils').get_default

local resolver = {}
local _resolve_map = {}
local _resolve_len = {}

-- Booleans
_resolve_map[function(val) return val == false end] = function(_, val)
_resolve_len[function(val) return val == false end] = function(_, val)
return function(...)
return val
end
end

-- Percentages
_resolve_map[function(val) return type(val) == 'number' and val >= 0 and val < 1 end] = function(selector, val)
_resolve_len[function(val) return type(val) == 'number' and val >= 0 and val < 1 end] = function(selector, val)
return function(...)
local selected = select(selector, ...)
return math.floor(val * selected)
end
end

-- Numbers
_resolve_map[function(val) return type(val) == 'number' and val >= 1 end] = function(selector, val)
_resolve_len[function(val) return type(val) == 'number' and val >= 1 end] = function(selector, val)
return function(...)
local selected = select(selector, ...)
return math.min(val, selected)
Expand All @@ -126,20 +126,20 @@ end
-- function(self, max_columns, max_lines): number
--
-- Resulting number is used for this configuration value.
_resolve_map[function(val) return type(val) == 'function' end] = function(_, val)
_resolve_len[function(val) return type(val) == 'function' end] = function(_, val)
return val
end

-- Add padding option
_resolve_map[function(val) return type(val) == 'table' and val['padding'] ~= nil end] = function(selector, val)
_resolve_len[function(val) return type(val) == 'table' and val['padding'] ~= nil end] = function(selector, val)
local resolve_pad = function(value)
for k, v in pairs(_resolve_map) do
for k, v in pairs(_resolve_len) do
if k(value) then
return v(selector, value)
end
end

error('invalid configuration option for padding:' .. tostring(value))
error('invalid configuration option for padding: ' .. tostring(value))
end

return function(...)
Expand Down Expand Up @@ -169,13 +169,13 @@ end
--- The returned function will have signature:
--- function(self, max_columns, max_lines): number
resolver.resolve_height = function(val)
for k, v in pairs(_resolve_map) do
for k, v in pairs(_resolve_len) do
if k(val) then
return v(3, val)
end
end

error('invalid configuration option for height:' .. tostring(val))
error('invalid configuration option for height: ' .. tostring(val))
end

--- Converts input to a function that returns the width.
Expand All @@ -197,13 +197,73 @@ end
--- The returned function will have signature:
--- function(self, max_columns, max_lines): number
resolver.resolve_width = function(val)
for k, v in pairs(_resolve_map) do
for k, v in pairs(_resolve_len) do
if k(val) then
return v(2, val)
end
end

error('invalid configuration option for width:' .. tostring(val))
error('invalid configuration option for width: ' .. tostring(val))
end



local _resolve_cut = {}

-- Booleans
_resolve_cut[function(val) return val == false or val == true end] = function(val)
return function()
return val
end
end

-- Columns
_resolve_cut[function(val) return type(val) == 'table' and val['columns'] ~= nil end] = function(val)
return function(_,max_columns,_)
return (val.columns <= max_columns)
end
end

-- Lines
_resolve_cut[function(val) return type(val) == 'table' and val['lines'] ~= nil end] = function(val)
return function(_,_,max_lines)
return (val.lines <= max_lines)
end
end

--- Function
_resolve_cut[function(val) return type(val) == 'function' end] = function(_, val)
return val
end

--- Converts input to a function that returns a boolean.
--- The input must take one of four forms:
--- 1. boolean:
--- The cutoff is then a function that always returns the given boolean.
--- 2. table of the form:
--- {columns = `foo`} <br>
--- where `foo` is a number >= 1. <br>
--- The cutoff is then a function that is true when `max_columns` is greater than
--- or equal to `foo` and is false otherwise.
--- 3. table of the form:
--- {lines = `foo`} <br>
--- where `foo` is a number >= 1. <br>
--- The cutoff is then a function that is true when `max_lines` is greater than
--- or equal to `foo` and is false otherwise.
--- 4. function <br>
--- Must have signature:
--- function(self, max_columns, max_lines): boolean
---
--- The returned function will have signature:
--- function(self, max_columns, max_lines): boolean
resolver.resolve_cutoff = function(val)
for k, v in pairs(_resolve_cut) do
if k(val) then
return v(val)
end
end

error('invalid configuration option for cutoff: ' .. tostring(val))
end

--- Win option always returns a table with preview, results, and prompt.
Expand Down
50 changes: 30 additions & 20 deletions lua/telescope/pickers/layout_strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ end
---
layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_extend("error", shared_options, {
preview_width = { "Change the width of Telescope's preview window", "See |resolver.resolve_width()|", },
preview_cutoff = "When columns are less than this value, the preview will be disabled",
preview_cutoff = {"Decides whether to display preview based on size of window.",
"See |resolver.resolve_cutoff|"},
prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
}), function(self, max_columns, max_lines, layout_config)
local initial_options = p_window.get_initial_window_options(self)
Expand All @@ -257,11 +258,11 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte
local picker_height = resolve.resolve_height(height_opt)(self, max_columns, max_lines)
local height_padding = math.floor((max_lines - picker_height)/2)

if self.previewer then
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
if has_preview then
preview.width = resolve.resolve_width(layout_config.preview_width or function(_, cols)
if not self.previewer or cols < layout_config.preview_cutoff then
return 0
elseif cols < 150 then
if cols < 150 then
return math.floor(cols * 0.4)
elseif cols < 200 then
return 80
Expand All @@ -279,7 +280,7 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte
prompt.height = 1
results.height = picker_height - prompt.height - 2

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

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

if not self.previewer or max_columns < layout_config.preview_cutoff then
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
if has_preview then
preview.height = math.floor(prompt.line - (2 + bs))
else
preview.height = 0
end

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

return {
preview = self.previewer and preview.width > 0 and preview,
preview = self.previewer and preview.height > 0 and preview,
results = results,
prompt = prompt
}
Expand Down Expand Up @@ -416,7 +421,10 @@ end)
---@eval { ["description"] = require("telescope.pickers.layout_strategies")._format("vertical") }
---
layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("error", shared_options, {
preview_height = { "Change the height of Telescope's preview window", "See |resolver.resolve_height()|" },
preview_height = { "Change the height of Telescope's preview window",
"See |resolver.resolve_height()|" },
preview_cutoff = {"Decides whether to display preview based on size of window.",
"See |resolver.resolve_cutoff|"},
}), function(self, max_columns, max_lines, layout_config)

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

local width_opt = layout_config.width
local picker_width = resolve.resolve_width(width_opt)(self,max_columns,max_lines)
local picker_width = resolve.resolve_width(width_opt)(self, max_columns, max_lines)
local width_padding = math.floor((max_columns - picker_width)/2)

local height_opt = layout_config.height
local picker_height = resolve.resolve_height(height_opt)(self,max_columns,max_lines)
local picker_height = resolve.resolve_height(height_opt)(self, max_columns, max_lines)
local height_padding = math.floor((max_lines - picker_height)/2)

if not self.previewer then
preview.width = 0
else
local prev_cutoff_func = resolve.resolve_cutoff(layout_config.preview_cutoff)
local has_preview = self.previewer and prev_cutoff_func(self, max_columns, max_lines)
if has_preview then
preview.width = picker_width
else
preview.width = 0
end
results.width = picker_width
prompt.width = picker_width

local preview_total = 0
preview.height = 0
if self.previewer then
if has_preview then
preview.height = resolve.resolve_height(
layout_config.preview_height or 0.5
)(self, max_columns, picker_height)
Expand All @@ -455,7 +465,7 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("

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

if self.previewer then
if has_preview then
if not layout_config.mirror then
preview.line = height_padding
results.line = preview.line + preview.height + 2
Expand All @@ -471,7 +481,7 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend("
end

return {
preview = self.previewer and preview.width > 0 and preview,
preview = self.previewer and preview.height > 0 and preview,
results = results,
prompt = prompt
}
Expand Down Expand Up @@ -568,7 +578,7 @@ layout_strategies.bottom_pane = make_documented_layout('bottom_pane', vim.tbl_ex
local prompt = initial_options.prompt
local preview = initial_options.preview

local result_height = resolve.resolve_height(layout_config.height)(self,max_columns,max_lines) or 25
local result_height = resolve.resolve_height(layout_config.height)(self, max_columns, max_lines) or 25

local prompt_width = max_columns
local col = 0
Expand Down
2 changes: 1 addition & 1 deletion lua/telescope/themes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function themes.get_dropdown(opts)
sorting_strategy = "ascending",
layout_strategy = "center",
layout_config = {
preview_cutoff = 1, -- Preview should always show (unless previewer = false)
preview_cutoff = true, -- Preview should always show (unless previewer = false)

width = function(_, max_columns, _)
return math.min(max_columns - 3, 80)
Expand Down

0 comments on commit 3a1ce1f

Please sign in to comment.