telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

File opened with Telescope: find_files can't be folded (E490: No fold found)

Open JoseConseco opened this issue 3 years ago • 16 comments

Description

Opening file with Telescope: find_files - makes it so that opened file can be folded. I use TreeSitter for folding :

vim.wo.foldmethod="expr"
vim.o.foldexpr="nvim_treesitter#foldexpr()"  
vim.wo.foldenable=false -- can be enabled directly in opened file - using 'zi' - toogle fold

The error when I try to fold - E490: No fold found Expected Behavior Opened files using :e File.py , or NvimTree can be folded ok.

Actual Behavior The error when I try to fold - E490: No fold found I can fix it by reloading file- :e %

Details

Steps to reproduce

  • : Telescope find_files
  • open file
  • use hotkey: zc (close fold) - in my case I have to use 'zi' hotkey to enable folding first.

My basic fold setup:

vim.wo.foldmethod="expr"
vim.o.foldexpr="nvim_treesitter#foldexpr()"  
vim.wo.foldenable=false -- can be enabled directly in opened file - using 'zi' - toogle fold

Treesitter:

local ts_config = require("nvim-treesitter.configs")

ts_config.setup {
    ensure_installed = {
        "html",
        "css",
        "bash",
        "python",
        "lua"
    },
    highlight = {
        enable = true,
        use_languagetree = true
    },
	indent = {enable = true},
    -- playground = {
    --     enable = true,
    --     disable = {},
    --     updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
    --     persist_queries = false -- Whether the query persists across vim sessions
    -- },
    autotag = {enable = true},
    rainbow = {enable = true},
} 

and Telescope:

require("telescope").setup {
    defaults = {
        vimgrep_arguments = {
            "rg",
            "--color=never",
            "--no-heading",
            "--with-filename",
            "--line-number",
            "--column",
            "--smart-case"
        },
        prompt_position = "bottom",
        prompt_prefix = " ",
        selection_caret = " ",
        entry_prefix = "  ",
        initial_mode = "insert",
        selection_strategy = "reset",
        sorting_strategy = "descending",
        layout_strategy = "horizontal",
        layout_defaults = {
            horizontal = {
                mirror = false,
                preview_width = 0.5
            },
            vertical = {
                mirror = false
            }
        },
        file_sorter = require "telescope.sorters".get_fuzzy_file,
        file_ignore_patterns = {},
        generic_sorter = require "telescope.sorters".get_generic_fuzzy_sorter,
        shorten_path = true,
        winblend = 0,
        width = 0.75,
        preview_cutoff = 120,
        results_height = 1,
        results_width = 0.8,
        border = {},
        borderchars = {"─", "│", "─", "│", "╭", "╮", "╯", "╰"},
        color_devicons = true,
        use_less = true,
        set_env = {["COLORTERM"] = "truecolor"}, -- default = nil,
        file_previewer = require "telescope.previewers".vim_buffer_cat.new,
        grep_previewer = require "telescope.previewers".vim_buffer_vimgrep.new,
        qflist_previewer = require "telescope.previewers".vim_buffer_qflist.new,
        -- Developer configurations: Not meant for general override
        buffer_previewer_maker = require "telescope.previewers".buffer_previewer_maker
    },
    extensions = {
        media_files = {
            filetypes = {"png", "webp", "jpg", "jpeg"},
            find_cmd = "rg" -- find command (defaults to `fd`)
        }
    }
}

require("telescope").load_extension("media_files")

local opt = {noremap = true, silent = true}

vim.g.mapleader = " "

-- mappings
vim.api.nvim_set_keymap("n", "<Leader>ff", [[<Cmd>lua require('telescope.builtin').find_files()<CR>]], opt)
vim.api.nvim_set_keymap( "n", "<Leader>fp", [[<Cmd>lua require('telescope').extensions.media_files.media_files()<CR>]], opt)
vim.api.nvim_set_keymap("n", "<Leader>fb", [[<Cmd>lua require('telescope.builtin').buffers()<CR>]], opt)
vim.api.nvim_set_keymap("n", "<Leader>fh", [[<Cmd>lua require('telescope.builtin').help_tags()<CR>]], opt)
vim.api.nvim_set_keymap("n", "<Leader>fo", [[<Cmd>lua require('telescope.builtin').oldfiles()<CR>]], opt)
vim.api.nvim_set_keymap("n", "<Leader>fm", [[<Cmd> Neoformat<CR>]], opt)

JoseConseco avatar Mar 29 '21 22:03 JoseConseco

Kinda dup of https://github.com/nvim-telescope/telescope.nvim/issues/559. tldr: I don't know why its not working. Maybe i should take a look at NvimTree

Conni2461 avatar Mar 30 '21 09:03 Conni2461

I can attest that this only seems to happen in long files.

pernambucano avatar May 24 '21 17:05 pernambucano

I'm having this issue too. Seems to happen on any file that I open via Telescope, regardless of file size. Opening via any other means seems fine.

asgeo1 avatar Jul 02 '21 04:07 asgeo1

Can confirm @asgeo1 's statement. It seems that this only happens when you open a file through telescope, I tried opening a file directly and also with defx, and they can be folded immediately. It actually also works when you open it from telescope, but it takes a really long time for it to take effect

stevenhansel avatar Aug 08 '21 09:08 stevenhansel

I'm also experiencing the issue with Telescope's live grep. I get E490: No fold found until I edit the file in some way. Opening via NvimTree folding works immediately without edits.

I haven't experienced it working after a really long time though, it seems to consistently not work for me until an edit.

Linell avatar Aug 10 '21 17:08 Linell

I'm having this problem too. Since I only really open files with Telescope, I assumed this was a problem with treesitter for the longest time. If I open a file directly, folding works right away.

~I found a workaround that seems to work though:~

-- Recompute folds after opening a Python file, works around this Telescope
-- bug: https://github.com/nvim-telescope/telescope.nvim/issues/699
vim.cmd [[ au BufEnter *.py :norm zX<CR> ]]

EDIT: Never mind, this works when opening a file, but also resets the folds every time the buffer is focused. I'm still looking for a good workaround. For now I just have to press zx every time I open a file.

Hubro avatar Sep 30 '21 08:09 Hubro

On my side, when opening files from Telescope I don't get any error messages about folds but I must reload the file with :e to make the folds collapsed.

Besides, I have this weird behaviour of folds being closed upon random edits, like this post.

I don't get any of these problems when opening files from nvim-tree.

aleprovencio avatar Jan 04 '22 17:01 aleprovencio

Someone found a fix for this issue a couple of days ago here https://github.com/nvim-telescope/telescope.nvim/issues/559#issuecomment-1002803413 and has proposed a PR which fixes the issue for me: https://github.com/nvim-telescope/telescope.nvim/pull/1643

Conni2461 avatar Jan 05 '22 22:01 Conni2461

It also works for me @Conni2461, that's awesome

aleprovencio avatar Jan 06 '22 11:01 aleprovencio

