I have been trying again to get the pretty printer output from elm-syntax-dsl to match the output of elm-format, and have fixed quite a few bugs towards this goal recently.
There are a couple of problems with elm-syntax, that cannot be solved in the pretty printer alone. Specifically, triple quoted multi-line strings “”", and single line comments --. Not that elm-syntax-dsl consumes elm-syntax as a dependency.
For Strings, In Elm.Syntax.Expression, line 125:
type Expression
= ...
| Literal String -- Just one constructor for ALL strings
| ...
In Elm/Parser/Expression.elm, lines 448-454:
literalExpression : Parser (WithComments (Node Expression))
literalExpression =
Tokens.singleOrTripleQuotedStringLiteralMapWithRange
(\\range string ->
{ comments = Rope.empty
, syntax = Node range (Literal string) -- Same constructor for both!
}
)
For single line comments, there is a representation:
type alias File =
{ moduleDefinition : Node Module
, imports : List (Node Import)
, declarations : List (Node Declaration)
, comments : List (Node Comment)
}
The difficulty here is that these comments are gathered at the file level, with their Node giving their position in the code. If I re-format the code, this position may change, so how do I know where to put the comments back again?
So lets add comments directly into Expressions? Except there a lot of places that you can put a comment in Elm code, and that would mean putting a lot of Maybe Comments in the Expression AST, > 99.9% of which would not be used.
The String one seems solvable by adding a Normal|Triple label to the Literal String representation.
The comments one… perhaps take a look at how elm-syntax does it and borrow from there.
Just curious if anyone has any thoughts on this?