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

An unexpected fold is started when "<2" is specified in "foldexpr" #12768

Closed
emonkak opened this issue Aug 12, 2023 · 1 comment
Closed

An unexpected fold is started when "<2" is specified in "foldexpr" #12768

emonkak opened this issue Aug 12, 2023 · 1 comment
Labels

Comments

@emonkak
Copy link
Contributor

emonkak commented Aug 12, 2023

Steps to reproduce

In 'foldexpr', "<2" indicates the end of a level 2 fold. However, if no fold has been started yet, it starts a level 1 fold instead.

This behavior is unexpected and looks like a bug. But, fixing it may break the existing 'foldexpr' settings.

This issue can be reproduced by sourcing the following script using :source:.

new
setlocal foldmethod=expr
let &l:foldexpr = 'v:lnum == 1 ? "<2" : "="'
call setline(1, range(1, 10))
echo map(range(1, 10)), 'foldlevel(v:val)')

Expected: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Actual: [0, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Expected behaviour

Never start a new fold when ending a fold with "<1" or "<2".

I found the code that is causing this issue. Surprisingly, this code has not been modified since 19 years ago. I believe that the following change will fix this issue:

--- a/src/fold.c
+++ b/src/fold.c
@@ -3362,7 +3362,7 @@ foldlevelExpr(fline_T *flp)
          break;

    // "<1", "<2", .. : end a fold with a certain level
-   case '<': flp->lvl_next = n - 1;
+   case '<': flp->lvl_next = MIN(flp->lvl, n - 1);
          flp->end = n;
          break;

vim/src/fold.c

Line 3365 in 81b8bf5

case '<': flp->lvl_next = n - 1;

Version of Vim

9.0.1695

Environment

OS: Gentoo Linux
Terminal: alacritty
$TERM: xterm-256color
shell: zsh

Logs and stack traces

No response

@emonkak emonkak added the bug label Aug 12, 2023
@chrisbra
Copy link
Member

can you please create a PR with a test for it?

zeertzjq added a commit to zeertzjq/neovim that referenced this issue Jan 3, 2024
Problem:  a closing fold expression may unexpectedly start a new fold
          when it should end a fold
          (reported by Shota Nozaki)
Solution: if a fold hasn't started yet, do not immediately
          start a new fold with level 1
          (Shota Nozaki)

fixes: vim/vim#12768
closes: vim/vim#13748

vim/vim@0689b87

Co-authored-by: Shota Nozaki <emonkak@gmail.com>
zeertzjq added a commit to neovim/neovim that referenced this issue Jan 3, 2024
Problem:  a closing fold expression may unexpectedly start a new fold
          when it should end a fold
          (reported by Shota Nozaki)
Solution: if a fold hasn't started yet, do not immediately
          start a new fold with level 1
          (Shota Nozaki)

fixes: vim/vim#12768
closes: vim/vim#13748

vim/vim@0689b87

Co-authored-by: Shota Nozaki <emonkak@gmail.com>
cleong14 pushed a commit to cleong14/neovim that referenced this issue Jan 16, 2024
…#26876)

Problem:  a closing fold expression may unexpectedly start a new fold
          when it should end a fold
          (reported by Shota Nozaki)
Solution: if a fold hasn't started yet, do not immediately
          start a new fold with level 1
          (Shota Nozaki)

fixes: vim/vim#12768
closes: vim/vim#13748

vim/vim@0689b87

Co-authored-by: Shota Nozaki <emonkak@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants