@@ -15,7 +15,8 @@ module Nix.Pretty where
1515
1616import Control.Applicative ( (<|>) )
1717import Control.Monad.Free
18- import Data.Fix ( Fix (.. ), foldFix )
18+ import Data.Fix ( Fix (.. )
19+ , foldFix )
1920import Data.HashMap.Lazy ( toList )
2021import qualified Data.HashMap.Lazy as M
2122import qualified Data.HashSet as HashSet
@@ -97,7 +98,7 @@ hasAttrOp = getSpecialOperator NHasAttrOp
9798wrapParens :: OperatorInfo -> NixDoc ann -> Doc ann
9899wrapParens op sub =
99100 bool
100- parens
101+ ( \ a -> " ( " <> a <> " ) " )
101102 id
102103 (precedence (rootOp sub) < precedence op
103104 || (precedence (rootOp sub) == precedence op
@@ -112,80 +113,81 @@ wrapPath :: OperatorInfo -> NixDoc ann -> Doc ann
112113wrapPath op sub =
113114 bool
114115 (wrapParens op sub)
115- (dquotes $ " $ " <> braces ( withoutParens sub) )
116+ (" \" ${ " <> withoutParens sub <> " } \" " )
116117 (wasPath sub)
117118
118119prettyString :: NString (NixDoc ann ) -> Doc ann
119- prettyString (DoubleQuoted parts) = dquotes . hcat . fmap prettyPart $ parts
120+ prettyString (DoubleQuoted parts) = " \" " <> ( mconcat . fmap prettyPart $ parts) <> " \" "
120121 where
122+ -- It serializes (@unpack@) Text -> String, because the helper code is done for String,
123+ -- please, can someone break that code.
121124 prettyPart (Plain t) = pretty . concatMap escape . unpack $ t
122125 prettyPart EscapedNewline = " ''\\ n"
123- prettyPart (Antiquoted r) = " $" <> braces ( withoutParens r)
126+ prettyPart (Antiquoted r) = " ${ " <> withoutParens r <> " } "
124127 escape ' "' = " \\\" "
125128 escape x =
126129 maybe
127130 [x]
128131 ((' \\ ' : ) . (: mempty ))
129132 (toEscapeCode x)
130133prettyString (Indented _ parts) = group $ nest 2 $ vcat
131- [dsquote , content, dsquote ]
134+ [" '' " , content, " '' " ]
132135 where
133- dsquote = squote <> squote
134136 content = vsep . fmap prettyLine . stripLastIfEmpty . splitLines $ parts
135137 stripLastIfEmpty = reverse . f . reverse where
136138 f ([Plain t] : xs) | Text. null (strip t) = xs
137139 f xs = xs
138140 prettyLine = hcat . fmap prettyPart
139141 prettyPart (Plain t) =
140- pretty . unpack . replace " ${" " ''${" . replace " ''" " '''" $ t
142+ pretty . replace " ${" " ''${" . replace " ''" " '''" $ t
141143 prettyPart EscapedNewline = " \\ n"
142- prettyPart (Antiquoted r) = " $" <> braces ( withoutParens r)
144+ prettyPart (Antiquoted r) = " ${ " <> withoutParens r <> " } "
143145
144146prettyParams :: Params (NixDoc ann ) -> Doc ann
145- prettyParams (Param n ) = pretty $ unpack n
147+ prettyParams (Param n ) = pretty n
146148prettyParams (ParamSet s v mname) = prettyParamSet s v <>
147- (\ name -> (" @" <> pretty (unpack name) ) `ifTrue` not (Text. null name)) `ifJust` mname
149+ (\ name -> (" @" <> pretty name) `ifTrue` not (Text. null name)) `ifJust` mname
148150
149151prettyParamSet :: ParamSet (NixDoc ann ) -> Bool -> Doc ann
150152prettyParamSet args var =
151153 encloseSep
152- (lbrace <> space)
153- (align (space <> rbrace) )
154+ " { "
155+ (align " } " )
154156 sep
155157 (fmap prettySetArg args <> prettyVariadic)
156158 where
157159 prettySetArg (n, maybeDef) =
158160 maybe
159- (pretty (unpack n) )
160- (\ x -> pretty (unpack n) <> " ? " <> withoutParens x)
161+ (pretty n )
162+ (\ x -> pretty n <> " ? " <> withoutParens x)
161163 maybeDef
162164 prettyVariadic = [ " ..." | var ]
163- sep = align (comma <> space)
165+ sep = align " , "
164166
165167prettyBind :: Binding (NixDoc ann ) -> Doc ann
166168prettyBind (NamedVar n v _p) =
167- prettySelector n <> space <> equals <> space <> withoutParens v <> semi
169+ prettySelector n <> " = " <> withoutParens v <> " ; "
168170prettyBind (Inherit s ns _p) =
169- " inherit " <> scope <> align (fillSep (fmap prettyKeyName ns)) <> semi
171+ " inherit " <> scope <> align (fillSep (fmap prettyKeyName ns)) <> " ; "
170172 where
171- scope = ((<> space ) . parens . withoutParens) `ifJust` s
173+ scope = ((<> " " ) . parens . withoutParens) `ifJust` s
172174
173175prettyKeyName :: NKeyName (NixDoc ann ) -> Doc ann
174- prettyKeyName (StaticKey " " ) = dquotes " "
175- prettyKeyName (StaticKey key) | HashSet. member key reservedNames =
176- dquotes $ pretty $ unpack key
177- prettyKeyName (StaticKey key) = pretty . unpack $ key
178- prettyKeyName ( DynamicKey key) = runAntiquoted
179- (DoubleQuoted [Plain " \n " ])
180- prettyString
181- (( " $ " <> ) . braces . withoutParens )
182- key
176+ prettyKeyName (StaticKey " " ) = " \"\ ""
177+ prettyKeyName (StaticKey key) | HashSet. member key reservedNames = " \" " <> pretty key <> " \" "
178+ prettyKeyName ( StaticKey key) = pretty key
179+ prettyKeyName (DynamicKey key) =
180+ runAntiquoted
181+ (DoubleQuoted [Plain " \n " ])
182+ prettyString
183+ ( \ x -> " ${ " <> withoutParens x <> " } " )
184+ key
183185
184186prettySelector :: NAttrPath (NixDoc ann ) -> Doc ann
185- prettySelector = hcat . punctuate dot . fmap prettyKeyName . NE. toList
187+ prettySelector = hcat . punctuate " . " . fmap prettyKeyName . NE. toList
186188
187189prettyAtom :: NAtom -> NixDoc ann
188- prettyAtom atom = simpleExpr $ pretty $ unpack $ atomText atom
190+ prettyAtom atom = simpleExpr $ pretty $ atomText atom
189191
190192prettyNix :: NExpr -> Doc ann
191193prettyNix = withoutParens . foldFix exprFNixDoc
@@ -259,7 +261,7 @@ exprFNixDoc = \case
259261 mkNixDoc opInfo $
260262 hsep
261263 [ wrapParens (f NAssocLeft ) r1
262- , pretty $ unpack $ operatorName opInfo
264+ , pretty $ operatorName opInfo
263265 , wrapParens (f NAssocRight ) r2
264266 ]
265267 where
@@ -269,17 +271,17 @@ exprFNixDoc = \case
269271 NUnary op r1 ->
270272 mkNixDoc
271273 opInfo
272- (pretty (unpack ( operatorName opInfo) ) <> wrapParens opInfo r1)
274+ (pretty (operatorName opInfo) <> wrapParens opInfo r1)
273275 where opInfo = getUnaryOperator op
274276 NSelect r' attr o ->
275277 (if isJust o then leastPrecedence else mkNixDoc selectOp)
276278 $ wrapPath selectOp r
277- <> dot
279+ <> " . "
278280 <> prettySelector attr
279281 <> ordoc
280282 where
281283 r = mkNixDoc selectOp (wrapParens appOpNonAssoc r')
282- ordoc = maybe mempty (((space <> " or " ) <> ) . wrapParens appOpNonAssoc) o
284+ ordoc = maybe mempty ((" or " <> ) . wrapParens appOpNonAssoc) o
283285 NHasAttr r attr ->
284286 mkNixDoc hasAttrOp (wrapParens hasAttrOp r <> " ? " <> prettySelector attr)
285287 NEnvPath p -> simpleExpr $ pretty (" <" <> p <> " >" )
@@ -295,7 +297,7 @@ exprFNixDoc = \case
295297 (" ./" <> _txt)
296298 _txt
297299 (any (`isPrefixOf` _txt) [" /" , " ~/" , " ./" , " ../" ])
298- NSym name -> simpleExpr $ pretty (unpack name)
300+ NSym name -> simpleExpr $ pretty name
299301 NLet binds body ->
300302 leastPrecedence $
301303 group $
@@ -316,17 +318,17 @@ exprFNixDoc = \case
316318 NWith scope body ->
317319 leastPrecedence $
318320 vsep
319- [" with " <> withoutParens scope <> semi , align $ withoutParens body]
321+ [" with " <> withoutParens scope <> " ; " , align $ withoutParens body]
320322 NAssert cond body ->
321323 leastPrecedence $
322324 vsep
323- [" assert " <> withoutParens cond <> semi , align $ withoutParens body]
324- NSynHole name -> simpleExpr $ pretty (" ^" <> unpack name)
325+ [" assert " <> withoutParens cond <> " ; " , align $ withoutParens body]
326+ NSynHole name -> simpleExpr $ pretty (" ^" <> name)
325327
326328valueToExpr :: forall t f m . MonadDataContext f m => NValue t f m -> NExpr
327329valueToExpr = iterNValue (\ _ _ -> thk) phi
328330 where
329- thk = Fix . NSym . pack $ " <expr>"
331+ thk = Fix . NSym $ " <expr>"
330332
331333 phi :: NValue' t f m NExpr -> NExpr
332334 phi (NVConstant' a ) = Fix $ NConstant a
@@ -336,9 +338,9 @@ valueToExpr = iterNValue (\_ _ -> thk) phi
336338 [ NamedVar (StaticKey k :| mempty ) v (fromMaybe nullPos (M. lookup k p))
337339 | (k, v) <- toList s
338340 ]
339- phi (NVClosure' _ _ ) = Fix . NSym . pack $ " <closure>"
341+ phi (NVClosure' _ _ ) = Fix . NSym $ " <closure>"
340342 phi (NVPath' p ) = Fix $ NLiteralPath p
341- phi (NVBuiltin' name _) = Fix . NSym . pack $ " builtins." <> name
343+ phi (NVBuiltin' name _) = Fix . NSym $ " builtins." <> pack name
342344 phi _ = error " Pattern synonyms foil completeness check"
343345
344346 mkStr ns = Fix $ NStr $ DoubleQuoted [Plain (stringIgnoreContext ns)]
@@ -356,20 +358,24 @@ prettyNValueProv
356358 )
357359 => NValue t f m
358360 -> Doc ann
359- prettyNValueProv v = do
360- let ps = citations @ m @ (NValue t f m ) v
361- case ps of
362- [] -> prettyNValue v
361+ prettyNValueProv v =
362+ case citations @ m @ (NValue t f m ) v of
363+ [] -> prettyNVal
363364 ps ->
364- let v' = prettyNValue v in
365365 fillSep
366- [ v'
367- , indent 2
368- $ parens
369- $ mconcat
370- $ " from: "
371- : fmap (prettyOriginExpr . _originExpr) ps
366+ [ prettyNVal
367+ , indent 2 $
368+ " (" <>
369+ mconcat (
370+ " from: "
371+ :
372+ fmap
373+ (prettyOriginExpr . _originExpr)
374+ ps
375+ ) <> " )"
372376 ]
377+ where
378+ prettyNVal = prettyNValue v
373379
374380prettyNThunk
375381 :: forall t f m ann
@@ -388,8 +394,7 @@ prettyNThunk t =
388394 $ fillSep
389395 [ v'
390396 , indent 2 $
391- parens $
392- mconcat $ " thunk from: " : fmap (prettyOriginExpr . _originExpr) ps
397+ " (" <> mconcat ( " thunk from: " : fmap (prettyOriginExpr . _originExpr) ps) <> " )"
393398 ]
394399
395400-- | This function is used only by the testing code.
0 commit comments