nvim-telescope / telescope-file-browser.nvim

File Browser extension for telescope.nvim
MIT License
1.68k stars 92 forks source link

Bug when using collapse_dirs set to true #348

Closed rogeriofrsouza closed 9 months ago

rogeriofrsouza commented 9 months ago

Description

I did setup collapse_dirs to true and an annoying bug is happening every time I open a directory with a single file inside.

I am currently using LazyVim 10.8.2 (2023-11-30) with telescope and telescope-file-browser plugins updated to the last version.

My telescope.lua is:

return {
  {
    "nvim-telescope/telescope.nvim",
    opts = {
      extensions = {
        file_browser = {
          collapse_dirs = true,
        },
      },
    },
    keys = {
      { "<leader>fb", false },
      { "<leader>fd", "<cmd>Telescope buffers sort_mru=true sort_lastused=true<cr>", desc = "Buffers" },
    },
  },
}

And telescope-file-browser.lua:

return {
  {
    "nvim-telescope/telescope-file-browser.nvim",
    dependencies = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" },
    keys = {
      {
        "<space>fb",
        ":Telescope file_browser<CR>",
        desc = "File Browser (root dir)",
        { noremap = true },
      },
      {
        "<space>fB",
        ":Telescope file_browser path=%:p:h select_buffer=true<CR>",
        desc = "File Browser (cwd)",
        { noremap = true },
      },
    },
  },
}

Neovim version

NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1702233742

Operating system and version

Manjaro Linux x86_64 - 6.1.69-1-MANJARO

Steps to reproduce

  1. nvim -nu minimal.lua
  2. :Telescope file_browser
  3. Open a directory which contains a single file

Expected behavior

Open the selected directory and show the contained file without crashing the plugin.

Actual behavior

This is the actual behavior, it shows this error output message and crashes the plugin. image

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-file-browser.nvim',
        },
      },
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    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 = {
        collapse_dirs = true,
      }
    }
  }
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()]]
jamestrew commented 9 months ago

Thanks for reporting, I can replicate this. Let me take a look.

jamestrew commented 9 months ago

After a little bit of digging, it looks like this bug existed ever since this option was added. Probably says something about how few people use it :smile:.

What do you think is the expected behavior? Simply open the file directly or open the folder regularly (option to only collapse directories to the last directory)?

I'm leaning collapsing only directory (option B) since I think that falls closer in line with the name of the option.

In hindsight, I'm not sure this option should really exist though. Seems useful, until you want to create a file/folder in one of the child folders that were collapse. Then suddenly you need to relaunch telescope disabling collapse_dirs or use another file browser.

rogeriofrsouza commented 9 months ago

I think it should open the folder regularly, this avoids some key presses when browsing in a Java project from root directory 😄

However, how you mentioned, it seems useful while browsing but would make it harder to manage files/directories directly. I've been using another plugin for that!

I agree with removing this option but I'm not certain if this may be useful in another situation or a different kind of project.

jamestrew commented 9 months ago

I've been going back and forth about keeping this option. Considering we have the folder browser (<C-f> by default after :Telescope file_browser, or :Telescope file_browser files=false), it feels redundant since that let's you open any child directory directly.

I'm leaning on deprecating it eventually. But for now, I've put in a fix for this bug making it so it opens folders regularly if it only has one file.