From d24e159e5a70f916e0395a5757a4daef2d105c87 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 14 Mar 2024 16:43:13 +0100 Subject: [PATCH] fixup! Merge branch 'master' into rfc101-style --- src/Nixfmt/Pretty.hs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 989b8789..d0cf7b09 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -17,7 +17,7 @@ import qualified Data.Text as Text (null, takeWhile) import Nixfmt.Predoc (Doc, GroupAnn(..), Pretty, emptyline, group, group', hardline, hardspace, hcat, line, line', - nest, offset, newline, pretty, sepBy, surroundWith, softline, text, comment, trailingComment, trailing, textWidth, + nest, offset, newline, pretty, sepBy, surroundWith, softline, softline', text, comment, trailingComment, trailing, textWidth, unexpandSpacing') import Nixfmt.Types (Ann(..), Binder(..), Expression(..), Item(..), Items(..), Leaf, @@ -139,9 +139,18 @@ prettyTerm (Token t) = pretty t prettyTerm (SimpleString (Ann leading s trailing')) = pretty leading <> prettySimpleString s <> pretty trailing' prettyTerm (IndentedString (Ann leading s trailing')) = pretty leading <> prettyIndentedString s <> pretty trailing' prettyTerm (Path p) = pretty p -prettyTerm (Selection term selectors Nothing) = pretty term <> hcat selectors -prettyTerm (Selection term selectors (Just (kw, def))) = - pretty term <> hcat selectors <> hardspace <> pretty kw <> hardspace <> pretty def +prettyTerm (Selection term selectors rest) = + pretty term <> sep <> hcat selectors + <> pretty ((\(kw, def) -> softline <> nest (pretty kw <> hardspace <> pretty def)) <$> rest) + where + -- Selection (`foo.bar.baz`) case distinction on the first element (`foo`): + sep = case term of + -- If it is an ident, keep it all together + (Token _) -> mempty + -- If it is a parenthesized expression, maybe add a line break + (Parenthesized _ _ _) -> softline' + -- Otherwise, very likely add a line break + _ -> line' -- Empty list prettyTerm (List (Ann leading paropen Nothing) (Items []) (Ann [] parclose trailing'))