Preserve indentation when re-running annotaterb on nested-position annotations.#333
Merged
Merged
Conversation
Cover the two cases that surface when annotations live inside a nested module/class with leading indentation: - AnnotationDiffGenerator should treat an indented current block as equivalent to a flush-left new block when the columns match. - AnnotatedFile::Updater should preserve the original leading whitespace on each line when rewriting the annotation in place. Both specs currently fail; the next commits address them.
When a schema annotation lives inside a nested module/class, every line is prefixed with whitespace. The header and column regexes anchored to ^# and therefore matched nothing for indented blocks, which left current_columns empty and made AnnotationDiff#changed? always return true on re-runs. - Allow optional [ \t]* before # in HEADER_PATTERN and COLUMN_PATTERN. - lstrip the scanned columns so an indented current block compares equal to the flush-left new block when the schema is unchanged.
AnnotatedFile::Updater previously replaced an indented annotation block with a flush-left one, because the new annotation was substituted in as-is while the matched range carried the original leading whitespace. As a result, every re-run on a nested-position file (or any hand-aligned annotation) stripped its indentation. Capture the leading whitespace of the existing annotation block and re-apply it to each line of the new annotation before substituting. Blank lines stay blank so we don't introduce trailing whitespace.
OdenTakashi
approved these changes
May 5, 2026
Collaborator
OdenTakashi
left a comment
There was a problem hiding this comment.
Thanks for fixing this. Looks good to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #332.
When a schema annotation is placed inside a nested module/class (e.g. via
--nested-position, or hand-aligned to match the surrounding indentation), re-running annotaterb stripped the indentation off every line of the block.On top of that, the re-run happened even when the schema had not changed, because the diff generator never recognised an indented block as "the same" as the freshly generated, flush-left one.
This PR fixes both halves of that problem so that nested-position annotations are stable across re-runs.