Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E490: no fold found with tree-sitter #28692

Open
adrian5 opened this issue May 10, 2024 · 8 comments
Open

E490: no fold found with tree-sitter #28692

adrian5 opened this issue May 10, 2024 · 8 comments
Labels
bug issues reporting wrong behavior folds treesitter

Comments

@adrian5
Copy link
Contributor

adrian5 commented May 10, 2024

Problem

Tree-sitter computed folds don't work on files opened through commands. This breaks session restore functionality.

Related:

Steps to reproduce

minimal.lua:

for name, url in pairs {
  ts = "https://github.com/nvim-treesitter/nvim-treesitter/",
} do
  local install_path = vim.fn.fnamemodify('nvim_issue/' .. name, ':p')
  if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
  end
  vim.opt.runtimepath:append(install_path)
end

require("nvim-treesitter.configs").setup({
  ensure_installed = { "c" }
})

vim.opt.foldmethod = "expr"
vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.opt.foldlevelstart = 99

vim.cmd("e hello-world.c")
vim.cmd("setf c")

helloworld.c:

#include <stdio.h>

int main() {
    printf("Hello, world!");
    return 0;
}

Steps:

  • nvim --clean -u minimal.lua
  • Press za on foldable section
  • message: E490: no fold found

Entering insert mode or pressing zx seems to restore fold functionality. Not working without these actions breaks a complete session restore.

Expected behavior

Folds should always work, including those restored via sessions.

Neovim version (nvim -v)

NVIM v0.10.0-dev-3103+gca735c755

Vim (not Nvim) behaves the same?

not applicable

Operating system/version

Arch Linux x86_64

Terminal name/version

Alacritty 0.13.2-2

$TERM environment variable

alacritty

Installation

GitHub nightly tarball

@adrian5 adrian5 added the bug issues reporting wrong behavior label May 10, 2024
@clason
Copy link
Member

clason commented May 10, 2024

You need to set these options in a FileType autocommand or ftplugin; these are not "global options".

@clason clason closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@adrian5
Copy link
Contributor Author

adrian5 commented May 10, 2024

And should :mksession work with options set the way you suggest? From the help file it looks like it might only track global options.

If it does not restore local options, does that mean I cannot restore folds (sessionoptions) in conjunction with nvim-treesitter?

@clason
Copy link
Member

clason commented May 10, 2024

The folds from treesitter behave exactly like other folds.

@adrian5
Copy link
Contributor Author

adrian5 commented May 10, 2024

As sparse as ever with your answers. I would have liked to figure this out with vanilla nvim, but will return to using nvim-ufo then, which somehow handles these use cases.

@clason clason reopened this May 10, 2024
@clason
Copy link
Member

clason commented May 10, 2024

That's all the time I have for, unfortunately. You can try your luck with @lewis6991.

@adrian5
Copy link
Contributor Author

adrian5 commented May 10, 2024

Fair enough, just my frustration speaking as I've gotten stuck on this far too long, apologies!

@wookayin
Copy link
Member

Try evaluating vim.treesitter.foldexpr() manually, e.g. :lua= vim.treesitter.foldexpr(3) and see what it prints. Check setlocal foldexpr and setlocal foldmethod too.

Also ensure the TS parser exists and works for the buffer: :lua= vim.treesitter.get_parser(0):lang().

@tomtomjhj
Copy link
Sponsor Contributor

tomtomjhj commented May 10, 2024

And should :mksession work with options set the way you suggest?

Yes that works. The problem in the repro script is that filetype is not set when foldexpr is first evaluated, so the foldexpr doesn't recognize the language. Setting up an FileType autocmd to set foldexpr will reevaluate foldexpr with proper filetype.

So there are two options for improvements

  • document that treesitter foldexpr should be set in FileType autocmd or ftplugin; or
  • let treesitter fold module setup FileType autocmd to reevaluate foldexpr, so that it works even when the user doesn't do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug issues reporting wrong behavior folds treesitter
Projects
None yet
Development

No branches or pull requests

4 participants