Skip to content

fix: preserve manually opened folds after text changes#320

Open
tasso-cs wants to merge 1 commit intokevinhwang91:mainfrom
tasso-cs:fix/preserve-manually-opened-folds
Open

fix: preserve manually opened folds after text changes#320
tasso-cs wants to merge 1 commit intokevinhwang91:mainfrom
tasso-cs:fix/preserve-manually-opened-folds

Conversation

@tasso-cs
Copy link
Copy Markdown

Summary

When using zM to close all folds (foldlevel=0) and then zo to manually open a fold, editing text would cause the fold to close again.

Root cause

  1. syncFoldedLines() wasn't called before getRangesFromExtmarks(), so ufo didn't know about manually opened folds
  2. When folds are recreated with foldlevel=0, all folds close, overriding manually opened ones

Fix

  • Call fb:syncFoldedLines(winid) before getRangesFromExtmarks() to sync ufo's internal state with Vim's actual fold state
  • After restoring foldlevel, if it's 0, explicitly reopen folds that should stay open (those not in rowPairs)

Steps to reproduce (before fix)

  1. Open a file with folds (e.g., markdown with headers)
  2. Run zM to close all folds
  3. Run zo to open one fold
  4. Edit text (e.g., x to delete a character)
  5. Bug: The fold closes again

Test plan

  • Tested manually: after fix, manually opened folds stay open after text edits
  • Verified both FFI and non-FFI code paths are updated

When using zM to close all folds (foldlevel=0) and then zo to manually
open a fold, editing text would cause the fold to close again.

Root cause:
1. syncFoldedLines() wasn't called before getRangesFromExtmarks(), so
   ufo didn't know about manually opened folds
2. When folds are recreated with foldlevel=0, all folds close,
   overriding manually opened ones

Fix:
- Call fb:syncFoldedLines(winid) before getRangesFromExtmarks() to sync
  ufo's internal state with Vim's actual fold state
- After restoring foldlevel, if it's 0, explicitly reopen folds that
  should stay open (those not in rowPairs)
@tasso-cs
Copy link
Copy Markdown
Author

@kevinhwang91 let me know if this is of interest to review / merge. thank you :)

@c-tesch
Copy link
Copy Markdown

c-tesch commented Feb 25, 2026

This happens when fully fold is the default too:

vim.opt.foldlevelstart = 0

whatever changes I make, x, d, or leave insert mode folds all sections

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants