Skip to content

Conversation

pcasaretto
Copy link

Summary

This PR adds support for recognizing RBS (Ruby Signature) inline type annotation comments in Ruby code. This enables better editor support for Ruby's type annotations by distinguishing them from regular comments in the AST.

Motivation

RBS is Ruby's official type signature language, and increasingly Ruby developers are using inline RBS comments to annotate their code with type information:

#: (Integer) -> String
def to_hex(num)
  num.to_s(16)
end

x = 42 #: Integer

Currently, tree-sitter-ruby treats these as regular comments, making it impossible for editors to provide enhanced support for type annotations (different highlighting, hover information, etc.).

Implementation

This implementation takes a minimal approach - it recognizes RBS comment patterns without parsing the type syntax itself:

  • #: comments are recognized as rbs_type_comment tokens
  • #| comments (multi-line continuations) are recognized as rbs_continuation_comment tokens
  • Regular # comments remain unchanged

Why not parse the full RBS syntax?

  1. Separation of concerns: RBS is effectively a different language. Tools that need to understand RBS types can extract the comment content and parse it with a dedicated RBS parser.
  2. Simplicity: Full RBS syntax would require 200+ additional grammar rules
  3. Maintenance: Keeping them separate means each parser can evolve independently

Changes

  • grammar.js: Added rbs_type_comment and rbs_continuation_comment token rules
  • queries/highlights.scm: Added highlighting as @type.annotation (vs @comment for regular comments)
  • test/corpus/rbs_annotations.txt: Added comprehensive test coverage with 11 test cases

Testing

All tests pass (301 total, including 11 new RBS tests):

  • ✅ Method signatures
  • ✅ Variable annotations
  • ✅ Type assertions
  • ✅ Multi-line signatures
  • ✅ Complex types (unions, generics, optionals)
  • ✅ Mixed regular and RBS comments

Impact

  • Non-breaking: Existing Ruby parsing is completely unchanged
  • Editor-friendly: Provides foundation for enhanced type annotation support
  • Future-proof: Can be extended if needed without breaking changes

Related Work

@@ -0,0 +1,231 @@
=======================
Copy link

Choose a reason for hiding this comment

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

Could you add a test showing the behavior if the #: is found inside a string:

puts "#: Hello, World"

Copy link
Author

Choose a reason for hiding this comment

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

Done!

Copy link
Author

Choose a reason for hiding this comment

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

Added some more edge cases involving strings.

@pcasaretto pcasaretto force-pushed the add-rbs-inline-annotations branch from 3ce9025 to 57b2f6b Compare September 1, 2025 12:57
Adds recognition of RBS (Ruby Signature) inline type annotation comments
to enable better editor support for Ruby type annotations.

Changes:
- Add rbs_type_comment token for #: comments
- Add rbs_continuation_comment token for #| comments
- Include both tokens in extras to allow them anywhere
- Add highlighting queries to style RBS comments as @type.annotation
- Add comprehensive test coverage with 11 test cases

This implementation intentionally only recognizes RBS comment patterns
without parsing the type syntax itself. This keeps the grammar simple
while providing the foundation for tools to identify and process type
annotations separately.

RBS comments are now distinguished from regular comments in the AST,
allowing editors to provide enhanced type information display and
navigation for Ruby code with inline type signatures.
@pcasaretto pcasaretto force-pushed the add-rbs-inline-annotations branch from 57b2f6b to 50c5dd3 Compare September 1, 2025 13:05
@pcasaretto pcasaretto requested a review from Morriar September 1, 2025 13:06
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.

2 participants