Skip to content

Commit

Permalink
fix: handle a 2 annoying edge cases of template strings
Browse files Browse the repository at this point in the history
- it would not format correctly sometimes and replace newlines with spaces
- it would do weird things with strings in templated values
  • Loading branch information
aboeglin committed Apr 20, 2024
1 parent 34d7860 commit fc685d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 0 additions & 3 deletions compiler/main/Canonicalize/Canonicalize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ instance Canonicalizable Src.Exp Can.Exp where
Src.Source _ _ (Src.LStr "") ->
Nothing

Src.Source area srcTarget (Src.LStr s) ->
Just $ Src.Source area srcTarget (Src.LStr ("\"" <> s <> "\""))

_ ->
Just exp

Expand Down
9 changes: 6 additions & 3 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) ->
(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'')
in (Pretty.nesting (\x -> Pretty.nest (-x) e') <> more', comments'')

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

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

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

0 comments on commit fc685d8

Please sign in to comment.