- Copy your code
- Switch to the browser
- Go to a website with the beautifier
- Paste the code in
- Click a button
- Select the code again
- Copy the code
- Switch back to your editor
- Paste the code back in the window
That’s nine distinct actions. Using Rhino and a text editor like BBEdit, you can reduce this to two distinct actions
- Select your code
- Invoke a Unix Filter
Taken for an individual case that’s not a big deal. Over a day of coding and debugging it’s a huge timesaver, as well as saving the mental energy of switching contexts from editor to browser and then back to editor.
Assuming you’ve installed the Rhino shell, just download this script and replace the following in the shebang line
with the full path to your js.jar file. The script will work with text streams or will treat the first argument as its input file.
cat ugly.js | beautify.js beautify.js ugly.js
Together At Last
The importPackage line is a convenience that lets us avoiding using a fully qualified package namespace. By importing java.io we can say
abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while with
You’ll notice the word [in] on that list. That means trying to use the following in your Rhino scripts will fail
Whether this is a good thing or a bad thing is better left to history.