Skip to content

Commit 9785810

Browse files
authored
Fix bug in pretty-printing with expressions (#2392)
Before this change the question mark would be escaped by the pretty-printer
1 parent d5b61c3 commit 9785810

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

dhall/src/Dhall/Pretty/Internal.hs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,12 @@ prettyLabel = prettyLabelShared False
529529
prettyAnyLabel :: Text -> Doc Ann
530530
prettyAnyLabel = prettyLabelShared True
531531

532-
prettyAnyLabels :: Foldable list => list (Maybe Src, Text, Maybe Src) -> Doc Ann
533-
prettyAnyLabels keys = Pretty.group (Pretty.flatAlt long short)
532+
prettyKeys
533+
:: Foldable list
534+
=> (key -> Doc Ann)
535+
-> list (Maybe Src, key, Maybe Src)
536+
-> Doc Ann
537+
prettyKeys prettyK keys = Pretty.group (Pretty.flatAlt long short)
534538
where
535539
short = (mconcat . Pretty.punctuate dot . map prettyKey . toList) keys
536540

@@ -550,7 +554,7 @@ prettyAnyLabels keys = Pretty.group (Pretty.flatAlt long short)
550554
. Pretty.punctuate Pretty.hardline
551555
. Data.Maybe.catMaybes
552556
$ [ renderSrcMaybe mSrc0
553-
, Just (prettyAnyLabel key)
557+
, Just (prettyK key)
554558
, renderSrcMaybe mSrc1
555559
]
556560

@@ -829,11 +833,11 @@ prettyPrinters characterSet =
829833
<> Pretty.align (keyword "with" <> " " <> update)
830834

831835
(update, _) =
832-
prettyKeyValue prettyOperatorExpression equals
833-
(makeKeyValue (fmap toText b) c)
836+
prettyKeyValue prettyKey prettyOperatorExpression equals
837+
(makeKeyValue b c)
834838

835-
toText WithQuestion = "?"
836-
toText (WithLabel k ) = k
839+
prettyKey (WithLabel text) = prettyAnyLabel text
840+
prettyKey WithQuestion = syntax "?"
837841
prettyExpression (Assert a) =
838842
Pretty.group (Pretty.flatAlt long short)
839843
where
@@ -1417,11 +1421,12 @@ prettyPrinters characterSet =
14171421

14181422
prettyKeyValue
14191423
:: Pretty a
1420-
=> (Expr Src a -> Doc Ann)
1424+
=> (key -> Doc Ann)
1425+
-> (Expr Src a -> Doc Ann)
14211426
-> Doc Ann
1422-
-> KeyValue Src a
1427+
-> KeyValue key Src a
14231428
-> (Doc Ann, Doc Ann)
1424-
prettyKeyValue prettyValue separator (KeyValue key mSrc val) =
1429+
prettyKeyValue prettyKey prettyValue separator (KeyValue key mSrc val) =
14251430
duplicate (Pretty.group (Pretty.flatAlt long short))
14261431
where
14271432
completion _T r =
@@ -1433,7 +1438,7 @@ prettyPrinters characterSet =
14331438
_ ->
14341439
prettySelectorExpression r
14351440

1436-
short = prettyAnyLabels key
1441+
short = prettyKeys prettyKey key
14371442
<> " "
14381443
<> separator
14391444
<> " "
@@ -1443,7 +1448,7 @@ prettyPrinters characterSet =
14431448
<> prettyValue val
14441449

14451450
long = Pretty.align
1446-
( prettyAnyLabels key
1451+
( prettyKeys prettyKey key
14471452
<> preSeparator
14481453
)
14491454
<> separator
@@ -1533,7 +1538,7 @@ prettyPrinters characterSet =
15331538
prettyRecord :: Pretty a => Map Text (RecordField Src a) -> Doc Ann
15341539
prettyRecord =
15351540
( braces
1536-
. map (prettyKeyValue prettyExpression colon . adapt)
1541+
. map (prettyKeyValue prettyAnyLabel prettyExpression colon . adapt)
15371542
. Map.toList
15381543
)
15391544
where
@@ -1590,12 +1595,12 @@ prettyPrinters characterSet =
15901595
| Var (V key' 0) <- Dhall.Syntax.shallowDenote val
15911596
, key == key'
15921597
, not (containsComment mSrc2) ->
1593-
duplicate (prettyAnyLabels [(mSrc0, key, mSrc1)])
1598+
duplicate (prettyKeys prettyAnyLabel [(mSrc0, key, mSrc1)])
15941599
_ ->
1595-
prettyKeyValue prettyExpression equals kv
1600+
prettyKeyValue prettyAnyLabel prettyExpression equals kv
15961601

15971602
prettyAlternative (key, Just val) =
1598-
prettyKeyValue prettyExpression colon (makeKeyValue (pure key) val)
1603+
prettyKeyValue prettyAnyLabel prettyExpression colon (makeKeyValue (pure key) val)
15991604
prettyAlternative (key, Nothing) =
16001605
duplicate (prettyAnyLabel key)
16011606

@@ -1793,24 +1798,25 @@ escapeTrailingSingleQuote chunks@(Chunks as b) =
17931798
pretty_ :: Pretty a => a -> Text
17941799
pretty_ = prettyToStrictText
17951800

1796-
data KeyValue s a = KeyValue
1797-
{ _keyValueKeys :: NonEmpty (Maybe s, Text, Maybe s)
1801+
data KeyValue k s a = KeyValue
1802+
{ _keyValueKeys :: NonEmpty (Maybe s, k , Maybe s)
17981803
, _keyValueSrc :: Maybe s
17991804
, _keyValueValue :: Expr s a
18001805
}
18011806

1802-
makeKeyValue :: NonEmpty Text -> Expr s a -> KeyValue s a
1807+
makeKeyValue :: NonEmpty key -> Expr s a -> KeyValue key s a
18031808
makeKeyValue keys expr = KeyValue (adapt <$> keys) Nothing expr
18041809
where
18051810
adapt key = (Nothing, key, Nothing)
18061811

18071812
{- This utility function converts
18081813
`{ x = { y = { z = 1 } } }` to `{ x.y.z = 1 }`
18091814
-}
1810-
consolidateRecordLiteral :: Map Text (RecordField Src a) -> [KeyValue Src a]
1815+
consolidateRecordLiteral
1816+
:: Map Text (RecordField Src a) -> [KeyValue Text Src a]
18111817
consolidateRecordLiteral = concatMap adapt . Map.toList
18121818
where
1813-
adapt :: (Text, RecordField Src a) -> [KeyValue Src a]
1819+
adapt :: (Text, RecordField Src a) -> [KeyValue Text Src a]
18141820
adapt (key, RecordField mSrc0 val mSrc1 mSrc2)
18151821
| not (containsComment mSrc2)
18161822
, RecordLit m <- e
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
λ(x : Optional Natural) x with ? = 2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
λ(x : Optional Natural) x with ? = 2

0 commit comments

Comments
 (0)