From 708184242a456e4a48aeaff8bc3e2f08876054aa Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Fri, 19 Jul 2024 03:44:45 +0200 Subject: [PATCH] Always preserve expanded attribute parameters --- src/Nixfmt/Pretty.hs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index 0bd91f5f..004c606d 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -358,10 +358,10 @@ instance Pretty Parameter where -- param: pretty (IDParameter i) = pretty i -- {}: - pretty (SetParameter bopen [] bclose) = - group $ pretty (moveTrailingCommentUp bopen) <> hardspace <> pretty bclose + pretty (SetParameter bopen@Ann{sourceLine = openLine} [] bclose@Ann{sourceLine = closeLine}) = + group $ pretty (moveTrailingCommentUp bopen) <> (if openLine /= closeLine then hardline else hardspace) <> pretty bclose -- { stuff }: - pretty (SetParameter bopen attrs bclose) = + pretty (SetParameter bopen@Ann{sourceLine = openLine} attrs bclose@Ann{sourceLine = closeLine}) = group $ pretty (moveTrailingCommentUp bopen) <> surroundWith sep (nest $ sepBy sep $ handleTrailingComma $ map moveParamAttrComment $ moveParamsComments attrs) @@ -376,14 +376,17 @@ instance Pretty Parameter where [pretty (ParamAttr name maybeDefault Nothing) <> trailing ","] handleTrailingComma (x : xs) = pretty x : handleTrailingComma xs - sep = case attrs of - [ParamEllipsis _] -> line - -- Attributes must be without default - [ParamAttr _ Nothing _] -> line - [ParamAttr _ Nothing _, ParamEllipsis _] -> line - [ParamAttr _ Nothing _, ParamAttr _ Nothing _] -> line - [ParamAttr _ Nothing _, ParamAttr _ Nothing _, ParamEllipsis _] -> line - _ -> hardline + sep = + if openLine /= closeLine + then hardline + else case attrs of + [ParamEllipsis _] -> line + -- Attributes must be without default + [ParamAttr _ Nothing _] -> line + [ParamAttr _ Nothing _, ParamEllipsis _] -> line + [ParamAttr _ Nothing _, ParamAttr _ Nothing _] -> line + [ParamAttr _ Nothing _, ParamAttr _ Nothing _, ParamEllipsis _] -> line + _ -> hardline pretty (ContextParameter param1 at param2) = pretty param1 <> pretty at <> pretty param2