Skip to content

Add RBS::Rewriter and use it in rbs annotate#2927

Merged
soutaro merged 5 commits intomasterfrom
rewriter
Apr 8, 2026
Merged

Add RBS::Rewriter and use it in rbs annotate#2927
soutaro merged 5 commits intomasterfrom
rewriter

Conversation

@soutaro
Copy link
Copy Markdown
Member

@soutaro soutaro commented Apr 8, 2026

Summary

  • Add RBS::Rewriter class for targeted character-range replacements on Buffer content
  • Provides add_comment, replace_comment, delete_comment methods for comment manipulation
  • Refactor rbs annotate to use Rewriter instead of Writer, preserving non-documentation comments and original formatting (inline comments, multi-line definitions, etc.)
  • Deprecate preserve: parameter in annotate_file since Rewriter always preserves formatting

Motivation

RBS::Writer regenerates the entire file from AST, which loses non-documentation comments (inline comments like # Some comment here at end of lines). RBS::Rewriter only modifies the specific locations that need to change, preserving everything else.

Test plan

  • ruby -Ilib -Itest test/rbs/rewriter_test.rb — 20 tests for Rewriter
  • ruby -Ilib -Itest test/rbs/annotate/rdoc_annotator_test.rb — 10 tests for annotator integration

🤖 Generated with Claude Code

soutaro and others added 4 commits April 8, 2026 15:06
Rewriter performs character-range replacements on Buffer content using
RBS::Location, preserving everything outside the rewritten ranges.
Unlike Writer which regenerates entire source from AST, this allows
modifying specific parts without losing non-documentation comments.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Split the comment rewriting API into three focused methods:
- add_comment(*locations, content:) inserts before the earliest location
- replace_comment(comment, content:) rewrites comment content in place
- delete_comment(comment) removes comment lines including indentation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace AST mutation + Writer serialization with Rewriter-based
comment editing, preserving non-documentation comments and original
formatting in annotated RBS files.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Rewriter always preserves formatting, so preserve: is no longer needed.
Default it to true and mark the overload with preserve: as deprecated.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@soutaro soutaro added this to the RBS 4.1 milestone Apr 8, 2026
@soutaro
Copy link
Copy Markdown
Member Author

soutaro commented Apr 8, 2026

I plan to implement more APIs on Rewriter class, but currently it has only Comment related ones.

Rewriter always preserves formatting, so the flag is no longer needed.
Keep the option for backwards compatibility but mark it as deprecated.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@soutaro soutaro merged commit ce47189 into master Apr 8, 2026
26 checks passed
@soutaro soutaro deleted the rewriter branch April 8, 2026 08:08
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.

1 participant