feat: more prompt position strategies (#1280)

* feat: allow `prompt_position` for `vertical` layout strategy

* feat: allow `prompt_position` for `bottom_pane` layout strategy

* stylua

* [docgen] Update doc/telescope.txt
skip-checks: true

* refactor: switch to `string.format`

* stylua

* feat: allow `prompt_position` for `center` layout strategy

* feat: handle user defined `prompt_position` within themes

* [docgen] Update doc/telescope.txt
skip-checks: true

* fix: tweak `center` layout

- ensure `prompt` title is visible when `prompt_position="bottom"`

* fix: refactor `center` tweak

- move title to bottom of picker when `prompt_position="bottom"`

* fix: tweak `bottom_pane` layout

* stylua

Co-authored-by: Github Actions <actions@github>
This commit is contained in:
Luke Kershaw
2021-10-04 20:16:58 +01:00
committed by GitHub
parent e39ed31f17
commit be600b5421
4 changed files with 79 additions and 19 deletions

View File

@@ -114,11 +114,13 @@ telescope.setup({opts}) *telescope.setup()*
Default: { Default: {
bottom_pane = { bottom_pane = {
height = 25 height = 25,
prompt_position = "top"
}, },
center = { center = {
height = 0.9, height = 0.9,
preview_cutoff = 40, preview_cutoff = 40,
prompt_position = "top",
width = 0.8 width = 0.8
}, },
cursor = { cursor = {
@@ -135,6 +137,7 @@ telescope.setup({opts}) *telescope.setup()*
vertical = { vertical = {
height = 0.9, height = 0.9,
preview_cutoff = 40, preview_cutoff = 40,
prompt_position = "bottom",
width = 0.8 width = 0.8
} }
} }
@@ -1679,7 +1682,8 @@ layout_strategies.vertical() *layout_strategies.vertical()*
- Change the height of Telescope's preview window - Change the height of Telescope's preview window
- See |resolver.resolve_height()| - See |resolver.resolve_height()|
- prompt_position: - prompt_position:
- (unimplemented, but we plan on supporting) - Where to place prompt window.
- Available Values: 'bottom', 'top'
layout_strategies.flex() *layout_strategies.flex()* layout_strategies.flex() *layout_strategies.flex()*

View File

@@ -87,6 +87,7 @@ local layout_config_defaults = {
vertical = { vertical = {
width = 0.8, width = 0.8,
height = 0.9, height = 0.9,
prompt_position = "bottom",
preview_cutoff = 40, preview_cutoff = 40,
}, },
@@ -94,6 +95,7 @@ local layout_config_defaults = {
width = 0.8, width = 0.8,
height = 0.9, height = 0.9,
preview_cutoff = 40, preview_cutoff = 40,
prompt_position = "top",
}, },
cursor = { cursor = {
@@ -104,6 +106,7 @@ local layout_config_defaults = {
bottom_pane = { bottom_pane = {
height = 25, height = 25,
prompt_position = "top",
}, },
} }

View File

@@ -351,7 +351,7 @@ layout_strategies.horizontal = make_documented_layout(
results.line = preview.line results.line = preview.line
prompt.line = results.line + results.height + 1 + bs prompt.line = results.line + results.height + 1 + bs
else else
error("Unknown prompt_position: " .. tostring(self.window.prompt_position) .. "\n" .. vim.inspect(layout_config)) error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
end end
if tbln then if tbln then
@@ -433,15 +433,26 @@ layout_strategies.center = make_documented_layout(
prompt.height = 1 prompt.height = 1
results.height = height - prompt.height - h_space results.height = height - prompt.height - h_space
local topline = (max_lines / 2) - ((results.height + (2 * bs)) / 2) + 1
-- Align the prompt and results so halfway up the screen is -- Align the prompt and results so halfway up the screen is
-- in the middle of this combined block -- in the middle of this combined block
prompt.line = (max_lines / 2) - ((results.height + (2 * bs)) / 2) + 1 if layout_config.prompt_position == "top" then
results.line = prompt.line + 1 + bs prompt.line = topline
results.line = prompt.line + 1 + bs
elseif layout_config.prompt_position == "bottom" then
results.line = topline
prompt.line = results.line + results.height + bs
if type(prompt.title) == "string" then
prompt.title = { { pos = "S", text = prompt.title } }
end
else
error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
end
preview.line = 2 preview.line = 2
if self.previewer and max_lines >= layout_config.preview_cutoff then if self.previewer and max_lines >= layout_config.preview_cutoff then
preview.height = math.floor(prompt.line - (3 + bs)) preview.height = math.floor(topline - (3 + bs))
else else
preview.height = 0 preview.height = 0
end end
@@ -594,7 +605,7 @@ layout_strategies.vertical = make_documented_layout(
vim.tbl_extend("error", shared_options, { vim.tbl_extend("error", shared_options, {
preview_cutoff = "When lines are less than this value, the preview will be disabled", preview_cutoff = "When lines are less than this value, the preview will be disabled",
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()|" },
prompt_position = { "(unimplemented, but we plan on supporting)" }, prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
}), }),
function(self, max_columns, max_lines, layout_config) function(self, max_columns, max_lines, layout_config)
local initial_options = p_window.get_initial_window_options(self) local initial_options = p_window.get_initial_window_options(self)
@@ -640,13 +651,28 @@ layout_strategies.vertical = make_documented_layout(
local height_padding = math.floor((max_lines - height) / 2) local height_padding = math.floor((max_lines - height) / 2)
if not layout_config.mirror then if not layout_config.mirror then
preview.line = height_padding + bs + 1 preview.line = height_padding + (1 + bs)
results.line = (preview.height == 0) and preview.line or preview.line + preview.height + (1 + bs) if layout_config.prompt_position == "top" then
prompt.line = results.line + results.height + (1 + bs) prompt.line = (preview.height == 0) and preview.line or preview.line + preview.height + (1 + bs)
results.line = prompt.line + prompt.height + (1 + bs)
elseif layout_config.prompt_position == "bottom" then
results.line = (preview.height == 0) and preview.line or preview.line + preview.height + (1 + bs)
prompt.line = results.line + results.height + (1 + bs)
else
error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
end
else else
prompt.line = height_padding + bs + 1 if layout_config.prompt_position == "top" then
results.line = prompt.line + prompt.height + (1 + bs) prompt.line = height_padding + (1 + bs)
preview.line = results.line + results.height + (1 + bs) results.line = prompt.line + prompt.height + (1 + bs)
preview.line = results.line + results.height + (1 + bs)
elseif layout_config.prompt_position == "bottom" then
results.line = height_padding + (1 + bs)
prompt.line = results.line + results.height + (1 + bs)
preview.line = prompt.line + prompt.height + (1 + bs)
else
error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config)))
end
end end
if tbln then if tbln then
@@ -750,7 +776,7 @@ end)
layout_strategies.bottom_pane = make_documented_layout( layout_strategies.bottom_pane = make_documented_layout(
"bottom_pane", "bottom_pane",
vim.tbl_extend("error", shared_options, { vim.tbl_extend("error", shared_options, {
-- No custom options... prompt_position = { "Where to place prompt window.", "Available Values: 'bottom', 'top'" },
}), }),
function(self, max_columns, max_lines, layout_config) function(self, max_columns, max_lines, layout_config)
local initial_options = p_window.get_initial_window_options(self) local initial_options = p_window.get_initial_window_options(self)
@@ -791,9 +817,20 @@ layout_strategies.bottom_pane = make_documented_layout(
end end
-- Line -- Line
prompt.line = max_lines - results.height - (1 + bs) + 1 if layout_config.prompt_position == "top" then
results.line = prompt.line + 1 prompt.line = max_lines - results.height - (1 + bs) + 1
preview.line = results.line + bs results.line = prompt.line + 1
preview.line = results.line + bs
elseif layout_config.prompt_position == "bottom" then
results.line = max_lines - results.height - (1 + bs) + 1
preview.line = results.line
prompt.line = max_lines - bs
if type(prompt.title) == "string" then
prompt.title = { { pos = "S", text = prompt.title } }
end
else
error("Unknown prompt_position: " .. tostring(self.window.prompt_position) .. "\n" .. vim.inspect(layout_config))
end
-- Col -- Col
prompt.col = 0 -- centered prompt.col = 0 -- centered

View File

@@ -52,6 +52,13 @@ function themes.get_dropdown(opts)
preview = { "", "", "", "", "", "", "", "" }, preview = { "", "", "", "", "", "", "", "" },
}, },
} }
if opts.layout_config and opts.layout_config.prompt_position == "bottom" then
theme_opts.borderchars = {
prompt = { "", "", "", "", "", "", "", "" },
results = { "", "", "", "", "", "", "", "" },
preview = { "", "", "", "", "", "", "", "" },
}
end
return vim.tbl_deep_extend("force", theme_opts, opts) return vim.tbl_deep_extend("force", theme_opts, opts)
end end
@@ -99,7 +106,7 @@ end
function themes.get_ivy(opts) function themes.get_ivy(opts)
opts = opts or {} opts = opts or {}
return vim.tbl_deep_extend("force", { local theme_opts = {
theme = "ivy", theme = "ivy",
sorting_strategy = "ascending", sorting_strategy = "ascending",
@@ -117,7 +124,16 @@ function themes.get_ivy(opts)
results = { " " }, results = { " " },
preview = { "", "", "", "", "", "", "", "" }, preview = { "", "", "", "", "", "", "", "" },
}, },
}, opts) }
if opts.layout_config and opts.layout_config.prompt_position == "bottom" then
theme_opts.borderchars = {
prompt = { " ", " ", "", " ", " ", " ", "", "" },
results = { "", " ", " ", " ", "", "", " ", " " },
preview = { "", " ", "", "", "", "", "", "" },
}
end
return vim.tbl_deep_extend("force", theme_opts, opts)
end end
return themes return themes