There are many different ways to autoformat JavaScript code, my favorite is always jsbeautifier.org. Apparently, you can also use it locally without a web browser since it supports running it with Rhino.
It took me just few minutes to reimplement the same functionality, but using Qt Script, the fantastic ECMAScript support built into Qt.
The code is in the usual X2 repository, under the javascript/jsbeautify. Note that this is just a convenient mirror as Einar's js-beautify repository already includes this Qt Script driver.
With the rise of server-side JavaScript and desktop-esque JavaScript tools, I hope Qt Script will become more popular. It is sadly still "underrated" right now.
6 comments:
I guess you can give jslint the same treatment.
http://www.jslint.com/rhino/
It would be nice if one could pass parameters to the beautifier. It does not do wrapping to 80 characters, does it?
Anonymous: lint will be my next victim, along with command line options and editor integration (see http://bit.ly/x2-jsedit)
Do you know about the javascript javascript editor?
http://codemirror.net/jstest.html
Anonymous: yes, I am aware of that. Why?
It seems that it would make sense to write the entire javascript editor in javascript. That way it is easy to deploy and make it possible for people to help out with code by visiting your website.
Well, that command line utility is not very usable as of now since it requires you to pipe the result into a new file. Maybe you should add a few commandline options to make it somewhat behave like astyle. I've slightly modified the code so it makes a backup of the original (by appending .orig to the old filename) and saves the new formatted version to the original file. It will skip files that are not modified and will give up on files that already have a *.orig backup file unless you pass --overwrite to it on the commandline. It's a dirty hack that I needed to get it to work, so it would be nice if you could add that functionality in a cleaner way.
Here is the source for those of you who might be interested:
http://www.pastebin.ca/1958967
Post a Comment