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

Skip indentation for empty line in multiline encoding #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ encodeLines :: Int -> [Text] -> Builder
encodeLines level ls =
mconcat $
(prefix :) $
intersperse (bs "\n" <> indent level) $ map (b . Text.Encoding.encodeUtf8) ls
intersperseOn (/= "") (bs "\n" <> indent level) (bs "\n") $ map Text.Encoding.encodeUtf8 ls
where
intersperseOn _ _ _ [] = []
intersperseOn cond thenClause elseClause (x:xs) = b x : map go xs
where
go x'
| cond x' = thenClause <> b x'
| otherwise = elseClause <> b x'
prefix =
mconcat
[ bs "|"
Expand Down
5 changes: 5 additions & 0 deletions test/Test/Data/Aeson/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tcDataTypes =
"leadingSymbol" : "!leading symbol",
"asteriskString": "*",
"multiLine": "The first line is followed by the\nsecond line\n",
"multiLineWithEmptyLines": "The first line is followed by an empty line\n\nthird line\n",
"multiLineWithSpaces": " This has extra\n spaces at the beginning\n",
"notMultiline": "This won't be\nmulti-lined",
"list": ["foo", "bar", "baz"],
Expand All @@ -86,6 +87,10 @@ listEmpty: []
multiLine: |
The first line is followed by the
second line
multiLineWithEmptyLines: |
The first line is followed by an empty line

third line
multiLineWithSpaces: |2
This has extra
spaces at the beginning
Expand Down