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

Folds are calculated before the tree-sitter tree is updated #25822

Closed
Bekaboo opened this issue Oct 29, 2023 · 5 comments
Closed

Folds are calculated before the tree-sitter tree is updated #25822

Bekaboo opened this issue Oct 29, 2023 · 5 comments
Labels
bug issues reporting wrong behavior folds treesitter
Milestone

Comments

@Bekaboo
Copy link

Bekaboo commented Oct 29, 2023

Problem

Same as #14977.

If using treesitter as fold provider, foldcolumn can be out of sync after the buffer is changed.

Steps to reproduce

  1. Create the minimal.lua init file:

    local tmp = vim.env.TMPDIR
      or vim.env.TEMPDIR
      or vim.env.TMP
      or vim.env.TEMP
      or '/tmp'
    local data = tmp .. '/' .. (vim.env.NVIM_APPNAME or 'nvim')
    local packages_root = data .. '/site'
    local cloned_root = packages_root .. '/pack/packages/start'
    local cloned_path = cloned_root .. '/nvim-treesitter'
    local url = 'https://github.com/nvim-treesitter/nvim-treesitter.git'
    
    vim.fn.mkdir(cloned_root, 'p')
    vim.opt.pp:prepend(packages_root)
    vim.opt.rtp:prepend(packages_root)
    
    if not vim.loop.fs_stat(cloned_path) then
      vim.fn.system({ 'git', 'clone', url, cloned_path })
    end
    
    require('nvim-treesitter.configs').setup({
      ensure_installed = 'json',
      highlight = { enable = true },
    })
    
    vim.o.foldcolumn = '1'
    vim.o.foldmethod = 'expr'
    vim.o.foldexpr = 'nvim_treesitter#foldexpr()'
    vim.o.foldlevelstart = 99
  2. Create the following json file test.json for testing:

    {
      "glossary": {
        "title": "example glossary",
        "GlossDiv": {
          "title": "S",
          "GlossList": {
            "GlossEntry": {
              "ID": "SGML",
              "SortAs": "SGML",
              "GlossTerm": "Standard Generalized Markup Language",
              "Acronym": "SGML",
              "Abbrev": "ISO 8879:1986",
              "GlossDef": {
                "para": "A meta-markup language, used to create markup languages such as DocBook.",
                "GlossSeeAlso": [
                  "GML",
                  "XML"
                ]
              },
              "GlossSee": "markup"
            }
          }
        }
      }
    }
  3. nvim --clean -u minimal.lua test.json

  4. zjzj to jump to the first line of the second fold

  5. O to insert a line above

  6. Observe that the foldcolumn is out of sync

Screenshots:

  • Step 3:

    image

  • Step 4:

    image

  • Step 5 (observe that foldcolumn not updated in time):

    image

Expected behavior

Foldcolumn should always be in sync.
Foldcolumn should be re-calculated after treesitter updates folds.

Neovim version (nvim -v)

NVIM v0.10.0-dev-1427+gac353e87aec

Vim (not Nvim) behaves the same?

no, vim does not have treesitter

Operating system/version

Linux 6.1.60-1-lts

Terminal name/version

alacritty 0.12.3 (5efb0690) + tmux 3.3a

$TERM environment variable

tmux-256color

Installation

AUR

@lucario387
Copy link
Contributor

lucario387 commented Nov 13, 2023

Only when using o or O the foldexpr is calculated incorrectly. If instead we use A<CR> it will be correct.

Edit: I was wrong. nnoremap o A<CR> works as expected

@tomtomjhj
Copy link
Sponsor Contributor

This might have been fixed by recent fixes in vim.treesitter.foldexpr() and extmark updates. Can you still reproduce on the latest version with vim.treesitter.foldexpr() instead of nvim_treesitter#foldexpr()?

@igorlfs
Copy link

igorlfs commented May 11, 2024

I can confirm I'm still having this issue with NVIM v0.10.0-dev-3116+gc1396afa7c

EDIT: actually, I'm having a similar issue: given a closed fold, if I use o in its line, it opens. And similarly, when using O in the line below it, it also opens. I had assumed those were the same issue, but my foldcolumn behaves as expected.

@justinmk justinmk added the folds label May 11, 2024
@justinmk justinmk modified the milestones: unplanned, backlog May 11, 2024
@tomtomjhj
Copy link
Sponsor Contributor

@igorlfs #28709 should fix the issue you're experiencing.

I can't reproduce the original issue Bekaboo described.

@igorlfs
Copy link

igorlfs commented May 12, 2024

Hey, thanks for the quick fix! I thought about reporting it, but I suspected it was just a side effect of this issue!

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

6 participants