Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Never fully collapse string gaps #1161

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

* Correctly format edge cases where fully collapsing string gaps changes the
string represented by a string literal. [Issue
1160](https://github.com/tweag/ormolu/issues/1160).

## Ormolu 0.8.0.0

* Format multiple files in parallel. [Issue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ s =
"""

s_2 =
"""Line 1
"""\ \Line 1
Line 2
Line 3
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

s =
"""
a b c d e
a b\ \ c d e
f g
"""

-- equivalent to
s' = "a b c d e\nf g"

weirdGap = """\65\ \0"""
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ s =

-- equivalent to
s' = "a b c d e\nf g"

weirdGap = """\65\ \0"""
4 changes: 3 additions & 1 deletion data/examples/declaration/value/function/strings-out.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

foo = "foobar"

bar = "foo\&barbaz"
bar = "foo\&bar\ \baz"

baz =
"foo\
\bar\
\baz"

weirdGap = "\65\ \0"
2 changes: 2 additions & 0 deletions data/examples/declaration/value/function/strings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ bar = "foo\&bar\ \baz"
baz = "foo\
\bar\
\baz"

weirdGap = "\65\ \0"
17 changes: 13 additions & 4 deletions src/Ormolu/Printer/Meat/Declaration/StringLiteral.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ p_stringLit src = case parseStringLiteral $ T.pack $ unpackFS src of
case stringLiteralKind of
RegularStringLiteral -> do
let singleLine =
txt $ T.concat segments
txt $ intercalateMinimalStringGaps segments
multiLine =
sep breakpoint f (attachRelativePos segments)
where
Expand Down Expand Up @@ -105,9 +105,9 @@ parseStringLiteral = \s -> do
splitMultilineString :: Text -> [Text]
splitMultilineString =
splitGaps
-- There is no reason to use gaps with multiline string literals, so
-- we collapse them.
>>> T.concat
-- There is no reason to use gaps with multiline string literals just to
-- emulate multi-line strings, so we replace them with "\\ \\".
>>> intercalateMinimalStringGaps
>>> splitNewlines
>>> fmap expandLeadingTabs
>>> rmCommonWhitespacePrefixAndBlank
Expand Down Expand Up @@ -150,3 +150,12 @@ parseStringLiteral = \s -> do
| otherwise = (Just $ Min leadingSpace, T.drop commonWs l)
where
leadingSpace = T.length $ T.takeWhile is_space l

-- | Add minimal string gaps between string literal chunks. Such string gaps
-- /can/ be semantically meaningful (so we preserve them for simplicity); for
-- example:
--
-- >>> "\65\ \0" == "\650"
-- False
intercalateMinimalStringGaps :: [Text] -> Text
intercalateMinimalStringGaps = T.intercalate "\\ \\"