Elm.Writer does not write any comments
Opened this issue · 3 comments
When parsing the following program, and then writing it again all comments are lost.
SSCCE:
module Test exposing (main)
import Elm.Parser
import Elm.Processing
import Elm.Syntax.File as File
import Elm.Writer as Writer
import Html
src =
"""
module Foo exposing (..)
-- foo
foo = 1
-- bar
greet =
let a = "Hello"
in a ++ "Peter"
"""
parse : String -> File.File
parse input =
case Elm.Parser.parse input of
Err e ->
Debug.todo <| "Oh no: " ++ Debug.toString e
Ok rawFile ->
Elm.Processing.process Elm.Processing.init rawFile
write : File.File -> String
write file =
Writer.write <|
Writer.writeFile file
main =
Html.pre [] [ Html.text <| write <| parse src ]
Ellie related to example: https://ellie-app.com/97gvYxGKYJDa1
The writer is currently not designed to write comments.
In theorie when you parse a file, it can be outputted as the exact same string when using the writer. However, the elm-syntax
Ranges that are connected to most of the syntax elements can be updated to have different values.
The challenge remains exists that comments cannot be written in the places where they were originally located. Due to this I have currently not implemented this, while it will be the root of a lot of 'bugs'.
For example when you have the following code:
x =
y
-- my comment on arg1
arg1
arg2
And the location of arg1
will be updated to be on the same line of y
. It is unclear where to put the comment. Tools such elm-format
can resolve this, while they have full control over the placement of both the syntax and the comment from the moment the code is parsed and it is written back to a file. For elm-syntax
the challenge exists due to the fact that the programmer or other software has control between the parse and the stringification.
For now the best option is to write your own Writer
that does the thing for your specific case.
@jfmengels Related to my comment on #198, looks like elm-syntax does strip comments. I don't know if this might be something you intend to address in your upgrade work on it or not?
Seems quite hard to do, since a comment can appear many places within the AST:
pow4 x =
let --hello
r = -- hello again!
x * x
in -- lets have one here too!
r * -- might as well put a comment in the middle of an expression
r
Which means if you put the comments in the AST, you need to leave holes for them in a ton of places.
Maybe there can be a better way.