google/jsonnet

Formatting tool

Closed this issue · 2 comments

It would be great to have a tool (e.g. jsonnet-fmt or jsonnet fmt) that automatically formats Jsonnet code, similar to go fmt.

Vague plan for this:

  1. Move all syntax desugaring out of the parser into a separate class. That allows more-or-less pretty printing the original AST (although without comments / original whitespace).

  2. Adjust lexer to preserve comments and whitespace. This allows pretty printing exactly in the original form. There may be some shortcuts here, if we don't mind dropping comments in various places in the source code.

  3. This is where is gets vague: My thinking is we can discard / overwrite almost all whitespace, but we must keep all comments (although we may reformat the content of the comment). Comments also should stay on the end of a line if that's where they were put. Double and single empty lines should be preserved, but any more than that can be squashed. Line breaking is quite a challenge. We should change the code to take advantage of the syntax sugars where possible.

Before starting on 3 I really need to take some time to examine go fmt, pyformat and other popular things of this kind.