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_
|> Result.map
(\syntaxModule ->
syntaxModule
|> ElmSyntaxPrint.module_
|> ElmSyntaxPrint.toString
)
-->
Ok """module Sample exposing (..)
plus2 n =
{- this adds 2 -}
n
+ 2
"""If you want to generate code, better bets are mdgriffith/elm-codegen or the-sett/elm-syntax-dsl.
of, exposing and as are needed to determine whether they should be on the next line or at the end of last line=, :, ,, |, ->, the operators and the keywords are needed to determine whether comments are before or afterPlease report others you notice <3
If you notice it's unusable for your use case, please open an issue.
Ideas:
Print.lineSpread faster (cache in Print and or convert Print to defunctionalized)