From b1ec1e8544af9357ff26f4cab97f0e250435cbed Mon Sep 17 00:00:00 2001 From: Luke Kershaw <35707277+l-kershaw@users.noreply.github.com> Date: Sun, 18 Jul 2021 17:59:17 +0100 Subject: [PATCH 1/4] fix: check if tabline is present when calculating certain layouts - also tweaks height calculation for `center` strategy --- lua/telescope/pickers/layout_strategies.lua | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 43f90600fa..7869073645 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -245,11 +245,18 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte preview_cutoff = "When columns are less than this value, the preview will be disabled", 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) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt + local tbln = (vim.o.showtabline == 2) + or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) + if tbln then + max_lines = max_lines - 1 + end + local width_opt = layout_config.width local picker_width = resolve.resolve_width(width_opt)(self, max_columns, max_lines) local width_padding = math.floor((max_columns - picker_width)/2) @@ -306,6 +313,12 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte error("Unknown prompt_position: " .. tostring(self.window.prompt_position) .. "\n" .. vim.inspect(layout_config)) end + if tbln then + prompt.line = prompt.line + 1 + results.line = results.line + 1 + preview.line = preview.line + 1 + end + return { preview = self.previewer and preview.width > 0 and preview, results = results, @@ -342,11 +355,18 @@ end) layout_strategies.center = make_documented_layout("center", vim.tbl_extend("error", shared_options, { preview_cutoff = "When lines are less than this value, the preview will be disabled", }), function(self, max_columns, max_lines,layout_config) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt + local tbln = (vim.o.showtabline == 2) + or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) + if tbln then + max_lines = max_lines - 1 + end + -- This sets the width for the whole layout local width_opt = layout_config.width local width = resolve.resolve_width(width_opt)(self, max_columns, max_lines) @@ -354,6 +374,10 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro -- This sets the number of results displayed local res_height_opt = layout_config.height local res_height = resolve.resolve_height(res_height_opt)(self, max_columns, max_lines) + if type(res_height_opt) ~= "function" + and (type(res_height_opt) ~= "number" or res_height_opt < 1) then + res_height = res_height - 2 + end local max_results = (res_height > max_lines and max_lines or res_height) local max_width = (width > max_columns and max_columns or width) @@ -384,6 +408,12 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro prompt.col = results.col preview.col = results.col + if tbln then + prompt.line = prompt.line + 1 + results.line = results.line + 1 + preview.line = preview.line + 1 + end + return { preview = self.previewer and preview.height > 0 and preview, results = results, @@ -416,6 +446,7 @@ layout_strategies.cursor = make_documented_layout("cursor", vim.tbl_extend("erro 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", }), function(self, max_columns, max_lines, layout_config) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results @@ -512,11 +543,18 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend(" preview_height = { "Change the height of Telescope's preview window", "See |resolver.resolve_height()|" }, prompt_position = { "(unimplemented, but we plan on supporting)" }, }), function(self, max_columns, max_lines, layout_config) + local initial_options = p_window.get_initial_window_options(self) local preview = initial_options.preview local results = initial_options.results local prompt = initial_options.prompt + local tbln = (vim.o.showtabline == 2) + or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) + if tbln then + max_lines = max_lines - 1 + end + local width_opt = layout_config.width local picker_width = resolve.resolve_width(width_opt)(self,max_columns,max_lines) local width_padding = math.floor((max_columns - picker_width)/2) @@ -563,6 +601,12 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend(" prompt.line = results.line + results.height + 2 end + if tbln then + prompt.line = prompt.line + 1 + results.line = results.line + 1 + preview.line = preview.line + 1 + end + return { preview = self.previewer and preview.height > 0 and preview, results = results, From 3f835d59125706550a6c481a11f721c91a20d209 Mon Sep 17 00:00:00 2001 From: Luke Kershaw <35707277+l-kershaw@users.noreply.github.com> Date: Sun, 18 Jul 2021 20:53:52 +0100 Subject: [PATCH 2/4] refactor: remove `center` strategy tweak - will reimplement something similar in another PR for a few of the strategies --- lua/telescope/pickers/layout_strategies.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 7869073645..fa953cf305 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -374,10 +374,6 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro -- This sets the number of results displayed local res_height_opt = layout_config.height local res_height = resolve.resolve_height(res_height_opt)(self, max_columns, max_lines) - if type(res_height_opt) ~= "function" - and (type(res_height_opt) ~= "number" or res_height_opt < 1) then - res_height = res_height - 2 - end local max_results = (res_height > max_lines and max_lines or res_height) local max_width = (width > max_columns and max_columns or width) From 5142c7a2e85fc299514c8a45cbacbd86aeb75979 Mon Sep 17 00:00:00 2001 From: Luke Kershaw <35707277+l-kershaw@users.noreply.github.com> Date: Mon, 19 Jul 2021 21:38:11 +0100 Subject: [PATCH 3/4] fix: check if `preview.line` is `nil` --- lua/telescope/pickers/layout_strategies.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index fa953cf305..f633a3e0d4 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -323,7 +323,7 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte preview = self.previewer and preview.width > 0 and preview, results = results, prompt = prompt -} + } end) --- Centered layout with a combined block of the prompt @@ -600,7 +600,7 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend(" if tbln then prompt.line = prompt.line + 1 results.line = results.line + 1 - preview.line = preview.line + 1 + preview.line = preview.line ~= nil and preview.line + 1 end return { From c14b48f3a4cc6c26f7b50508b53ee707029021ea Mon Sep 17 00:00:00 2001 From: Luke Kershaw <35707277+l-kershaw@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:56:03 +0100 Subject: [PATCH 4/4] refactor: factor out `tbln` calculation and `max_lines` adjustment - also tweaked `vertical` calculations so that no `nil` check is required --- lua/telescope/pickers/layout_strategies.lua | 52 ++++++++++----------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index f633a3e0d4..4630a64d7c 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -46,6 +46,15 @@ local get_border_size = function(opts) return 1 end +local calc_tabline = function(max_lines) + local tbln = (vim.o.showtabline == 2) + or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) + if tbln then + max_lines = max_lines - 1 + end + return max_lines, tbln +end + local layout_strategies = {} layout_strategies._configurations = {} @@ -251,11 +260,8 @@ layout_strategies.horizontal = make_documented_layout('horizontal', vim.tbl_exte local results = initial_options.results local prompt = initial_options.prompt - local tbln = (vim.o.showtabline == 2) - or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) - if tbln then - max_lines = max_lines - 1 - end + local tbln + max_lines, tbln = calc_tabline(max_lines) local width_opt = layout_config.width local picker_width = resolve.resolve_width(width_opt)(self, max_columns, max_lines) @@ -361,11 +367,8 @@ layout_strategies.center = make_documented_layout("center", vim.tbl_extend("erro local results = initial_options.results local prompt = initial_options.prompt - local tbln = (vim.o.showtabline == 2) - or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) - if tbln then - max_lines = max_lines - 1 - end + local tbln + max_lines, tbln = calc_tabline(max_lines) -- This sets the width for the whole layout local width_opt = layout_config.width @@ -545,11 +548,8 @@ layout_strategies.vertical = make_documented_layout("vertical", vim.tbl_extend(" local results = initial_options.results local prompt = initial_options.prompt - local tbln = (vim.o.showtabline == 2) - or (vim.o.showtabline == 1 and #vim.api.nvim_list_tabpages() > 1) - if tbln then - max_lines = max_lines - 1 - end + local tbln + max_lines, tbln = calc_tabline(max_lines) local width_opt = layout_config.width local picker_width = resolve.resolve_width(width_opt)(self,max_columns,max_lines) @@ -582,25 +582,21 @@ 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 not layout_config.mirror then - preview.line = height_padding - results.line = preview.line + preview.height + 2 - prompt.line = results.line + results.height + 2 - else - prompt.line = height_padding - results.line = prompt.line + prompt.height + 2 - preview.line = results.line + results.height + 2 - end - else - results.line = height_padding + if not layout_config.mirror then + preview.line = height_padding + results.line = (preview.height == 0) and preview.line + or preview.line + preview.height + 2 prompt.line = results.line + results.height + 2 + else + prompt.line = height_padding + results.line = prompt.line + prompt.height + 2 + preview.line = results.line + results.height + 2 end if tbln then prompt.line = prompt.line + 1 results.line = results.line + 1 - preview.line = preview.line ~= nil and preview.line + 1 + preview.line = preview.line + 1 end return {