Skip to content

Commit 60c6386

Browse files
committed
fix: indent + operator when concatenating paths on new line
Closes #327
1 parent a49757a commit 60c6386

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/Nixfmt/Pretty.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,12 @@ prettyApp indentFunction pre hasPost f a =
502502
(\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post)
503503
<> (if hasPost && not (null comment') then hardline else mempty)
504504

505-
prettyOp :: Bool -> Expression -> Leaf -> Doc
506-
prettyOp forceFirstTermWide operation op =
505+
isPath :: Term -> Bool
506+
isPath (Path _) = True
507+
isPath _ = False
508+
509+
prettyOp :: Bool -> Bool -> Expression -> Leaf -> Doc
510+
prettyOp forceFirstTermWide indentPlusOps operation op =
507511
let -- Walk the operation tree and put a list of things on the same level.
508512
-- We still need to keep the operators around because they might have comments attached to them.
509513
-- An operator is put together with its succeeding expression. Only the first operand has none.
@@ -526,7 +530,9 @@ prettyOp forceFirstTermWide operation op =
526530
prettyOperation (Nothing, expr) = pretty expr
527531
-- The others
528532
prettyOperation (Just op', expr) =
529-
line <> pretty (moveTrailingCommentUp op') <> nest (absorbOperation expr)
533+
if indentPlusOps && value op' == TPlus
534+
then line <> nest (pretty (moveTrailingCommentUp op') <> absorbOperation expr)
535+
else line <> pretty (moveTrailingCommentUp op') <> nest (absorbOperation expr)
530536
in group' RegularG $
531537
(concatMap prettyOperation . flatten Nothing) operation
532538

@@ -645,7 +651,7 @@ absorbRHS expr = case expr of
645651
| isAbsorbableTerm t
646652
&& matchFirstToken (not . hasPreTrivia) t
647653
&& isUpdateConcatPlus value ->
648-
hardspace <> prettyOp True expr op
654+
hardspace <> prettyOp True (value == TPlus && isPath t) expr op
649655
-- Case 2: LHS fits onto first line, RHS is an absorbable term
650656
(Operation l (LoneAnn op) (Term t))
651657
| isAbsorbable t && isUpdateConcatPlus op ->
@@ -740,7 +746,7 @@ instance Pretty Expression where
740746
| op' == TLess || op' == TGreater || op' == TLessEqual || op' == TGreaterEqual || op' == TEqual || op' == TUnequal =
741747
pretty a <> softline <> pretty op <> hardspace <> pretty b
742748
-- all other operators
743-
pretty operation@(Operation _ op _) = prettyOp False operation op
749+
pretty operation@(Operation _ op _) = prettyOp False False operation op
744750
pretty (MemberCheck expr qmark sel) =
745751
pretty expr
746752
<> softline
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let
2+
aLongVariableNameOrEvenDeepAttrsetGettingAssignedAnAppendedToLocalPath = ./.
3+
+ "some/string/path/that/forces/a/wrap/to/happen";
4+
multiplePathConcatenationsWithLongNamesAndPathsThatDefinitelyWrap = ./base
5+
+ "/first/very/long/path/component"
6+
+ "/second/even/longer/path/component/that/continues";
7+
in
8+
"example"

0 commit comments

Comments
 (0)