nvim-telescope / telescope.nvim

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

'line' is out of range for buffer previewer when previewer height is 2 or less #3001

Closed TanglingTreats closed 7 months ago

TanglingTreats commented 7 months ago

Description

An exception is thrown for buffer previewer when encountering unsupported file types e.g. binary files if buffer previewer window is two lines or less

Neovim version

NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1710088188

Operating system and version

macOS 14.4

Telescope version / branch / rev

0.1.6

checkhealth telescope

==============================================================================
telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

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

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

Telescope Extension: `file_browser` ~
- No healthcheck provided

Telescope Extension: `mapper` ~
- No healthcheck provided

Steps to reproduce

  1. Configure height of file browser with the dropdown theme to open such that resulting previewer height is 2 lines tall
  2. Go to a directory with non-text files such as images, icons

Expected behavior

Buffer previewer should display 'Binary cannot be previewed'

Actual behavior

Screenshot 2024-03-22 at 22 41 03 Screenshot 2024-03-22 at 22 11 49

Error is thrown while message do not fit within the previewer

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' },
        },
      },
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
      {
        'nvim-telescope/telescope-file-browser.nvim',
        dependencies = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" } 
      },
      { 'nvim-treesitter/nvim-treesitter',
        run = function()
          local ts_update = require('nvim-treesitter.install').update({ with_sync = true })
          ts_update()
        end,
      }
    },
    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({
    extensions = {
      file_browser = {
        initial_mode = "normal",
        theme = "dropdown",
        hijack_netrw = true,
      }
    }
  })
  require('telescope').load_extension('fzf')
  -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
  require('telescope').load_extension('file_browser')
  local opts = { noremap = true, silent = true }
  function telescope_buffer_dir()
    return vim.fn.expand('%:p:h')
  end
  vim.keymap.set('n', '<leader>e',
    function() 
      require('telescope').extensions.file_browser.file_browser({
        path = "%:p:h",
        cwd = telescope_buffer_dir(),
        respect_git_ignore = false,
        hidden = true,
        grouped = true,
        previewer = true,
        layout_config={height=40} -- height needs to be modified such that previewer is 2 lines tall
      }) end,
    opts)
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()]]
TanglingTreats commented 7 months ago

Quick fix for this issue is to check if calculated line position is less than buffer previewer height. If true, call set_extmark.

  local col = math.floor((width - strings.strdisplaywidth(lines[2])) / 2)
  for i, line in ipairs(lines) do
    local line_pos = math.floor(height / 2) - 1 + i 
    if line_pos <= height 
    then
      vim.api.nvim_buf_set_extmark(
        bufnr,
        anon_ns,
        math.floor(height / 2) - 1 + i,
        0,
        { virt_text = { { line, "TelescopePreviewMessage" } }, virt_text_pos = "overlay", virt_text_win_col = col }
      )
    end
  end

Another potential solution is to set preview message at line 0 if height <= 2, else execute loop for lines