Skip to content

Commit d65dd67

Browse files
committed
Fix #622 Handle multi-line value for field description
1 parent 05b8c03 commit d65dd67

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Changes in 0.38.3
2+
- Handle a multi-line value for the `description` key of a flag (see #623)
3+
14
## Changes in 0.38.2
25
- Infer `cabal-version: 3.12` when `PackageInfo_*` is used (see #620)
36

hpack.cabal

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spec-version: 0.36.0
22
name: hpack
3-
version: 0.38.2
3+
version: 0.38.3
44
synopsis: A modern format for Haskell packages
55
description: See README at <https://github.com/sol/hpack#readme>
66
author: Simon Hengel <[email protected]>

src/Hpack/Render.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ renderPackageWith settings headerFieldsAlignment existingFieldOrder sectionsFiel
104104
return $ concat [
105105
sourceRepository
106106
, customSetup
107-
, map renderFlag packageFlags
107+
, map (renderFlag packageCabalVersion) packageFlags
108108
, library
109109
, internalLibraries
110110
, executables
@@ -145,13 +145,16 @@ 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
149152
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 ' '
@@ -179,13 +182,17 @@ renderSourceRepository SourceRepository{..} = Stanza "source-repository head" [
179182
, Field "subdir" (maybe "" Literal sourceRepositorySubdir)
180183
]
181184

182-
renderFlag :: Flag -> Element
183-
renderFlag Flag {..} = Stanza ("flag " ++ flagName) $ description ++ [
185+
renderFlag :: CabalVersion -> Flag -> Element
186+
renderFlag cabalVersion Flag {..} = Stanza ("flag " ++ flagName) $ description ++ [
184187
Field "manual" (Literal $ show flagManual)
185188
, Field "default" (Literal $ show flagDefault)
186189
]
187190
where
188-
description = maybe [] (return . Field "description" . Literal) 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
189196

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

test/Hpack/RenderSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ spec = do
274274
describe "renderFlag" $ do
275275
it "renders flags" $ do
276276
let flag = (Flag "foo" (Just "some flag") True False)
277-
render defaultRenderSettings 0 (renderFlag flag) `shouldBe` [
277+
render defaultRenderSettings 0 (renderFlag cabalVersion flag) `shouldBe` [
278278
"flag foo"
279279
, " description: some flag"
280280
, " manual: True"

0 commit comments

Comments
 (0)