@@ -62,6 +62,7 @@ import Nixfmt.Types (
62
62
mapLastToken' ,
63
63
tokenText ,
64
64
)
65
+ import Nixfmt.Util (isSpaces )
65
66
import Prelude hiding (String )
66
67
67
68
toLineComment :: TrailingComment -> Trivium
@@ -412,7 +413,10 @@ prettyApp indentFunction pre hasPost f a =
412
413
<> pretty parclose
413
414
where
414
415
-- If the brackets are on different lines, keep them like that
415
- sur = if sourceLine paropen /= sourceLine parclose then hardline else line
416
+ sur
417
+ | sourceLine paropen /= sourceLine parclose = hardline
418
+ | null $ unItems items = hardspace
419
+ | otherwise = line
416
420
absorbInner expr = pretty expr
417
421
418
422
-- Render the last argument of a function call
@@ -536,6 +540,12 @@ isAbsorbable (Path _) = True
536
540
-- Non-empty sets and lists
537
541
isAbsorbable (Set _ _ (Items (_ : _)) _) = True
538
542
isAbsorbable (List _ (Items (_ : _)) _) = True
543
+ -- Empty sets and lists if they have a line break
544
+ -- https://github.com/NixOS/nixfmt/issues/253
545
+ isAbsorbable (Set _ (Ann {sourceLine = line1}) (Items [] ) (Ann {sourceLine = line2}))
546
+ | line1 /= line2 = True
547
+ isAbsorbable (List (Ann {sourceLine = line1}) (Items [] ) (Ann {sourceLine = line2}))
548
+ | line1 /= line2 = True
539
549
isAbsorbable (Parenthesized (LoneAnn _) (Term t) _) = isAbsorbable t
540
550
isAbsorbable _ = False
541
551
@@ -599,7 +609,11 @@ absorbRHS expr = case expr of
599
609
-- Special case `//` and `++` operations to be more compact in some cases
600
610
-- Case 1: two arguments, LHS is absorbable term, RHS fits onto the last line
601
611
(Operation (Term t) (LoneAnn op) b)
602
- | isAbsorbable t && isUpdateOrConcat op ->
612
+ | isAbsorbable t
613
+ && isUpdateOrConcat op
614
+ -- Exclude further operations on the RHS
615
+ -- Hotfix for https://github.com/NixOS/nixfmt/issues/198
616
+ && case b of (Operation {}) -> False ; _ -> True ->
603
617
group' RegularG $ line <> group' Priority (prettyTermWide t) <> line <> pretty op <> hardspace <> pretty b
604
618
-- Case 2a: LHS fits onto first line, RHS is an absorbable term
605
619
(Operation l (LoneAnn op) (Term t))
@@ -768,6 +782,9 @@ isSimple (Term (SimpleString (LoneAnn _))) = True
768
782
isSimple (Term (IndentedString (LoneAnn _))) = True
769
783
isSimple (Term (Path (LoneAnn _))) = True
770
784
isSimple (Term (Token (LoneAnn (Identifier _)))) = True
785
+ isSimple (Term (Token (LoneAnn (Integer _)))) = True
786
+ isSimple (Term (Token (LoneAnn (Float _)))) = True
787
+ isSimple (Term (Token (LoneAnn (EnvPath _)))) = True
771
788
isSimple (Term (Selection t selectors def)) =
772
789
isSimple (Term t) && all isSimpleSelector selectors && isNothing def
773
790
isSimple (Term (Parenthesized (LoneAnn _) e (LoneAnn _))) = isSimple e
@@ -805,11 +822,13 @@ instance Pretty StringPart where
805
822
(unexpandSpacing' (Just 30 ) whole')
806
823
807
824
instance Pretty [StringPart ] where
808
- -- When the interpolation is the only thing on the string line,
825
+ -- When the interpolation is the only thing on the string line (ignoring leading whitespace) ,
809
826
-- then absorb the content (i.e. don't surround with line').
810
827
-- Only do this when there are no comments
811
- pretty [Interpolation (Whole expr [] )] =
812
- group $ text " ${" <> nest inner <> text " }"
828
+ pretty [Interpolation (Whole expr [] )] = pretty [TextPart " " , Interpolation (Whole expr [] )]
829
+ pretty [TextPart pre, Interpolation (Whole expr [] )]
830
+ | isSpaces pre =
831
+ text pre <> offset (textWidth pre) (group $ text " ${" <> nest inner <> text " }" )
813
832
where
814
833
-- Code copied over from parentheses. Could be factored out into a common function one day
815
834
inner = case expr of
0 commit comments