Skip to content

Commit

Permalink
fix: improve multiline template string formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aboeglin committed Apr 21, 2024
1 parent 1117f4e commit c28979d
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions compiler/main/Format/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,10 @@ typingToDoc canBreak comments typing = case typing of

templateStringExpsToDoc :: [Comment] -> [Exp] -> (Pretty.Doc ann, [Comment])
templateStringExpsToDoc comments exps = case exps of
(Source area target (LStr s) : more) | not ("\"" `isPrefixOf` s) && not ("\"" `isSuffixOf` s) ->
let (e', comments') = expToDoc comments (Source area target (LStr $ escapeBackticks s))
(more', comments'') = templateStringExpsToDoc comments' more
in (Pretty.nesting (\x -> Pretty.nest (-x) e') <> more', comments'')
(Source _ _ (LStr s) : more) ->
let e' = Pretty.hcat $ Pretty.punctuate Pretty.hardline (map Pretty.pretty $ lines s)
(more', comments') = templateStringExpsToDoc comments more
in (Pretty.nesting (\x -> Pretty.nest (-x) e') <> more', comments')

(e : more) ->
let (e', comments') = expToDoc comments e
Expand Down Expand Up @@ -1154,12 +1154,8 @@ expToDoc comments exp =
(Pretty.pretty $ renderChar c, comments')

Source _ _ (LStr s) ->
if "\n" `isSuffixOf` s then
(Pretty.pretty (init s) <> Pretty.hardline, comments')
else if "\n" `isPrefixOf` s then
(Pretty.hardline <> Pretty.pretty (tail s), comments')
else
(Pretty.pretty s, comments')
let s' = "\"" <> s <> "\""
in (Pretty.pretty s', comments')

Source _ _ (LBool b) ->
(Pretty.pretty b, comments')
Expand Down

0 comments on commit c28979d

Please # to comment.