Skip to content

Commit

Permalink
Fix formatting of multi-line strings that end with a single-quote (#1554
Browse files Browse the repository at this point in the history
)

Fixes #1547.

Also fix haddocks for escapeLastLineLeadingWhitespace.
  • Loading branch information
sjakobi authored Nov 17, 2019
1 parent 1a831d1 commit 4bf970b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
24 changes: 20 additions & 4 deletions dhall/src/Dhall/Pretty/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,13 +1147,16 @@ prettyCharacterSet characterSet expression =
-- >>> multilineChunks (Chunks [] "\n\NUL\b\f\t")
-- Chunks [("\n",TextLit (Chunks [] "\NUL\b\f"))] "\t"
multilineChunks :: Chunks s a -> Chunks s a
multilineChunks = escapeControlCharacters . escapeLastLineLeadingWhitespace
multilineChunks =
escapeTrailingSingleQuote
. escapeControlCharacters
. escapeLastLineLeadingWhitespace

-- | Escape leading whitespace on the last line by moving it into a string
-- string interpolation
-- interpolation
--
-- Unescaped leading whitespace on the last line would otherwise be removed
-- by the parser's dedentation logic.
-- This ensures that the parser can find the correct indentation level, no matter
-- what the other lines contain.-
--
-- >>> escapeLastLineLeadingWhitespace (Chunks [] "\n \tx")
-- Chunks [("\n",TextLit (Chunks [] " \t"))] "x"
Expand Down Expand Up @@ -1231,6 +1234,19 @@ splitOnPredicate p t = case Text.break p t of
(c, d) -> case splitOnPredicate p d of
(e, f) -> ((a, c) : e, f)

-- | Escape a trailing single quote by moving it into a string interpolation
--
-- Otherwise the multiline-string would end with @'''@, which would be parsed
-- as an escaped @''@.
--
-- >>> escapeTrailingSingleQuote (Chunks [] "\n'")
-- Chunks [("\n",TextLit (Chunks [] "'"))] ""
escapeTrailingSingleQuote :: Chunks s a -> Chunks s a
escapeTrailingSingleQuote chunks@(Chunks as b) =
case Text.unsnoc b of
Just (b', '\'') -> Chunks (as ++ [(b', TextLit (Chunks [] "'"))]) ""
_ -> chunks

-- | Pretty-print a value
pretty_ :: Pretty a => a -> Text
pretty_ = prettyToStrictText
Expand Down
1 change: 1 addition & 0 deletions dhall/tests/format/multilineTrailingSingleQuoteA.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"\n'"
3 changes: 3 additions & 0 deletions dhall/tests/format/multilineTrailingSingleQuoteB.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
''

${"'"}''

0 comments on commit 4bf970b

Please sign in to comment.