wcandillon/xqlint — pull request #1 by ldoblies (2012-12-12 10:28:16+00:00)
Title: CodeFormatter

Hello William,

Here's the CodeFormatter status. I also added CodeFormatter to run_tests (it just prints out the pre- and postformat code), and testcases in tests/*.xq so just run tests with e.g.

node run_tests.js -f tests/flwor.xq

All but tests/typeswitch.xq should produce the expected result. typeswitch.xq has some xml tags, which I didn't take care of yet (it introduces too many spaces). Also, whitespace is deleted from the original code up to now.

What bothers me a bit is that I use string.charAt and string.substring quite a lot when writing spaces / newlines to the formatted code (the postNewLine()/postSpace()/post() methods), but I don't know how bad this actually is performancewise in js, so I guess we'll just see how well it does on large files in the end, and if necessary I can rewrite it.

For the if-then-else and return statements, I already implemented simple line-wrapping checks: If below a certain length, the statement is written on one line (this corresponds to the style document). I think this approach is quite cool (it first tries to format on one line, and if it's too long, it wraps, see e.g. this.ReturnClause), and I now also believe it should work in one pass, maybe even easier than multipass. I still have to work on where things are wrapped though, and use the max-line length (80 is proposed by the doc) on all nodes. But I guess I'll first implement the default formatting and then extend it by the line-wrapping stuff.

Looking forward to your feedback.

Luchin