nvim-telescope / telescope.nvim

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

preview flag does not get overriden (for find_files picker) #3325

Open weisbrja opened 1 week ago

weisbrja commented 1 week ago

Description

When using

opts = {
    defaults = { preview = true },
    pickers = { find_files = { preview = false } }
}

to setup telescope, the find_files picker still shows a preview. I also tried passing preview = false directly into the find_files function, but that didn't work either.

Neovim version

NVIM v0.10.2
Build type: RelWithDebInfo
LuaJIT 2.1.1725453128

Operating system and version

Arch Linux x86_64

Telescope version / branch / rev

master

checkhealth telescope

telescope: health#telescope#check

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

Checking external dependencies ~
- OK rg: found ripgrep 14.1.1
- OK fd: found fd 10.2.0

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

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

Telescope Extension: `ui-select` ~
- No healthcheck provided

Steps to reproduce

  1. nvim -nu minimal.lua
  2. press <Space>f

Expected behavior

I expected the find_files picker to not show a preview.

Actual behavior

The find_files picker showed a preview.

Minimal config

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    {
        "nvim-telescope/telescope.nvim",
        keys = {
            {
                "<Space>f",
                function()
                    require("telescope.builtin").find_files() -- also reproducible with { preview = true } as args
                end,
            },
        },
        dependencies = { "nvim-lua/plenary.nvim" },
        opts = {
            defaults = { preview = true },
            pickers = { find_files = { preview = false } },
        },
    },
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})
weisbrja commented 1 week ago

Maybe this has something to do with inheriting the values. I read somewhere that the behavior isn't well documented yet.

jamestrew commented 1 week ago

This is a lazy.nvim usage error.

You need to put the pickers table into the opts table. I can elaborate more when I'm not on my phone but hope this helps.

weisbrja commented 1 week ago

Sorry, I made a typo in the minimal reproduction file. I meant the pickers to be in the opts table. This bug is not a configuration issue. I'll edit the issue.

weisbrja commented 1 week ago

@jamestrew I tested the minmal config on my device and it reproduced the bug. Please reopen this issue.

jamestrew commented 1 week ago

Ok I haven't had a chance to try myself but I'll try to take a look.

jamestrew commented 1 week ago

I think the above PR will fix this. If you can give it a try and confirm, that'll be helpful.

weisbrja commented 1 week ago

Tried the PR with my own config. It fixes the issue that preview couldn't be overriden from the pickers section. But I also have a custom picker which I create via require("telescope.pickers").new(opts, defaults). Even if I set preview = false in defaults and opts, the picker will still show a preview, because it probably still uses the global preview setting.

I also think that this might be a more systemic issue and that just fixing it for the preview option might not be enough. For me it's fine though :shrug:

weisbrja commented 1 week ago

I have to add that layout_strategy for example can be overridden from anywhere. So maybe it's not a systemic bug, but rather effects just preview. Just guessing though.

weisbrja commented 1 week ago

I just don't understand why preview can be overriden from anywhere, if the global default is false. Why does it's value matter?

jamestrew commented 6 days ago

I also have a custom picker which I create via require("telescope.pickers").new(opts, defaults). Even if I set preview = false in defaults and opts, the picker will still show a preview, because it probably still uses the global preview setting.

Do you have an example of this I can see? I'm trying this rudimentary find_files example I made and I'm not getting a preview as I'd expect.

vim.keymap.set("n", "<space>f", function()
  local pickers = require("telescope.pickers")
  local finders = require("telescope.finders")
  local make_entry = require("telescope.make_entry")
  local conf = require("telescope.config").values

  local files = vim.trim(vim.system({ "fd", "-tf" }):wait().stdout)
  files = vim.split(files, "\n")
  local opts = { preview = false }
  pickers
    .new(opts, {
      prompt_title = "colors",
      finder = finders.new_table({
        results = files,
        entry_maker = make_entry.gen_from_file(opts),
      }),
      previewer = conf.file_previewer(opts),
      sorter = conf.generic_sorter(opts),
    })
    :find()
end)
weisbrja commented 6 days ago

https://gitlab.com/weisbrja/dotfiles/-/blob/74cabf5158869233283b0e74c4b18fd213fe3c05/.config/nvim/lua/plugins/telescope.lua

weisbrja commented 6 days ago

I'll try to reproduce the bug in a more minimal setup and post the config here.

weisbrja commented 6 days ago

Steps to reproduce

  1. run nvim -nu minimal.lua
  2. press <Space>H.

Minimal config

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    {
        "jamestrew/telescope.nvim",
        branch = "preview-opt-inheritance",
        keys = {
            {
                -- This seems fixed.
                "<Space>f",
                function()
                    require("telescope.builtin").find_files()
                end,
            },
            -- This does not.
            "<Space>H",
        },
        dependencies = { "nvim-lua/plenary.nvim" },
        opts = {
            defaults = { preview = true },
            pickers = { find_files = { preview = false } },
        },
        config = function(_, opts)
            local telescope = require("telescope")
            telescope.setup(opts)

            local conf = require("telescope.config").values
            vim.keymap.set("n", "<Space>H", function()
                local files = vim.split(vim.trim(vim.system({ "fd", "-tf" }):wait().stdout), "\n")

                -- FIXME: global preview option still can't be overridden here
                require("telescope.pickers")
                    .new({
                        preview = false,
                    }, {
                        preview = false,
                        prompt_title = "Harpoon",
                        -- layout_strategy = "center",
                        finder = require("telescope.finders").new_table({
                            results = files,
                            -- I tried this, but it didn't change anything and I didn't need it before.
                            -- entry_maker = require("telescope.make_entry").gen_from_file({ preview = false }),
                        }),
                        previewer = conf.file_previewer({}),
                        sorter = conf.generic_sorter({}),
                    })
                    :find()
            end)
        end,
    },
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})
weisbrja commented 2 days ago

@jamestrew could you reproduce the issue with my minimal config?