Ruby formatter
Add this line to your application's Gemfile:
gem 'rufo'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rufo
$ rufo file names or dir names
$ cat file.rb | rufo
$ rufo --check file names or dir names
This will print one line for each file that isn't correctly formatted according to rufo, and will exit with exit code 1.
- Sublime Text: sublime-rufo
Rufo follows most (if not all) of the conventions found in this Ruby style guide. It does a bit more than that, and it can also be configured a bit.
To configure it, place a .rufo
file in your project. When formatting a file or a directory
via the rufo
program, a .rufo
file will try to be found in that directory or parent directories.
The .rufo
file is a Ruby file that is evaluated in the context of the formatter. These are the
available configurations:
# Whether to align successive comments (default: true)
align_comments true
# Whether to align successive assignments (default: true)
align_assignments true
# Whether to align successive hash keys (default: true)
align_hash_keys true
# Whether to convert multiline `{ ... }` block to `do ... end` (default: true)
convert_brace_to_do true
# The indent size (default: 2)
indent_size 2
As time passes there might be more configurations available. Please open an issue if you need something else to be configurable.
The current version is 0.0.3. The formatter was tested on some big files with many Ruby syntax and idioms, but since it's so new there might be some missing stuff. Don't hesitate to open an issue if you find something is not working well. In any case, if the formatter chokes on some valid input you will get an error prompting you to submit a bug report here :-)
Rufo is a real formatter, not a simple find and replace one. It works by employing a Ruby parser and a Ruby lexer. The parser is used for the shape of the program. The program is traversed and the lexer is used to sync this structure to tokens. This is why comments can be handled well, because they are provided by the lexer (comments are not returned by a parser).
To parse and lex, Ripper is used.
As a reference, this was implemented in a similar fashion to Crystal's formatter.
And as a side note, rufo has no dependencies. It only depends on rspec
for tests, but that's it.
That means it loads very fast (no need to read many Ruby files), and because Ripper
is mostly written
in C (uses Ruby's lexer and parser) it formats files pretty fast too.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/asterite/rufo.
The gem is available as open source under the terms of the MIT License.