Skip to content

Preserve indentation when re-running annotaterb on nested-position annotations.#333

Merged
OdenTakashi merged 3 commits into
drwl:mainfrom
yamat47:fix/preserve-indentation-on-rerun
May 5, 2026
Merged

Preserve indentation when re-running annotaterb on nested-position annotations.#333
OdenTakashi merged 3 commits into
drwl:mainfrom
yamat47:fix/preserve-indentation-on-rerun

Conversation

@yamat47
Copy link
Copy Markdown
Contributor

@yamat47 yamat47 commented May 5, 2026

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.

yamat47 added 3 commits May 5, 2026 14:06
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 OdenTakashi self-requested a review May 5, 2026 05:31
Copy link
Copy Markdown
Collaborator

@OdenTakashi OdenTakashi left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this. Looks good to me.

@OdenTakashi OdenTakashi merged commit 980724a into drwl:main May 5, 2026
26 checks passed
@yamat47 yamat47 deleted the fix/preserve-indentation-on-rerun branch May 5, 2026 21:04
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.

Re-running annotaterb strips indentation from nested (indented) schema annotations

2 participants