Fix body block cache index offset when header exists#160
Conversation
When a header element is present, the tree path for body blocks is offset by 1 (header occupies tree index 0). The cache update in splitBlock, mergeBlock, and deleteBlock used the raw tree path index as the array index into doc.blocks, causing off-by-one writes — the split overwrote the wrong block and duplicated content (e.g. "asdf\nasdf" instead of "asdf\n"). Extract localBlockIndex() helper that resolves the correct body-relative index via getRegionBlocks().topIndex, and apply it to all four cache-update sites (insertBlockAfter already had the fix inline). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
📝 WalkthroughWalkthroughIntroduces a shared Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Verification: verify:selfResult: ✅ PASS in 123.0s
Verification: verify:integrationResult: ✅ PASS |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Summary
splitBlock,mergeBlock, anddeleteBlockused the raw tree path index as the array index intodoc.blocks, causing off-by-one writes — the split overwrote the wrong block and duplicated content (e.g. "asdf\nasdf" instead of "asdf\n").localBlockIndex()helper that resolves the correct body-relative index viagetRegionBlocks().topIndex, and apply it to all four cache-update sites.Test plan
verify:fast+verify:selfpass (pre-push hook)🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests