nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.25k stars 815 forks source link

Odd default behavior with flex layout when resizing horizontally #3138

Closed ronandalton closed 2 months ago

ronandalton commented 3 months ago

Description

When using the flex layout with default settings, odd behavior is observed when resizing the window horizontally. The preview pane is hidden at certain widths.

Neovim version

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713484068

Operating system and version

Linux 6.8.1

Telescope version / branch / rev

master (c2ce039)

checkhealth telescope

Checking for required plugins ~
- OK plenary installed.
- WARNING nvim-treesitter not found. (Required for `:Telescope treesitter`.)

Checking external dependencies ~
- OK rg: found ripgrep 14.1.0
- OK fd: found fd 9.0.0

===== Installed extensions ===== ~

Telescope Extension: `fzf` ~
- OK lib working as expected
- OK file_sorter correctly configured
- OK generic_sorter correctly configured

Steps to reproduce

  1. nvim -nu minimal.lua
  2. Resize Neovim window so it's at least 130x50 characters
  3. Execute :Telescope live_grep
  4. Start reducing the width of the window gradually

Expected behavior

The preview pane should remain visible while resizing horizontally. The layout should flip to vertical once the width is reduced below 120.

Actual behavior

There is a period while resizing horizontally where the preview pane is not visible. This happens between widths of 120 and 100. When the window width is reduced below 100, the preview appears again in a vertical layout. This doesn't make sense because if you started with a vertical layout, you wouldn't expect increasing the width of the window to show less of the UI.

Video: https://github.com/nvim-telescope/telescope.nvim/assets/86718942/f8e272a0-b719-4752-9adc-fa08f085c099

Analysis

The reason this happens is because of a mismatch between the default flip_columns and flip_lines values (which are 100 and 20) for the flex layout and the preview_cutoff values for the horizontal and vertical layouts (which are 120 and 40). When the window width is increased to 100, the horizontal layout is switched to, but the horizontal layout won't display a preview pane because the window width is below the preview cutoff for the horizontal layout.

My suggested change would be to set the default flip_columns and flip_lines values in the flex layout code to 120 and 40 respectively. This would ensure the vertical layout is only switched away from when the window is wide enough for the horizontal layout to display correctly. Alternatively, the default clip_columns and flip_lines values could be taken from the preview_cutoff values for the horizontal and vertical layouts automatically.

Note that the desired behavior can be achieved just by adding the following code to telescope's config, but as I consider the default behavior a bit strange, I thought it might be better to have that changed so this config isn't needed.

        defaults = {
          layout_strategy = 'flex',
          layout_config = {
            flex = {
              flip_columns = 120,
              flip_lines = 40,
            },
          },
        },

Minimal config

vim.cmd([[set runtimepath=$VIMRUNTIME]])
vim.cmd([[set packpath=/tmp/nvim/site]])
local package_root = "/tmp/nvim/site/pack"
local install_path = package_root .. "/packer/start/packer.nvim"
local function load_plugins()
    require("packer").startup({
        {
            "wbthomason/packer.nvim",
            {
                "nvim-telescope/telescope.nvim",
                requires = {
                    "nvim-lua/plenary.nvim",
                    { "nvim-telescope/telescope-fzf-native.nvim", run = "make" },
                },
            },
        },
        config = {
            package_root = package_root,
            compile_path = install_path .. "/plugin/packer_compiled.lua",
            display = { non_interactive = true },
        },
    })
end
_G.load_config = function()
    require("telescope").setup({ defaults = { layout_strategy = "flex" } })
    require("telescope").load_extension("fzf")
end
if vim.fn.isdirectory(install_path) == 0 then
    print("Installing Telescope and dependencies.")
    vim.fn.system({ "git", "clone", "--depth=1", "https://github.com/wbthomason/packer.nvim", install_path })
end
load_plugins()
require("packer").sync()
vim.cmd([[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]])
ronandalton commented 2 months ago

I created a PR that fixes this issue. I went with my alternate suggestion of setting the default clip_columns and flip_lines values to the preview_cutoff values for the vertical and horizontal layouts. This should be more robust since if those values change, these values won't get out of sync again. It also means less configuration is needed if a user changes their default preview_cutoff values.