Skip to content

Commit

Permalink
Merge #1019: print expressions with Nix2.4-like ${ escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha authored Jan 6, 2022
2 parents 084766c + f97d97d commit a9436eb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
30 changes: 20 additions & 10 deletions src/Nix/Pretty.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# language CPP #-}
{-# language AllowAmbiguousTypes #-}
{-# language ViewPatterns, PatternSynonyms, OverloadedStrings #-}


{-# options_ghc -fno-warn-name-shadowing #-}

Expand Down Expand Up @@ -99,20 +101,27 @@ wrapPath op sub =
("\"${" <> withoutParens sub <> "}\"")
(wasPath sub)


infixr 5 :<
pattern (:<) :: Char -> Text -> Text
pattern t :< ts <- (Text.uncons -> Just (t, ts))
where (:<) = Text.cons

escapeDoubleQuoteString :: Text -> Text
escapeDoubleQuoteString ('"':<xs) = "\\\"" <> escapeDoubleQuoteString xs
escapeDoubleQuoteString ('$':<'{':<xs) = "\\${" <> escapeDoubleQuoteString xs
escapeDoubleQuoteString ('$':<xs) = '$' :< escapeDoubleQuoteString xs
escapeDoubleQuoteString (x:<xs) = maybe (one x) (('\\' :<) . one) (toEscapeCode x)
<> escapeDoubleQuoteString xs
escapeDoubleQuoteString a = a


prettyString :: NString (NixDoc ann) -> Doc ann
prettyString (DoubleQuoted parts) = "\"" <> foldMap prettyPart parts <> "\""
where
-- It serializes Text -> String, because the helper code is done for String,
-- please, can someone break that code.
prettyPart (Plain t) = pretty . foldMap escape . toString $ t
prettyPart (Plain t) = pretty $ escapeDoubleQuoteString t
prettyPart EscapedNewline = "''\\n"
prettyPart (Antiquoted r) = "${" <> withoutParens r <> "}"
escape '"' = "\\\""
escape x =
maybe
(one x)
(('\\' :) . one)
(toEscapeCode x)
prettyString (Indented _ parts) = group $ nest 2 $ vcat
["''", content, "''"]
where
Expand Down Expand Up @@ -382,6 +391,7 @@ prettyNThunk t =
"(" <> fold (one "thunk from: " <> (prettyOriginExpr . _originExpr <$> ps)) <> ")"
]


-- | This function is used only by the testing code.
printNix :: forall t f m . MonadDataContext f m => NValue t f m -> Text
printNix = iterNValueByDiscardWith thk phi
Expand All @@ -390,7 +400,7 @@ printNix = iterNValueByDiscardWith thk phi

phi :: NValue' t f m Text -> Text
phi (NVConstant' a ) = atomText a
phi (NVStr' ns) = show $ ignoreContext ns
phi (NVStr' ns) = "\"" <> escapeDoubleQuoteString (ignoreContext ns) <> "\""
phi (NVList' l ) = "[ " <> unwords l <> " ]"
phi (NVSet' _ s) =
"{ " <>
Expand Down
1 change: 1 addition & 0 deletions tests/NixLanguageTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ newFailingTests = Set.fromList
, "eval-okay-path" -- #128
, "eval-okay-types"
, "eval-okay-fromTOML"
, "eval-okay-ind-string" -- #1000 #610
]

-- | Upstream tests that test cases that HNix disaded as a misfeature that is used so rarely
Expand Down
2 changes: 1 addition & 1 deletion tests/PrettyTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ case_string_antiquotation =
do
assertPretty
(mkStr "echo $foo")
"\"echo \\$foo\""
"\"echo $foo\""
assertPretty
(mkStr "echo ${foo}")
"\"echo \\${foo}\""
Expand Down
2 changes: 2 additions & 0 deletions tests/eval-compare/ind-string-18.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# to observe the upstream nix behavior for dollar sign excape in doublequote string
"\${foo \$ $foo"
4 changes: 4 additions & 0 deletions tests/eval-compare/ind-string-19.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# to observe the upstream nix behavior for dollar excape in Indented string
''
''$ $ ''${
''

0 comments on commit a9436eb

Please sign in to comment.