Everyone has a preference on formatting code. I have found that there are a lot of very adamant people when it comes to the formatting of code. Unless a developer is using a language, like Python, that requires you to indent code it is up to the developer to correctly indent. When working with a team of people of you can have someone who does not care as much for strict formatting and you can have some one who is a formatting extremist.
To resolve some issues with other team members I have created a simple ruby formatter. I call it simple because it only formats one line at a time. There is not looking around at different lines, no knowledge of location in an object, or even scoping. I would like to create a rbchecker that would be like
Pychecker, but this project is not that advanced. There is no parsing of ruby syntax, instead I am just looking for simple patterns. I am sure I missed a pattern or two but I have the option to create a back of the original file. I suggest turning on the back up for the first time you run this.
After I finished up a few test i figured i would look around to see if there were any other formatters. Turns out there are a
few but, sine I am already done... Why not release it anyways?
Right now it is mostly comments, and no i did not run the formatter on it's self. I have been testing it on other projects of mine and koders.com ruby files.
Where to get it:
My svn
http://svn.stephenbeckeriv.com/code/ruby_formatter/
How to use it:
Required:
-f file/location/e.rb
This can also be called with commas
-f file/location/e.rb,file/location/q.rb,file/location/w.rb
I am willing to change this if someone has a better idea.
Optional:
-s # will set using spaces with the size of an indent being #, the default is a tab
-b will create a backup of the file that is filename+",bk."+#{Time.now} I suggest using this when you run it for the first time.
ruby simple_formatter.rb -f /home/sbecker/test_file.rb
The only output should be "Done!"
ruby simple_formatter.rb -s 2 -f /home/sbecker/test_file.rb
Same formatted file now with two spaces instead of tabs
ruby simple_formatter.rb -b -f /home/sbecker/test_file.rb
Creates formated file as test_file.rb and the original file test_file.bk.#{Time.now}
I will be updating the code as a find more things that break it. Right now its worked for 95% of the code i tested it on. The only thing i know it does not work with currently is one line do end statements.
Good News Everybody!
Ruby formatter has an raa site! But the real good news is Andrew Nutter-Upham submitted a patch that changes how the options are done!
ruby [options] filelist
options:
-s # will change the indent to a space count of # per level
by default we space with 1 tab per level
-b # create a backup file
examples:
ruby simple_formatter.rb -s 3 -b /moo/cluck/cow.rb
runs with the indent of 3 spaces,creates a backup file, and formats moo/cluck/cow.rb
ruby simple_formatter.rb -s 31 /moo/cluck/cow.rb /beef/lo/mein.rb
formats both files with 31 spaces per level.
I like this formatting a lot better. You can also do a --help (if you have RI installed) and it produces something (I do not have RI installed). I hope to stay active with this project and look forward to hearing from more people.
There are a few basic rules I have for the simple formatter.
1) Key words are followed by a space
So no if(true). I want if (true)
2) Comments will be aligned with the current depth
I tried not formatting the comments depth, but I did not like the results
3) Block comments and here docs(soon) are left unformatted
The begin and end lines will be aligned but the inside of the comment/doc will not be touched
Thats about it right now. A few problems problems I fixed was using words like definition, comments after statements, and the order in which I strip user data. The current code in svn fixes these problems.
I have a few debugging statements or a debug mode if you will. Just set DEBUG_ME to true and watch some logic unfold.
I have been busy with the ruby formatter. I made the formatter a class. By class I mean I took the code from simple_formatter.rb and created some methods. I did this so people can create their own interfaces for the formatter.
Example:
format_me = Formatter.new
ruby_code =<<-RUBY
def moo
p ouch
end
RUBY
puts format_me.format_string(ruby_code)
format_me.format_file("/x/y/z.rb")
I also added a test file that I will add code to if someone sends me a chunk of code that breaks the formatter
I also fixed a few thing:
Here docs are not formatted
while statements at the end of blocks work
checks for one-line rescue statements
better command line options
better debugging statments and use of profiler
I strip out all regex, strings and comments before checking ruby code
Next step? Find more code to break the formatter and improve performance.
I have been using profile to find places to improve my code. I was looking at the profiler when I was in debug mode but, in debug mode I call methods more then once. After turning the profiler on and the debug statements off I was able to locate methods that were called more then once per line of formatted code. I also changed some if statements around and added constant regxs.
I saved a log my changes and put them up on the svn
Someone emailed me today about my formatter being broken. I was incorrectly looking for do statements. For example I would only catch do |x| and not just do. I was also sending the string to the one liner check. These have been fixed and committed.
I created a blank rails 2.0.1 project the other day and froze the gems to the vendor dir. I then used a script to gather all the .rb files in the vendor dir. I then ran my simple_formatter.rb script on them. Out of 654 files 611 did not throw a negative index error. This does not mean that all 611 were formatted correctly but it is a good sign that I am getting close to a complete formatter.