Skip to content

Commit 2db4b1f

Browse files
amesgenmrkkrp
authored andcommitted
Never fully collapse string gaps
1 parent 01117f4 commit 2db4b1f

File tree

7 files changed

+30
-7
lines changed

7 files changed

+30
-7
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
* Correctly format edge cases where fully collapsing string gaps changes the
4+
string represented by a string literal. [Issue
5+
1160](https://github.com/tweag/ormolu/issues/1160).
6+
17
## Ormolu 0.8.0.0
28

39
* Format multiple files in parallel. [Issue

data/examples/declaration/value/function/multiline-strings-0-out.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ s =
77
"""
88

99
s_2 =
10-
"""Line 1
10+
"""\ \Line 1
1111
Line 2
1212
Line 3
1313
"""

data/examples/declaration/value/function/multiline-strings-1-out.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
s =
44
"""
5-
a b c d e
5+
a b\ \ c d e
66
f g
77
"""
88

99
-- equivalent to
1010
s' = "a b c d e\nf g"
11+
12+
weirdGap = """\65\ \0"""

data/examples/declaration/value/function/multiline-strings-1.hs

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ s =
99

1010
-- equivalent to
1111
s' = "a b c d e\nf g"
12+
13+
weirdGap = """\65\ \0"""

data/examples/declaration/value/function/strings-out.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
foo = "foobar"
44

5-
bar = "foo\&barbaz"
5+
bar = "foo\&bar\ \baz"
66

77
baz =
88
"foo\
99
\bar\
1010
\baz"
11+
12+
weirdGap = "\65\ \0"

data/examples/declaration/value/function/strings.hs

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ bar = "foo\&bar\ \baz"
55
baz = "foo\
66
\bar\
77
\baz"
8+
9+
weirdGap = "\65\ \0"

src/Ormolu/Printer/Meat/Declaration/StringLiteral.hs

+13-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ p_stringLit src = case parseStringLiteral $ T.pack $ unpackFS src of
2828
case stringLiteralKind of
2929
RegularStringLiteral -> do
3030
let singleLine =
31-
txt $ T.concat segments
31+
txt $ intercalateMinimalStringGaps segments
3232
multiLine =
3333
sep breakpoint f (attachRelativePos segments)
3434
where
@@ -105,9 +105,9 @@ parseStringLiteral = \s -> do
105105
splitMultilineString :: Text -> [Text]
106106
splitMultilineString =
107107
splitGaps
108-
-- There is no reason to use gaps with multiline string literals, so
109-
-- we collapse them.
110-
>>> T.concat
108+
-- There is no reason to use gaps with multiline string literals just to
109+
-- emulate multi-line strings, so we replace them with "\\ \\".
110+
>>> intercalateMinimalStringGaps
111111
>>> splitNewlines
112112
>>> fmap expandLeadingTabs
113113
>>> rmCommonWhitespacePrefixAndBlank
@@ -150,3 +150,12 @@ parseStringLiteral = \s -> do
150150
| otherwise = (Just $ Min leadingSpace, T.drop commonWs l)
151151
where
152152
leadingSpace = T.length $ T.takeWhile is_space l
153+
154+
-- | Add minimal string gaps between string literal chunks. Such string gaps
155+
-- /can/ be semantically meaningful (so we preserve them for simplicity); for
156+
-- example:
157+
--
158+
-- >>> "\65\ \0" == "\650"
159+
-- False
160+
intercalateMinimalStringGaps :: [Text] -> Text
161+
intercalateMinimalStringGaps = T.intercalate "\\ \\"

0 commit comments

Comments
 (0)