Skip to content

Commit 3a3e0d3

Browse files
committed
absorbRHS: Add TPlus to list of edge cases
1 parent a6dcf43 commit 3a3e0d3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/Nixfmt/Pretty.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,20 +637,22 @@ absorbRHS expr = case expr of
637637
(Application f a) -> nest $ prettyApp False line False f a
638638
(With{}) -> nest $ group' RegularG $ line <> pretty expr
639639
-- Special case `//` and `++` and `+` operations to be more compact in some cases
640+
-- The following code assumes all of these operators are parsed with right-handed associativity
641+
-- (even though in Nix, addition technically is considered left-associative)
640642
-- Case 1: LHS is absorbable term, unindent concatenations
641643
-- https://github.com/NixOS/nixfmt/issues/228
642644
(Operation (Term t) op@(Ann{value}) _)
643645
| isAbsorbableTerm t
644-
&& matchFirstToken (\Ann{preTrivia} -> preTrivia == []) t
645-
&& elem value [TUpdate, TConcat, TPlus] ->
646+
&& matchFirstToken (not . hasPreTrivia) t
647+
&& isUpdateConcatPlus value ->
646648
hardspace <> prettyOp True expr op
647649
-- Case 2a: LHS fits onto first line, RHS is an absorbable term
648650
(Operation l (LoneAnn op) (Term t))
649-
| isAbsorbable t && isUpdateOrConcat op ->
651+
| isAbsorbable t && isUpdateConcatPlus op ->
650652
nest $ group' RegularG $ line <> pretty l <> line <> group' Transparent (pretty op <> hardspace <> group' Priority (prettyTermWide t))
651653
-- Case 2b: LHS fits onto first line, RHS is a function application
652654
(Operation l (LoneAnn op) (Application f a))
653-
| isUpdateOrConcat op
655+
| isUpdateConcatPlus op
654656
&& matchFirstToken (not . hasPreTrivia) f ->
655657
nest $ line <> group l <> line <> prettyApp False (pretty op <> hardspace) False f a
656658
-- Everything else:
@@ -659,9 +661,10 @@ absorbRHS expr = case expr of
659661
-- Otherwise, start on new line, expand fully (including the semicolon)
660662
_ -> nest $ line <> group expr
661663
where
662-
isUpdateOrConcat TUpdate = True
663-
isUpdateOrConcat TConcat = True
664-
isUpdateOrConcat _ = False
664+
isUpdateConcatPlus TUpdate = True
665+
isUpdateConcatPlus TConcat = True
666+
isUpdateConcatPlus TPlus = True
667+
isUpdateConcatPlus _ = False
665668

666669
instance Pretty Expression where
667670
pretty (Term t) = pretty t

0 commit comments

Comments
 (0)