Skip to content

Commit a9436eb

Browse files
Merge #1019: print expressions with Nix2.4-like ${ escaping
2 parents 084766c + f97d97d commit a9436eb

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

src/Nix/Pretty.hs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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+
102119
prettyString :: NString (NixDoc ann) -> Doc ann
103120
prettyString (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)
116125
prettyString (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.
386396
printNix :: forall t f m . MonadDataContext f m => NValue t f m -> Text
387397
printNix = 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
"{ " <>

tests/NixLanguageTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ newFailingTests = Set.fromList
6161
, "eval-okay-path" -- #128
6262
, "eval-okay-types"
6363
, "eval-okay-fromTOML"
64+
, "eval-okay-ind-string" -- #1000 #610
6465
]
6566

6667
-- | Upstream tests that test cases that HNix disaded as a misfeature that is used so rarely

tests/PrettyTests.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ case_string_antiquotation =
2323
do
2424
assertPretty
2525
(mkStr "echo $foo")
26-
"\"echo \\$foo\""
26+
"\"echo $foo\""
2727
assertPretty
2828
(mkStr "echo ${foo}")
2929
"\"echo \\${foo}\""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# to observe the upstream nix behavior for dollar sign excape in doublequote string
2+
"\${foo \$ $foo"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# to observe the upstream nix behavior for dollar excape in Indented string
2+
''
3+
''$ $ ''${
4+
''

0 commit comments

Comments
 (0)