From e733986f3c4636cc5fbb6e989d463934251fe52d Mon Sep 17 00:00:00 2001 From: Georges Dubus Date: Sun, 28 Jan 2018 18:18:15 +0100 Subject: [PATCH] Add a pretty-printer for NValue The implementation is a bit hacky: we convert the NValue to a NExpr, which let us reuse the ast pretty-printing logic. This commit also moves `atomText` from `Nix.Pretty` to `Nix.Eval` to avoid a circular dependency. I don't have a strong argument for that, except that the pretty-printer depending from the evaluator makes a bit more sense than other way around. --- Nix/Eval.hs | 8 +++++++- Nix/Pretty.hs | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Nix/Eval.hs b/Nix/Eval.hs index 7335ac8..531f3cf 100644 --- a/Nix/Eval.hs +++ b/Nix/Eval.hs @@ -15,7 +15,6 @@ import qualified Data.Text as Text import Data.Traversable as T import Data.Typeable (Typeable) import GHC.Generics -import Nix.Pretty (atomText) import Nix.StringOperations (runAntiquoted) import Nix.Atoms import Nix.Expr @@ -58,6 +57,13 @@ valueText = cata phi where phi (NVLiteralPath p) = Text.pack p phi (NVEnvPath p) = Text.pack p +-- | Translate an atom into its nix representation. +atomText :: NAtom -> Text +atomText (NInt i) = Text.pack (show i) +atomText (NBool b) = if b then "true" else "false" +atomText NNull = "null" +atomText (NUri uri) = uri + buildArgument :: Params (NValue m) -> NValue m -> NValue m buildArgument paramSpec arg = either error (Fix . NVSet) $ case paramSpec of Param name -> return $ Map.singleton name arg diff --git a/Nix/Pretty.hs b/Nix/Pretty.hs index 3693147..0e603f6 100644 --- a/Nix/Pretty.hs +++ b/Nix/Pretty.hs @@ -8,6 +8,7 @@ import Data.Maybe (isJust) import Data.Text (Text, pack, unpack, replace, strip) import Data.List (isPrefixOf) import Nix.Atoms +import Nix.Eval (NValue, NValueF (..), atomText) import Nix.Expr import Nix.Parser.Library (reservedNames) import Nix.Parser.Operators @@ -106,13 +107,6 @@ prettyKeyName (DynamicKey key) = runAntiquoted prettyString withoutParens key prettySelector :: NAttrPath NixDoc -> Doc prettySelector = hcat . punctuate dot . map prettyKeyName --- | Translate an atom into its nix representation. -atomText :: NAtom -> Text -atomText (NInt i) = pack (show i) -atomText (NBool b) = if b then "true" else "false" -atomText NNull = "null" -atomText (NUri uri) = uri - prettyAtom :: NAtom -> NixDoc prettyAtom atom = simpleExpr $ text $ unpack $ atomText atom @@ -175,3 +169,20 @@ prettyNix = withoutParens . cata phi where text "assert" <+> withoutParens cond <> semi <+> withoutParens body recPrefix = text "rec" <> space + +prettyNixValue :: Functor m => NValue m -> Doc +prettyNixValue = prettyNix . valueToExpr + where valueToExpr :: Functor m => NValue m -> NExpr + valueToExpr = hmap go + -- hmap does the recursive conversion from NValue to NExpr + -- fun fact: it is not defined in data-fixed, but I was certain it should exists so I found it in unification-fd by hoogling its type + hmap :: (Functor f, Functor g) => (forall a. f a -> g a) -> Fix f -> Fix g + hmap eps = ana (eps . unFix) + go (NVConstant a) = NConstant a + go (NVStr t) = NStr (DoubleQuoted [Plain t]) + go (NVList l) = NList l + go (NVSet s) = NSet [NamedVar [StaticKey k] v | (k, v) <- toList s] + go (NVFunction p _) = NSym . pack $ ("") + go (NVLiteralPath fp) = NLiteralPath fp + go (NVEnvPath p) = NEnvPath p +