diff --git a/src/Nixfmt/Predoc.hs b/src/Nixfmt/Predoc.hs index 9741a5c3..d265bb37 100644 --- a/src/Nixfmt/Predoc.hs +++ b/src/Nixfmt/Predoc.hs @@ -166,9 +166,11 @@ trailing t = [Text 0 0 Trailing t] group :: (HasCallStack) => (Pretty a) => a -> Doc group x = pure . Group RegularG $ - if p /= [] && (isSoftSpacing (head p) || isSoftSpacing (last p)) - then error $ "group should not start or end with whitespace, use `group'` if you are sure; " <> show p - else p + case p of + (headP : _) + | isSoftSpacing headP || isSoftSpacing (last p) -> + error $ "group should not start or end with whitespace, use `group'` if you are sure; " <> show p + _ -> p where p = pretty x @@ -631,9 +633,9 @@ layoutGreedy tw iw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0 then let -- We know that the last printed character was a line break (cc == 0), -- therefore drop any leading whitespace within the group to avoid duplicate newlines - grp' = case head grp of - Spacing _ -> tail grp - Group ann ((Spacing _) : inner) -> Group ann inner : tail grp + grp' = case grp of + Spacing _ : tl -> tl + Group ann ((Spacing _) : inner) : tl -> Group ann inner : tl _ -> grp (nl, off) = nextIndent grp' diff --git a/src/Nixfmt/Types.hs b/src/Nixfmt/Types.hs index 1ddac3d0..d964eadd 100644 --- a/src/Nixfmt/Types.hs +++ b/src/Nixfmt/Types.hs @@ -372,7 +372,7 @@ instance LanguageElement Term where walkSubprograms = \case -- Map each item to a singleton list, then handle that - (List _ items _) | Prelude.length (unItems items) == 1 -> case Prelude.head (unItems items) of + (List _ (Items [singleItem]) _) -> case singleItem of (Item item) -> [Term item] (Comments _) -> [] (List open items close) -> @@ -381,7 +381,7 @@ instance LanguageElement Term where [Term (List (stripTrivia open) (Items [Item item]) (stripTrivia close))] Comments c -> [Term (List (stripTrivia open) (Items [Comments c]) (stripTrivia close))] - (Set _ _ items _) | Prelude.length (unItems items) == 1 -> case Prelude.head (unItems items) of + (Set _ _ (Items [singleItem]) _) -> case singleItem of (Item (Inherit _ from sels _)) -> (Term <$> maybeToList from) ++ concatMap walkSubprograms sels (Item (Assignment sels _ expr _)) ->