Skip to content

Conversation

@seflue
Copy link
Contributor

@seflue seflue commented Dec 30, 2025

Summary

This PR fixes fold corruption that occurs when cycling TODO states on folded headlines. When a TODO state change triggers line insertions (CLOSED timestamps, notes) or removals, Neovim's fold boundaries become stale, causing visual glitches or incorrect fold behavior. The fix captures the fold state before modifications, recomputes folds via zx, and restores the original fold state.

Related Issues

Changes

  • Add recompute_folds() helper function that recomputes fold boundaries and restores current fold state
  • Capture fold state before TODO state modifications in _todo_change_state()
  • Call recompute_folds() at all exit points (same-group transitions, done/undone transitions, repeater handling)
  • Add test verifying fold boundaries are preserved after TODO state cycling

Checklist

I confirm that I have:

  • Followed the
    Conventional Commits
    specification
    (e.g., feat: add new feature, fix: correct bug,
    docs: update documentation).
  • My PR title also follows the conventional commits specification.
  • Updated relevant documentation, if necessary.
  • Thoroughly tested my changes.
  • Added tests (if applicable) and verified existing tests pass with
    make test.
  • Checked for breaking changes and documented them, if any.

@kristijanhusak
Copy link
Member

Thanks for the PR! Even though this fixes that specific issue, I think it might introduce some other issues.
zx affects the whole buffer, and if someone opened few folds around the file, and does a todo change on one thing, everything gets recomputed to the current foldlevel, which might not be desired result.

I'm not really a user of folds, but can you clarify when the issue happens more often, when doing todo change on the closed fold or on the opened fold?
You could hook up into TodoChanged event and do this for your use case, only issue is that the event is fired before the log notes and everything, but we might be able to move that around if it helps.

@seflue
Copy link
Contributor Author

seflue commented Jan 4, 2026

It actually happens all the time, when manipulating a folded headline. This is the reason, why I added this test.
I am a heavy user of folds and for simplicity I choose to get other folds closed over having broken folds (I would need to recompute them by myself). To avoid this, we could enhance the implementation by actually caching all open folds and restore them, but the state bevor my PR is less usable for users of folds, because the whole fold experience breaks, when changing a headline.
I didn't really find a less invasive solution then zx, to fix the folds, but I am open for suggestions. Only keeping it the way it currently is on master is not really an option - because it offloads the burden to fix the folds to the user.

@seflue seflue force-pushed the fix/recompute-folds branch from 6646332 to 2a2c013 Compare January 25, 2026 12:58
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