Skip to content

Commit 8a37854

Browse files
committed
Fix #622 Handle multi-line value for field description
1 parent f5a6550 commit 8a37854

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/Hpack/Render.hs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,25 @@ sortStanzaFields sectionsFieldOrder = go
145145
Stanza name fields : xs | Just fieldOrder <- lookup name sectionsFieldOrder -> Stanza name (sortFieldsBy fieldOrder fields) : go xs
146146
x : xs -> x : go xs
147147

148+
descriptionFieldSize :: Int
149+
descriptionFieldSize = length ("description: " :: String)
150+
148151
formatDescription :: CabalVersion -> Alignment -> String -> String
149-
formatDescription cabalVersion (Alignment alignment) description = case formatDescription' cabalVersion description of
152+
formatDescription cabalVersion (Alignment alignment) description = case map emptyLineToDot $ lines description of
150153
x : xs -> intercalate "\n" (x : indent xs)
151154
[] -> ""
152155
where
153156
n :: Int
154-
n = max alignment (length ("description: " :: String))
157+
n = max alignment descriptionFieldSize
155158

156159
indentation :: String
157160
indentation = replicate n ' '
158161

162+
emptyLineToDot :: String -> String
163+
emptyLineToDot xs
164+
| isEmptyLine xs && cabalVersion < makeCabalVersion [3] = "."
165+
| otherwise = xs
166+
159167
indent :: [String] -> [String]
160168
indent = map indentLine
161169

@@ -164,16 +172,8 @@ formatDescription cabalVersion (Alignment alignment) description = case formatDe
164172
| isEmptyLine xs = ""
165173
| otherwise = indentation ++ xs
166174

167-
isEmptyLine :: String -> Bool
168-
isEmptyLine = all isSpace
169-
170-
formatDescription' :: CabalVersion -> String -> [String]
171-
formatDescription' cabalVersion = map emptyLineToDot . lines
172-
where
173-
emptyLineToDot :: String -> String
174-
emptyLineToDot xs
175-
| isEmptyLine xs && cabalVersion < makeCabalVersion [3] = "."
176-
| otherwise = xs
175+
isEmptyLine :: String -> Bool
176+
isEmptyLine = all isSpace
177177

178178
renderSourceRepository :: SourceRepository -> Element
179179
renderSourceRepository SourceRepository{..} = Stanza "source-repository head" [
@@ -188,8 +188,11 @@ renderFlag cabalVersion Flag {..} = Stanza ("flag " ++ flagName) $ description +
188188
, Field "default" (Literal $ show flagDefault)
189189
]
190190
where
191-
description = maybe [] (return . Field "description" . LineSeparatedList) formattedFlagDescription
192-
formattedFlagDescription = formatDescription' cabalVersion <$> flagDescription
191+
description = maybe [] (return . Field "description" . Literal) formattedFlagDescription
192+
-- We have to 'hard code' that the flag stanza's description field is
193+
-- indented by two spaces:
194+
alignment = Alignment (descriptionFieldSize + 2)
195+
formattedFlagDescription = formatDescription cabalVersion alignment <$> flagDescription
193196

194197
renderInternalLibraries :: Map String (Section Library) -> RenderM [Element]
195198
renderInternalLibraries = traverse renderInternalLibrary . Map.toList

test/Hpack/RenderSpec.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ spec = do
276276
let flag = (Flag "foo" (Just "some flag") True False)
277277
render defaultRenderSettings 0 (renderFlag cabalVersion flag) `shouldBe` [
278278
"flag foo"
279-
, " description:"
280-
, " some flag"
279+
, " description: some flag"
281280
, " manual: True"
282281
, " default: False"
283282
]

0 commit comments

Comments
 (0)