11{-# language CPP #-}
22{-# language AllowAmbiguousTypes #-}
3+ {-# language ViewPatterns, PatternSynonyms, OverloadedStrings #-}
4+
35
46{-# options_ghc -fno-warn-name-shadowing #-}
57
@@ -99,20 +101,27 @@ wrapPath op sub =
99101 (" \" ${" <> withoutParens sub <> " }\" " )
100102 (wasPath sub)
101103
104+
105+ infixr 5 :<
106+ pattern (:<) :: Char -> Text -> Text
107+ pattern t :< ts <- (Text. uncons -> Just (t, ts))
108+ where (:<) = Text. cons
109+
110+ escapeDoubleQuoteString :: Text -> Text
111+ escapeDoubleQuoteString (' "' :< xs) = " \\\" " <> escapeDoubleQuoteString xs
112+ escapeDoubleQuoteString (' $' :< ' {' :< xs) = " \\ ${" <> escapeDoubleQuoteString xs
113+ escapeDoubleQuoteString (' $' :< xs) = ' $' :< escapeDoubleQuoteString xs
114+ escapeDoubleQuoteString (x:< xs) = maybe (one x) ((' \\ ' :< ) . one) (toEscapeCode x)
115+ <> escapeDoubleQuoteString xs
116+ escapeDoubleQuoteString a = a
117+
118+
102119prettyString :: NString (NixDoc ann ) -> Doc ann
103120prettyString (DoubleQuoted parts) = " \" " <> foldMap prettyPart parts <> " \" "
104121 where
105- -- It serializes Text -> String, because the helper code is done for String,
106- -- please, can someone break that code.
107- prettyPart (Plain t) = pretty . foldMap escape . toString $ t
122+ prettyPart (Plain t) = pretty $ escapeDoubleQuoteString t
108123 prettyPart EscapedNewline = " ''\\ n"
109124 prettyPart (Antiquoted r) = " ${" <> withoutParens r <> " }"
110- escape ' "' = " \\\" "
111- escape x =
112- maybe
113- (one x)
114- ((' \\ ' : ) . one)
115- (toEscapeCode x)
116125prettyString (Indented _ parts) = group $ nest 2 $ vcat
117126 [" ''" , content, " ''" ]
118127 where
@@ -382,6 +391,7 @@ prettyNThunk t =
382391 " (" <> fold (one " thunk from: " <> (prettyOriginExpr . _originExpr <$> ps)) <> " )"
383392 ]
384393
394+
385395-- | This function is used only by the testing code.
386396printNix :: forall t f m . MonadDataContext f m => NValue t f m -> Text
387397printNix = iterNValueByDiscardWith thk phi
@@ -390,7 +400,7 @@ printNix = iterNValueByDiscardWith thk phi
390400
391401 phi :: NValue' t f m Text -> Text
392402 phi (NVConstant' a ) = atomText a
393- phi (NVStr' ns) = show $ ignoreContext ns
403+ phi (NVStr' ns) = " \" " <> escapeDoubleQuoteString ( ignoreContext ns) <> " \" "
394404 phi (NVList' l ) = " [ " <> unwords l <> " ]"
395405 phi (NVSet' _ s) =
396406 " { " <>
0 commit comments