Working With RBS in RubyMine
Starting with v2021.2, RubyMine provides code assistance for working with RBS files, along with all of the essential features that make RBS code easier to read. Here’s a brief overview.
Creating RBS code
- Generate RBS type signatures using the Generate Code popup (⌘N or Alt+Insert).
- Run RBS commands with Run Anything (double ^ or double Ctrl).
- Use file templates to create new RBS files, classes, modules, or interfaces.
- Create your own RBS live templates to insert frequently-used constructions into your code.
- Code completion now works for RBS code and includes suggestions for classes, modules, methods, keywords, and more.
Formatting and readability
- Syntax highlighting, code folding, and formatting.
- Smart indentation, auto-insertion of parentheses, and the end keyword.
- Commenting with a shortcut (⌘/ or Ctrl+/ ) and automatic continuation of comments.
- Structure view for RBS files (⌘7 or Alt+7).
- You can now navigate between corresponding
.rbsfiles using new navigation actions. Right-click a project entity, like a class or a method, and select Go to | Type Signature | Associated Declaration. You can also use the ⌃⌘↑ (Ctrl+Alt+Home) shortcut.
- Partial declarations in RBS files are now marked with gutter icons. You can navigate between them by clicking on these icons.
- The Rename refactoring now works within RBS files. For simple refactorings, RubyMine uses the inline version, while in complex cases RubyMine will show a popup. The refactoring accepts names without prefixes, so there is no need to write
@@, for interfaces, global variables, and instance/class variables respectively.
- If you rename an element in RBS, the IDE will automatically try to find and rename text occurrences in comments and string literals.
- RubyMine checks the syntax of RBS files, and if there’s an error, shows what is expected instead.
- A couple of new inspections specific to RBS: unresolved references, and the presence of singleton methods or modules in interfaces.
Support for RBS was first introduced in RubyMine 2021.1 and improved in v2021.2.
To start using RBS in RubyMine, you will need to have the rbs gem in your project. It comes bundled with Ruby 3.0.0 (or later), but needs to be manually installed for earlier Ruby versions (Ruby 2.7.0 and later).
Lastly, check out our post about the benefits of taking the time to add RBS declarations to your project: RBS: How to Get the Most Out of RubyMine’s Code Assistance.
The RubyMine team