Skip to content

Commit 4e3aa9b

Browse files
committed
Pretty: refactor
1 parent 6e23add commit 4e3aa9b

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

src/Nix/Eval.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ addStackFrames f v =
516516
scopes <- currentScopes :: m (Scopes m v)
517517

518518
-- sectioning gives GHC optimization
519+
-- If opimization question would arrive again, check the @(`withFrameInfo` f v) $ EvaluatingExpr scopes v@
520+
-- for possible @scopes@ implementation @v@ type arguments sharing between runs.
519521
(`withFrameInfo` f v) $ (`EvaluatingExpr` v) scopes
520522
where
521523
withFrameInfo = withFrame Info

src/Nix/Pretty.hs

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -214,40 +214,53 @@ exprFNixDoc = \case
214214
NStr str -> simpleExpr $ prettyString str
215215
NList [] -> simpleExpr $ lbracket <> rbracket
216216
NList xs ->
217-
simpleExpr
218-
$ group
219-
$ nest 2
220-
$ vsep
221-
$ concat
222-
$ [[lbracket], fmap (wrapParens appOpNonAssoc) xs, [rbracket]]
217+
simpleExpr $
218+
group $
219+
nest 2 $
220+
vsep $
221+
concat
222+
[ [lbracket]
223+
, wrapParens appOpNonAssoc <$>
224+
xs
225+
, [rbracket]]
223226
NSet NNonRecursive [] -> simpleExpr $ lbrace <> rbrace
224227
NSet NNonRecursive xs ->
225-
simpleExpr
226-
$ group
227-
$ nest 2
228-
$ vsep
229-
$ concat
230-
$ [[lbrace], fmap prettyBind xs, [rbrace]]
228+
simpleExpr $
229+
group $
230+
nest 2 $
231+
vsep $
232+
concat
233+
[ [lbrace]
234+
, prettyBind <$> xs
235+
, [rbrace]
236+
]
231237
NSet NRecursive [] -> simpleExpr $ recPrefix <> lbrace <> rbrace
232238
NSet NRecursive xs ->
233-
simpleExpr
234-
$ group
235-
$ nest 2
236-
$ vsep
237-
$ concat
238-
$ [[recPrefix <> lbrace], fmap prettyBind xs, [rbrace]]
239+
simpleExpr $
240+
group $
241+
nest 2 $
242+
vsep $
243+
concat
244+
[ [recPrefix <> lbrace]
245+
, prettyBind <$> xs
246+
, [rbrace]
247+
]
239248
NAbs args body ->
240-
leastPrecedence
241-
$ nest 2
242-
$ vsep
243-
$ [prettyParams args <> colon, withoutParens body]
249+
leastPrecedence $
250+
nest 2 $
251+
vsep
252+
[ prettyParams args <> colon
253+
, withoutParens body
254+
]
244255
NBinary NApp fun arg ->
245-
mkNixDoc appOp (wrapParens appOp fun <> space <> wrapParens appOpNonAssoc arg)
246-
NBinary op r1 r2 -> mkNixDoc opInfo $ hsep
247-
[ wrapParens (f NAssocLeft) r1
248-
, pretty $ unpack $ operatorName opInfo
249-
, wrapParens (f NAssocRight) r2
250-
]
256+
mkNixDoc appOp (wrapParens appOp fun <> space <> wrapParens appOpNonAssoc arg)
257+
NBinary op r1 r2 ->
258+
mkNixDoc opInfo $
259+
hsep
260+
[ wrapParens (f NAssocLeft) r1
261+
, pretty $ unpack $ operatorName opInfo
262+
, wrapParens (f NAssocRight) r2
263+
]
251264
where
252265
opInfo = getBinaryOperator op
253266
f x | associativity opInfo /= x = opInfo { associativity = NAssocNone }
@@ -283,32 +296,33 @@ exprFNixDoc = \case
283296
(any (`isPrefixOf` _txt) ["/", "~/", "./", "../"])
284297
NSym name -> simpleExpr $ pretty (unpack name)
285298
NLet binds body ->
286-
leastPrecedence
287-
$ group
288-
$ vsep
289-
$ [ "let"
290-
, indent 2 (vsep (fmap prettyBind binds))
291-
, "in " <> withoutParens body
292-
]
299+
leastPrecedence $
300+
group $
301+
vsep
302+
[ "let"
303+
, indent 2 (vsep (fmap prettyBind binds))
304+
, "in " <> withoutParens body
305+
]
293306
NIf cond trueBody falseBody ->
294-
leastPrecedence
295-
$ group
296-
$ nest 2
297-
$ vsep
298-
$ [ "if " <> withoutParens cond
299-
, align ("then " <> withoutParens trueBody)
300-
, align ("else " <> withoutParens falseBody)
301-
]
307+
leastPrecedence $
308+
group $
309+
nest 2 $
310+
sep
311+
[ "if " <> withoutParens cond
312+
, align ("then " <> withoutParens trueBody)
313+
, align ("else " <> withoutParens falseBody)
314+
]
302315
NWith scope body ->
303-
leastPrecedence
304-
$ vsep
305-
$ ["with " <> withoutParens scope <> semi, align $ withoutParens body]
316+
leastPrecedence $
317+
vsep
318+
["with " <> withoutParens scope <> semi, align $ withoutParens body]
306319
NAssert cond body ->
307-
leastPrecedence
308-
$ vsep
309-
$ ["assert " <> withoutParens cond <> semi, align $ withoutParens body]
320+
leastPrecedence $
321+
vsep
322+
["assert " <> withoutParens cond <> semi, align $ withoutParens body]
310323
NSynHole name -> simpleExpr $ pretty ("^" <> unpack name)
311-
where recPrefix = "rec" <> space
324+
where
325+
recPrefix = "rec" <> space
312326

313327
valueToExpr :: forall t f m . MonadDataContext f m => NValue t f m -> NExpr
314328
valueToExpr = iterNValue (\_ _ -> thk) phi

0 commit comments

Comments
 (0)