Skip to content

🐛 Keep a nested final-block foot comment at its indent on edit#215

Merged
frenck merged 1 commit into
mainfrom
frenck/foot-final-block-indent
Jul 4, 2026
Merged

🐛 Keep a nested final-block foot comment at its indent on edit#215
frenck merged 1 commit into
mainfrom
frenck/foot-final-block-indent

Conversation

@frenck

@frenck frenck commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Breaking change

None. Fixes a round-trip fidelity bug; no API change.

Proposed change

Fixes a foot-comment fidelity bug exposed while adding comment_after (#213). A comment at the end of a nested final block round-trips byte-for-byte when untouched, but the moment the document is edited it re-emits at column 0 instead of the block's indent:

parent:
  a: 1
  b: 2
  # end of parent      <- edit `b`, and this jumps to column 0

The cause was in the composer: every trailing comment left after the walk was attached to the outermost (root) node's foot, which the emitter renders at indent 0. On the AST re-emit path (taken after an edit) the comment therefore detached from the block it belongs to.

The fix routes each trailing comment to the deepest block it is indented into, by descending the last-child spine while the child is a block collection no more indented than the comment. A comment aligned with a nested block's contents now lands on that block (and re-emits at its indent); a comment at the document's own indent still stays on the outermost node. Multi-level trailing comments each attach to their own level. The descent is bounded by the document's nesting depth and stack-guarded, consistent with the other composer walks.

Because unmodified documents re-emit their trailing comment verbatim (independent of which node owns it), this changes only the post-edit path: the full compliance suite and the entire real-world round-trip corpus stay byte-for-byte. New tests cover the edit-keeps-indent case, the nested read-back attribution, and multi-level routing.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

Checklist

  • I have read the AI Policy, and this pull request was not created by an autonomous agent.
  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and uv run pytest passes locally. A pull request cannot be merged unless CI is green.
  • uv run ruff check . and uv run ruff format --check . pass.
  • cargo fmt --check and cargo clippy --all-targets -- -D warnings pass.
  • Round-trip fidelity is preserved: an unmodified document still re-emits byte-for-byte.
  • No commented-out or dead code is left in the pull request.

If the change is user-facing:

  • Documentation under docs/ is added or updated, and docs/verify_examples.py still passes.

Copilot AI review requested due to automatic review settings July 4, 2026 19:59
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@frenck, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 47 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 75918a7d-fb58-4dec-8071-da01eb7a619d

📥 Commits

Reviewing files that changed from the base of the PR and between 11b61e6 and a830d98.

📒 Files selected for processing (3)
  • src/roundtrip/composer.rs
  • tests/roundtrip/test_node.py
  • tests/roundtrip/test_roundtrip.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a round-trip fidelity bug in the composer's trailing-comment attachment. Previously, all comments left after the AST walk were attached to the outermost (root) node's foot, which the emitter renders at column 0. For an unmodified document this was hidden by the verbatim source cache, but once the document was edited (forcing the AST re-emit path), a comment at the end of a nested final block would flatten to column 0 instead of keeping the block's indent. The fix introduces a bounded descent (foot_target) that routes each trailing comment to the deepest block collection it is indented into, so it re-emits at the correct indent. This is a follow-up to #213 (writable foot comments).

Changes:

  • Added foot_target/foot_target_inner to descend the last-child spine and attribute each trailing comment to the deepest block collection whose column is <= the comment's column.
  • Added is_block_collection helper to identify non-empty block mappings/sequences that can carry a foot comment.
  • Added tests for the edit-keeps-indent case, nested read-back attribution, and multi-level trailing-comment routing.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/roundtrip/composer.rs Routes trailing comments to the deepest indented block via a new bounded, stack-guarded descent instead of always the root node.
tests/roundtrip/test_roundtrip.py Adds a test that an edited nested final-block foot comment keeps its indent.
tests/roundtrip/test_node.py Adds tests that nested/multi-level trailing comments read back as the owning block's comment_after.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/roundtrip/composer.rs Outdated
@codspeed-hq

codspeed-hq Bot commented Jul 4, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing frenck/foot-final-block-indent (a830d98) with main (11b61e6)

Open in CodSpeed

@codecov-commenter

codecov-commenter commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.64%. Comparing base (11b61e6) to head (a830d98).

Files with missing lines Patch % Lines
src/roundtrip/composer.rs 97.05% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #215      +/-   ##
==========================================
+ Coverage   92.62%   92.64%   +0.01%     
==========================================
  Files          40       40              
  Lines       12155    12188      +33     
==========================================
+ Hits        11259    11291      +32     
- Misses        896      897       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frenck frenck force-pushed the frenck/foot-final-block-indent branch from 81080ea to a830d98 Compare July 4, 2026 20:11
@frenck frenck merged commit 5e56c8b into main Jul 4, 2026
73 checks passed
@frenck frenck deleted the frenck/foot-final-block-indent branch July 4, 2026 20:44
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants