New Pretty-Printer

Pretty-printing, i.e. regularly formatting, of source code has been part of qooxdoo’s tool chain for a long time. But it also often didn’t work the way people expected, either due to bugs or ‘by design’. After much laboring with bug fixing and adding options to support more tastes, we decided to do a complete re-write.

The new pretty-printer takes a slightly different approach than the old one. Rather than an  abstract syntax tree it uses more of a concrete syntax tree, which retains all lexems from the source code, including commas, semis, all kinds of closing brackets and so forth. This allows to capture all kinds of comments together with their actual ordering amongst the  source code tokens, so pretty-printing can reproduce it. In that it is much more reliable (no more shuffling comments around, or even omitting them, or even breaking the syntax).

(For the curious, an example where this is getting crucial is this: If you have an empty array literal with a block comment in it,

var a = [/*42*/];

it becomes indistinguishable in an abstract syntax tree from this rendering

var a = []/*42*/;

which is a rather different thing for most people.)

The new implementation tries to reproduce the former functionality (inserting of TODOC comments is not yet implemented), with the addition of a new text width option that allows you to gear the length of each source line towards this width (config key pretty-print/general/text-width).

If you give it a try and find any issue, please open a bug for it. You should regard pretty-printing as a rough first step towards well-formatted code that you apply to code you include from foreign sources, or which has been neglected for some while. The automatic process lays a regualar foundation to the source code which you then tune by hand. But it cannot capture all the fine prints of code layout that you might wish for (unless, of course, you deploy a plethora of configuration options).

For example, you might prefer an opening '{' on the same line as the preceeding code except for class maps that are passed to qx.Class.define calls. Or you want to lay out multiple parameters of a function call across multiple lines, with a certain grouping on each line. Or you want to vertically align the '=' in multiple assignment statemens across several lines. These fine tunings would get lost with regularly running the pretty-print job on your code.

One thought on “New Pretty-Printer

  1. Sounds great! I’ll check this out soon. :) I for one won’t be missing the adding of TODOC comments, as that would be the first thing I’ll turn off when it’s implemented again.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">