Skip to content

Commit 4bf970b

Browse files
authored
Fix formatting of multi-line strings that end with a single-quote (#1554)
Fixes #1547. Also fix haddocks for escapeLastLineLeadingWhitespace.
1 parent 1a831d1 commit 4bf970b

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

dhall/src/Dhall/Pretty/Internal.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,16 @@ prettyCharacterSet characterSet expression =
11471147
-- >>> multilineChunks (Chunks [] "\n\NUL\b\f\t")
11481148
-- Chunks [("\n",TextLit (Chunks [] "\NUL\b\f"))] "\t"
11491149
multilineChunks :: Chunks s a -> Chunks s a
1150-
multilineChunks = escapeControlCharacters . escapeLastLineLeadingWhitespace
1150+
multilineChunks =
1151+
escapeTrailingSingleQuote
1152+
. escapeControlCharacters
1153+
. escapeLastLineLeadingWhitespace
11511154

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

1237+
-- | Escape a trailing single quote by moving it into a string interpolation
1238+
--
1239+
-- Otherwise the multiline-string would end with @'''@, which would be parsed
1240+
-- as an escaped @''@.
1241+
--
1242+
-- >>> escapeTrailingSingleQuote (Chunks [] "\n'")
1243+
-- Chunks [("\n",TextLit (Chunks [] "'"))] ""
1244+
escapeTrailingSingleQuote :: Chunks s a -> Chunks s a
1245+
escapeTrailingSingleQuote chunks@(Chunks as b) =
1246+
case Text.unsnoc b of
1247+
Just (b', '\'') -> Chunks (as ++ [(b', TextLit (Chunks [] "'"))]) ""
1248+
_ -> chunks
1249+
12341250
-- | Pretty-print a value
12351251
pretty_ :: Pretty a => a -> Text
12361252
pretty_ = prettyToStrictText
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"\n'"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
''
2+
3+
${"'"}''

0 commit comments

Comments
 (0)