I had to revert the PR :(

Conni2461 avatar Jan 09 '22 20:01 Conni2461

Any progress on this? I am developing a plugin (opener.nvim) with a custom telescope extension, and I have had similar problems with telescope. Folding does not work if I restore a session inside an action.

attach_mappings = function(_,_)
    actions.select_default:replace(action)
    return true
end,
...
local function action(prompt_bufnr)
    local selection = action_state.get_selected_entry()
    if selection then
        actions.close(prompt_bufnr)
        local dir = selection[1];
        -- restore session
    end
end

However, as a workaround I did the following.

local function action(prompt_bufnr)
    local selection = action_state.get_selected_entry()
    if selection then
        actions.close(prompt_bufnr)
        local dir = selection[1];
        vim.defer_fn(function ()
            -- restore session
        end, 100)
    end
end

And it seems to magically work. I am no telescope-expert but it seems that what you do in an action handler is done while telescope is running (this makes sense), however actions.close(prompt_bufnr) does not actually close telescope immediately, which is not very intuitive. I think it might fix this if actions.close(prompt_bufnr) actually closed telescope immediatly. This is tricky though since I assume telescope is calling the action-handler.

willthbill avatar Feb 12 '22 11:02 willthbill

There is 'zx' hotkey in vim that is recalculating folds on current document. Maybye all there is to it - is to add this command to file opener? http://vimdoc.sourceforge.net/htmldoc/fold.html#zx

JoseConseco avatar Feb 12 '22 16:02 JoseConseco

I'm having the same problem. Along with Telescope I use Harpoon too. When I open a file using Harpoon, it works as it should. It has folding turned on and working, it also correct restores my view using loadview.

The zx works for me turn on folding again after opening a file using Telescope, but it loses my folds from a previous session.

KiLLeRRaT avatar Feb 25 '22 02:02 KiLLeRRaT

Well, I think you guys can try this, until this issue is fixed:

vim.api.nvim_create_autocmd({ "BufEnter" }, {
    pattern = { "*" },
    command = "normal zx",
})

askfiy avatar Jun 19 '22 07:06 askfiy

@askfiy unfortunately this causes problems on whichkey on my config upon hitting the leader key:

E5108: Error executing lua .../pack/packer/start/which-key.nvim/lua/which-key/view.lua:52: Window was closed immediately
stack traceback:
        [C]: in function 'nvim_open_win'
        .../pack/packer/start/which-key.nvim/lua/which-key/view.lua:52: in function 'show'
        .../pack/packer/start/which-key.nvim/lua/which-key/view.lua:262: in function 'on_keys'
        .../pack/packer/start/which-key.nvim/lua/which-key/view.lua:226: in function 'open'
        .../pack/packer/start/which-key.nvim/lua/which-key/init.lua:48: in function 'show'
        [string ":lua"]:1: in main chunk

aleprovencio avatar Jun 20 '22 13:06 aleprovencio

@aleprovencio This is actually a bug in which-key itself, not related to the autocommand above

273

I also gave a fix in this question

askfiy avatar Jun 20 '22 16:06 askfiy

Well, I think you guys can try this, until this issue is fixed:

vim.api.nvim_create_autocmd({ "BufEnter" }, {
    pattern = { "*" },
    command = "normal zx",
})

Toggling nvimtree causes terminal to flicker while using this.

aasutossh avatar Dec 24 '22 14:12 aasutossh

Well, I think you guys can try this, until this issue is fixed:

vim.api.nvim_create_autocmd({ "BufEnter" }, {
    pattern = { "*" },
    command = "normal zx",
})

Toggling nvimtree causes terminal to flicker while using this.

He is normal for me ..

askfiy avatar Dec 24 '22 14:12 askfiy

https://user-images.githubusercontent.com/18498932/209440290-5f384eeb-340b-4673-a6d2-42e44c8989de.mov

@askfiy check it in action.

aasutossh avatar Dec 24 '22 14:12 aasutossh

Peek 2022-12-26 11-04

@aasutossh i terminal is st, i use arch linux

askfiy avatar Dec 26 '22 03:12 askfiy

I tried alacritty too, same issue persists. My OS is MacOS.

On Mon, Dec 26, 2022, 8:51 AM askfiy @.***> wrote:

[image: Peek 2022-12-26 11-04] https://user-images.githubusercontent.com/81478335/209494549-78ff02b0-fee0-4a0f-a6fd-23d17c9c8d62.gif

@aasutossh https://github.com/aasutossh i terminal is st, i use arch linux

— Reply to this email directly, view it on GitHub https://github.com/nvim-telescope/telescope.nvim/issues/699#issuecomment-1364835350, or unsubscribe https://github.com/notifications/unsubscribe-auth/AENEK5GGWPVM5IZAUGTMSHDWPEDUVANCNFSM42ARZFHQ . You are receiving this because you were mentioned.Message ID: @.***>

aasutossh avatar Dec 26 '22 03:12 aasutossh

iTerm2 mainly.

aasutossh avatar Dec 26 '22 03:12 aasutossh

iTerm2为主。

Whether I use alacritty or st, this problem does not occur..

askfiy avatar Dec 26 '22 03:12 askfiy

Having the same issue as OP. Reproduces the same. Using 'zx' fixes the main issue, at the cost of folding every fold in the file, everytime i open a file with find_file() or telescope file browser.

jaredpmurphy avatar Jan 28 '23 04:01 jaredpmurphy

@jaredpmurphy Then try this:

vim.api.nvim_create_autocmd({ "BufEnter" }, {
    pattern = { "*" },
    command = "normal zx zR",
})

jcjgraf avatar Feb 28 '23 21:02 jcjgraf

Another workaround for the people who were having issues with the zx solution:

vim.api.nvim_create_autocmd({ "BufEnter", "BufNew", "BufWinEnter"  }, {
  group = vim.api.nvim_create_augroup("ts_fold_workaround", { clear = true }),
  command = "set foldexpr=nvim_treesitter#foldexpr()",
})

benelan avatar Jun 02 '23 01:06 benelan

Telescope doesn't seem to trigger any of the BufEnter, BufNew, or BufWinEnter events. If you jump from another buffer it works though.

yads avatar Jun 20 '23 00:06 yads

I was able to use folds on my end for the first time in months, even with files open through telescope. I had to modify my config with the setting given here to use treesitter for folding expressions: https://neovim.io/doc/user/treesitter.html#vim.treesitter.foldexpr(). I have also updated to [email protected] and updated all my plugins.

Nargonath avatar Jun 29 '23 21:06 Nargonath

I'm currently using this work-around in LazyVim:

vim.api.nvim_create_autocmd("BufEnter", {
  callback = function()
    if vim.opt.foldmethod:get() == "expr" then
      vim.schedule(function()
        vim.opt.foldmethod = "expr"
      end)
    end
  end,
})

folke avatar Oct 03 '23 16:10 folke

Actually let me take back what I think in my previous comment. It worked for a few days but stopped working again. Thanks @folke your solution worked for me as well.

Nargonath avatar Oct 03 '23 17:10 Nargonath