lue-bird/elm-syntax-format

pretty print elm-syntax like elm-format


Keywords
ast, elm, elm-format, elm-syntax, pretty-print
License
MIT
Install
elm-package install lue-bird/elm-syntax-format 1.1.3

Documentation

Pretty print an elm-syntax tree as elm-format would (breaking lines and inserting comments in the right places).

import ElmSyntaxPrint
import ElmSyntaxParserLenient

"""
module   Sample  exposing(...)
plus2 (n)= {- this adds 2-} n
+
2
"""
    |> ElmSyntaxParserLenient.run ElmSyntaxParserLenient.module_
    |> Maybe.map
        (\syntaxModule ->
            syntaxModule
                |> ElmSyntaxPrint.module_
                |> ElmSyntaxPrint.toString
        )
-->
Just """module Sample exposing (..)


plus2 n =
    {- this adds 2 -}
    n
        + 2
"""

To try it out, you can run this node script.

If you want to generate code, better bets are mdgriffith/elm-codegen or the-sett/elm-syntax-dsl.

known deviations in printing

  • ranges of of, exposing and as are needed to determine whether they should be on the next line or at the end of last line
  • ranges of =, :, ,, |, ->, pattern ::, the operators and the keywords are needed to determine whether comments are before or after
  • function types outputting a parenthesized function do not preserve the parens because type parens are not stored in the syntax tree
  • comments before/after parenthesized types will get eaten because type parens are not stored in the syntax tree
  • some floats in exponent representation are formatted to without it and the other way around
  • handling int (and float?) overflow (elm-format itself seems to have issues here, too avh4/elm-format#635)
  • formatting documentation markdown

Please report others you notice <3

performance problems?

Please open an issue.