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

bug(statuscolumn): Adjacent folds not displaying correctly with foldopen #1533

Open
4 tasks done
gczcn opened this issue Mar 8, 2025 · 7 comments · May be fixed by #1534
Open
4 tasks done

bug(statuscolumn): Adjacent folds not displaying correctly with foldopen #1533

gczcn opened this issue Mar 8, 2025 · 7 comments · May be fixed by #1534
Labels
bug Something isn't working

Comments

@gczcn
Copy link

gczcn commented Mar 8, 2025

Did you check docs and existing issues?

  • I have read all the snacks.nvim docs
  • I have updated the plugin to the latest version before submitting this issue
  • I have searched the existing issues of snacks.nvim
  • I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

0.11.0-dev-1930+gb2fa51bf15

Operating system/version

macOS 15.3.1

Describe the bug

When there are two adjacent folds, the part that determines whether a line is the beginning of a fold may not work properly. This is because when two folds are adjacent, the foldlevel of the previous line of the latter fold may be the same as that of the previous line, causing this line to be considered a line in a fold.

a simple example:

Image

line 361 does not display the foldopen symbol.

Steps To Reproduce

  1. Ensure the statuscolumn section in the configuration has the open option set to true
  2. Create a lua file and enter the following content, making sure there are adjacent folds in the file
if true then
  print('true')
end
if false then
  print('false')
end
  1. Problem occurs

Expected Behavior

Correctly display foldopen symbols for adjacent folds.

Repro

vim.o.foldlevel = 99
vim.o.foldmethod = 'expr'
vim.o.foldexpr = "v:lua.vim.treesitter.foldexpr()"
vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    {
      "folke/snacks.nvim",
      opts = {
        statuscolumn = {
          folds = {
            open = true,
          },
        },
      },
    },

    -- install the treesitter parser for lua
    {
      "nvim-treesitter/nvim-treesitter",
      opts = {
        ensure_installed = { 'lua' },
      },
    },
  },
})

-- Example
if true then
  -- ...
end
if false then
  -- ...
end
@gczcn gczcn added the bug Something isn't working label Mar 8, 2025
@dpetka2001
Copy link
Contributor

dpetka2001 commented Mar 8, 2025

That's because vim.fn.foldlevel() which is used by the statuscolumn returns the same number of level folding for those mappings.

vim.keymap.set("n", "t", function()    <-- foldlevel is some number here
  fadskfjas
end)
vim.keymap.set("n", "s", function()    <-- foldlevel here is the same as the previous
  fjasiofaja
end)

The opened markers show up only at lines where the foldlevel is not the same between the current and previous line.

@dpetka2001
Copy link
Contributor

Actually, if we change the line here

elseif config.folds.open and vim.fn.foldlevel(lnum) > vim.fn.foldlevel(lnum - 1) then
with the following elseif config.folds.open and vim.treesitter.foldexpr(vim.v.lnum):sub(1, 1) == ">" then I believe it behaves more to how you describe.

@gczcn
Copy link
Author

gczcn commented Mar 8, 2025

Actually, if we change the line here

snacks.nvim/lua/snacks/statuscolumn.lua

Line 147 in bc0630e

elseif config.folds.open and vim.fn.foldlevel(lnum) > vim.fn.foldlevel(lnum - 1) then
with the following elseif config.folds.open and vim.treesitter.foldexpr(vim.v.lnum):sub(1, 1) == ">" then I believe it behaves more to how you describe.

For me, this solved the problem. However, as described in this issue, using vim.treesitter.foldexpr() will only allow folds provided by treesitter.

@gczcn gczcn closed this as completed Mar 8, 2025
@gczcn gczcn reopened this Mar 8, 2025
@gczcn
Copy link
Author

gczcn commented Mar 8, 2025

Sorry, I just closed this question by accident.

@dpetka2001
Copy link
Contributor

dpetka2001 commented Mar 8, 2025

I see, you're right. I wasn't aware of that issue. Then probably my PR which essentially reverts the change that was made for that issue you mentioned will be denied. I'm not aware of a better solution in this case.

Hopefully, maintainer will have a better idea with regards to this. Maybe it's possible maybe not. But vim.fn.foldlevel() only returns the level which for adjacent foldings will be the same.

I'll close my PR and wait for maintainer's input.

@gczcn
Copy link
Author

gczcn commented Mar 9, 2025

The statuscol.nvim plugin can handle this situation correctly, but I can't understand its code.

@dpetka2001
Copy link
Contributor

I pushed a change which somewhat takes into consideration vim.lsp.foldexpr() as well, but there's a small caveat (which I mention in my PR).

I'm sure maintainer will have a more proper answer with regards to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants