From bc9cdf03c4d587c891809cbba484a247c71c6ceb Mon Sep 17 00:00:00 2001 From: piegames Date: Wed, 2 Oct 2024 15:45:04 +0200 Subject: [PATCH 1/6] tests: Add function calls with list-heavy arguments --- test/diff/apply_with_lists/in.nix | 78 +++++++++++++ test/diff/apply_with_lists/out-pure.nix | 134 +++++++++++++++++++++++ test/diff/apply_with_lists/out.nix | 139 ++++++++++++++++++++++++ 3 files changed, 351 insertions(+) create mode 100644 test/diff/apply_with_lists/in.nix create mode 100644 test/diff/apply_with_lists/out-pure.nix create mode 100644 test/diff/apply_with_lists/out.nix diff --git a/test/diff/apply_with_lists/in.nix b/test/diff/apply_with_lists/in.nix new file mode 100644 index 0000000..86eebd2 --- /dev/null +++ b/test/diff/apply_with_lists/in.nix @@ -0,0 +1,78 @@ +# This file contains an assortment of test cases involving list-heavy function calls + +[ + (f [ ] [ rhs lhs ]) + (lib.mkMerge [ false false ]) + (replaceStrings + [ "\${" "''" ] + #force multiline + [ "''\${" "'''" ]) + (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) + (replaceStrings + [ ''"'' "\\" ] + # force multiline + [ ''\"'' "\\\\" ] + name) + (replaceStrings + [ "@" ":" "\\" "[" "]" ] + [ "-" "-" "-" "" "" ]) + (lists.removePrefix [ 1 2 ] [ ]) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ 1 2 ] [ ]) + (builtins.replaceStrings + [ "@NIX_STORE_VERITY@" ] + [partitionTypes.usr-verity] + (builtins.readFile ./assert_uki_repart_match.py)) + (replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget)) + (lib.mkChangedOptionModule + [ "security" "acme" "validMin" ] + [ "security" "acme" "defaults" "validMinDays" ] + (config: config.security.acme.validMin / (24 * 3600))) + (lib.replaceStrings + [ "https://registry" ".io/providers" ] + [ "registry" ".io" ] + homepage) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) + # This line is engineered to exactly hit the line length limit + (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ "hardware" "graphics" ]) + (mkRenamedOptionModule [ + "services" + "xserver" + "displayManager" + "sddm" + "enable" + ] [ "services" "displayManager" "sddm" "enable" ]) + (map (buildAllowCommand "allow" [ "snapshot" "mount" "destroy" ])) + (map (x: "${x} ${escapeShellArgs [ stateDir workDir logsDir ]}") [ + "+${unconfigureRunner}" # runs as root + configureRunner + setupWorkDir + ]) + (lib.checkListOfEnum "${pname}: theme accent" + [ + "Blue" + "Flamingo" + "Green" + ] + [ accent ] + lib.checkListOfEnum + "${pname}: color variant" + [ "Latte" "Frappe" "Macchiato" "Mocha" ] + [ variant ] + ) + (lib.switch [ coq.coq-version ssreflect.version ] [ + { + cases = [ + (lib.versions.range "8.15" "8.20") + lib.pred.true + ]; + out = "2.0.4"; + } + { + cases = [ + "8.5" + lib.pred.true + ]; + out = "20170512"; + } + ] null) +] diff --git a/test/diff/apply_with_lists/out-pure.nix b/test/diff/apply_with_lists/out-pure.nix new file mode 100644 index 0000000..1437bf1 --- /dev/null +++ b/test/diff/apply_with_lists/out-pure.nix @@ -0,0 +1,134 @@ +# This file contains an assortment of test cases involving list-heavy function calls + +[ + (f [ ] [ + rhs + lhs + ]) + (lib.mkMerge [ + false + false + ]) + (replaceStrings [ "\${" "''" ] + #force multiline + [ + "''\${" + "'''" + ] + ) + (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) + (replaceStrings [ ''"'' "\\" ] + # force multiline + [ ''\"'' "\\\\" ] + name + ) + (replaceStrings + [ + "@" + ":" + "\\" + "[" + "]" + ] + [ + "-" + "-" + "-" + "" + "" + ] + ) + (lists.removePrefix [ + 1 + 2 + ] [ ]) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ + 1 + 2 + ] [ ]) + (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ + partitionTypes.usr-verity + ] (builtins.readFile ./assert_uki_repart_match.py)) + (replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget)) + (lib.mkChangedOptionModule [ "security" "acme" "validMin" ] [ + "security" + "acme" + "defaults" + "validMinDays" + ] (config: config.security.acme.validMin / (24 * 3600))) + (lib.replaceStrings [ "https://registry" ".io/providers" ] [ + "registry" + ".io" + ] homepage) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ + "console" + "extraTTYs" + ]) + # This line is engineered to exactly hit the line length limit + (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ + "hardware" + "graphics" + ]) + (mkRenamedOptionModule + [ + "services" + "xserver" + "displayManager" + "sddm" + "enable" + ] + [ + "services" + "displayManager" + "sddm" + "enable" + ] + ) + (map ( + buildAllowCommand "allow" [ + "snapshot" + "mount" + "destroy" + ] + )) + (map + ( + x: + "${x} ${ + escapeShellArgs [ + stateDir + workDir + logsDir + ] + }" + ) + [ + "+${unconfigureRunner}" # runs as root + configureRunner + setupWorkDir + ] + ) + (lib.checkListOfEnum "${pname}: theme accent" [ "Blue" "Flamingo" "Green" ] + [ accent ] + lib.checkListOfEnum + "${pname}: color variant" + [ "Latte" "Frappe" "Macchiato" "Mocha" ] + [ variant ] + ) + (lib.switch [ coq.coq-version ssreflect.version ] [ + { + cases = [ + (lib.versions.range "8.15" "8.20") + lib.pred.true + ]; + out = "2.0.4"; + } + { + cases = [ + "8.5" + lib.pred.true + ]; + out = "20170512"; + } + ] null) +] diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix new file mode 100644 index 0000000..3f4329f --- /dev/null +++ b/test/diff/apply_with_lists/out.nix @@ -0,0 +1,139 @@ +# This file contains an assortment of test cases involving list-heavy function calls + +[ + (f [ ] [ + rhs + lhs + ]) + (lib.mkMerge [ + false + false + ]) + (replaceStrings [ "\${" "''" ] + #force multiline + [ + "''\${" + "'''" + ] + ) + (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) + (replaceStrings [ ''"'' "\\" ] + # force multiline + [ ''\"'' "\\\\" ] + name + ) + (replaceStrings + [ + "@" + ":" + "\\" + "[" + "]" + ] + [ + "-" + "-" + "-" + "" + "" + ] + ) + (lists.removePrefix [ + 1 + 2 + ] [ ]) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ + 1 + 2 + ] [ ]) + (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ + partitionTypes.usr-verity + ] (builtins.readFile ./assert_uki_repart_match.py)) + (replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget)) + (lib.mkChangedOptionModule [ "security" "acme" "validMin" ] [ + "security" + "acme" + "defaults" + "validMinDays" + ] (config: config.security.acme.validMin / (24 * 3600))) + (lib.replaceStrings [ "https://registry" ".io/providers" ] [ + "registry" + ".io" + ] homepage) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ + "console" + "extraTTYs" + ]) + # This line is engineered to exactly hit the line length limit + (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ + "hardware" + "graphics" + ]) + (mkRenamedOptionModule + [ + "services" + "xserver" + "displayManager" + "sddm" + "enable" + ] + [ + "services" + "displayManager" + "sddm" + "enable" + ] + ) + (map ( + buildAllowCommand "allow" [ + "snapshot" + "mount" + "destroy" + ] + )) + (map + ( + x: + "${x} ${ + escapeShellArgs [ + stateDir + workDir + logsDir + ] + }" + ) + [ + "+${unconfigureRunner}" # runs as root + configureRunner + setupWorkDir + ] + ) + (lib.checkListOfEnum "${pname}: theme accent" + [ + "Blue" + "Flamingo" + "Green" + ] + [ accent ] + lib.checkListOfEnum + "${pname}: color variant" + [ "Latte" "Frappe" "Macchiato" "Mocha" ] + [ variant ] + ) + (lib.switch [ coq.coq-version ssreflect.version ] [ + { + cases = [ + (lib.versions.range "8.15" "8.20") + lib.pred.true + ]; + out = "2.0.4"; + } + { + cases = [ + "8.5" + lib.pred.true + ]; + out = "20170512"; + } + ] null) +] From 7e279fafc45ccca6b70ce93d788a92f6b65d161d Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 3 Oct 2024 10:35:37 +0200 Subject: [PATCH 2/6] application: Special case list arguments 1/2 --- src/Nixfmt/Pretty.hs | 6 +++ test/diff/apply_with_lists/out-pure.nix | 66 +++++++++++++------------ test/diff/apply_with_lists/out.nix | 63 ++++++++++++----------- 3 files changed, 74 insertions(+), 61 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index f03f7bc..c74e039 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -381,6 +381,12 @@ prettyApp indentFunction pre hasPost f a = -- because if they get expanded before anything else, -- only the `.`-and-after part gets to a new line, which looks very odd absorbApp (Application f' a'@(Term Selection{})) = group' Transparent (absorbApp f') <> line <> nest (group' RegularG $ absorbInner a') + -- If two consecutive arguments are lists, treat them specially: Don't priority expand, and also + -- if one does not fit onto the line then put both on a new line each. + -- Note that this does not handle the case where the two last arguments are lists, as the last argument + -- is handled elsewhere and cannot be pattern-matched here. + absorbApp (Application (Application f' l1@(Term List{})) l2@(Term List{})) = + group' Transparent (group' Transparent (absorbApp f') <> nest (group' RegularG $ line <> group (absorbInner l1) <> line <> group (absorbInner l2))) absorbApp (Application f' a') = group' Transparent (absorbApp f') <> line <> nest (group' Priority $ absorbInner a') -- First argument absorbApp expr diff --git a/test/diff/apply_with_lists/out-pure.nix b/test/diff/apply_with_lists/out-pure.nix index 1437bf1..176b458 100644 --- a/test/diff/apply_with_lists/out-pure.nix +++ b/test/diff/apply_with_lists/out-pure.nix @@ -17,7 +17,8 @@ ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) - (replaceStrings [ ''"'' "\\" ] + (replaceStrings + [ ''"'' "\\" ] # force multiline [ ''\"'' "\\\\" ] name @@ -46,20 +47,18 @@ 1 2 ] [ ]) - (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ - partitionTypes.usr-verity - ] (builtins.readFile ./assert_uki_repart_match.py)) + (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] + (builtins.readFile ./assert_uki_repart_match.py) + ) (replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget)) - (lib.mkChangedOptionModule [ "security" "acme" "validMin" ] [ - "security" - "acme" - "defaults" - "validMinDays" - ] (config: config.security.acme.validMin / (24 * 3600))) - (lib.replaceStrings [ "https://registry" ".io/providers" ] [ - "registry" - ".io" - ] homepage) + (lib.mkChangedOptionModule + [ "security" "acme" "validMin" ] + [ "security" "acme" "defaults" "validMinDays" ] + (config: config.security.acme.validMin / (24 * 3600)) + ) + (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] + homepage + ) (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" @@ -108,27 +107,32 @@ setupWorkDir ] ) - (lib.checkListOfEnum "${pname}: theme accent" [ "Blue" "Flamingo" "Green" ] + (lib.checkListOfEnum "${pname}: theme accent" + [ "Blue" "Flamingo" "Green" ] [ accent ] lib.checkListOfEnum "${pname}: color variant" [ "Latte" "Frappe" "Macchiato" "Mocha" ] [ variant ] ) - (lib.switch [ coq.coq-version ssreflect.version ] [ - { - cases = [ - (lib.versions.range "8.15" "8.20") - lib.pred.true - ]; - out = "2.0.4"; - } - { - cases = [ - "8.5" - lib.pred.true - ]; - out = "20170512"; - } - ] null) + (lib.switch + [ coq.coq-version ssreflect.version ] + [ + { + cases = [ + (lib.versions.range "8.15" "8.20") + lib.pred.true + ]; + out = "2.0.4"; + } + { + cases = [ + "8.5" + lib.pred.true + ]; + out = "20170512"; + } + ] + null + ) ] diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix index 3f4329f..851779e 100644 --- a/test/diff/apply_with_lists/out.nix +++ b/test/diff/apply_with_lists/out.nix @@ -17,7 +17,8 @@ ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) - (replaceStrings [ ''"'' "\\" ] + (replaceStrings + [ ''"'' "\\" ] # force multiline [ ''\"'' "\\\\" ] name @@ -46,20 +47,18 @@ 1 2 ] [ ]) - (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ - partitionTypes.usr-verity - ] (builtins.readFile ./assert_uki_repart_match.py)) + (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] + (builtins.readFile ./assert_uki_repart_match.py) + ) (replaceStrings [ "-" ] [ "_" ] (toUpper final.rust.cargoShortTarget)) - (lib.mkChangedOptionModule [ "security" "acme" "validMin" ] [ - "security" - "acme" - "defaults" - "validMinDays" - ] (config: config.security.acme.validMin / (24 * 3600))) - (lib.replaceStrings [ "https://registry" ".io/providers" ] [ - "registry" - ".io" - ] homepage) + (lib.mkChangedOptionModule + [ "security" "acme" "validMin" ] + [ "security" "acme" "defaults" "validMinDays" ] + (config: config.security.acme.validMin / (24 * 3600)) + ) + (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] + homepage + ) (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" @@ -120,20 +119,24 @@ [ "Latte" "Frappe" "Macchiato" "Mocha" ] [ variant ] ) - (lib.switch [ coq.coq-version ssreflect.version ] [ - { - cases = [ - (lib.versions.range "8.15" "8.20") - lib.pred.true - ]; - out = "2.0.4"; - } - { - cases = [ - "8.5" - lib.pred.true - ]; - out = "20170512"; - } - ] null) + (lib.switch + [ coq.coq-version ssreflect.version ] + [ + { + cases = [ + (lib.versions.range "8.15" "8.20") + lib.pred.true + ]; + out = "2.0.4"; + } + { + cases = [ + "8.5" + lib.pred.true + ]; + out = "20170512"; + } + ] + null + ) ] From 48cbfec0b4f6986c71df6fc0bd31a00b544270a7 Mon Sep 17 00:00:00 2001 From: piegames Date: Mon, 14 Oct 2024 11:48:54 +0200 Subject: [PATCH 3/6] cli: add --ir --- main/Main.hs | 10 ++++++++-- src/Nixfmt.hs | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/main/Main.hs b/main/Main.hs index 583a85d..5a26f86 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -51,7 +51,8 @@ data Nixfmt = Nixfmt strict :: Bool, verify :: Bool, ast :: Bool, - filename :: Maybe FilePath + filename :: Maybe FilePath, + ir :: Bool } deriving (Show, Data, Typeable) @@ -83,7 +84,11 @@ options = Nothing &= help "The filename to display when the file input is given through stdin.\n\ - \Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is." + \Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is.", + ir = + False + &= help + "Pretty print the internal intermediate representation, only for debugging" } &= summary ("nixfmt " ++ versionFromFile) &= help "Format Nix source code" @@ -164,6 +169,7 @@ type Formatter = FilePath -> Text -> Either String Text toFormatter :: Nixfmt -> Formatter toFormatter Nixfmt{ast = True} = Nixfmt.printAst +toFormatter Nixfmt{ir = True} = Nixfmt.printIR toFormatter Nixfmt{width, verify = True, strict} = Nixfmt.formatVerify (layout width strict) toFormatter Nixfmt{width, verify = False, strict} = Nixfmt.format (layout width strict) diff --git a/src/Nixfmt.hs b/src/Nixfmt.hs index fd432a6..716de3d 100644 --- a/src/Nixfmt.hs +++ b/src/Nixfmt.hs @@ -7,6 +7,7 @@ module Nixfmt ( format, formatVerify, printAst, + printIR, ) where @@ -15,7 +16,7 @@ import Data.Either (fromRight) import Data.Text (Text, unpack) import Data.Text.Lazy (toStrict) import qualified Nixfmt.Parser as Parser -import Nixfmt.Predoc (Pretty) +import Nixfmt.Predoc (Pretty, fixup, pretty) import Nixfmt.Pretty () import Nixfmt.Types (Expression, LanguageElement, ParseErrorBundle, Whole (..), walkSubprograms) import qualified Text.Megaparsec as Megaparsec (parse) @@ -41,6 +42,12 @@ printAst path unformatted = do Whole unformattedParsed' _ <- first errorBundlePretty . Megaparsec.parse Parser.file path $ unformatted Left (unpack $ toStrict $ pShow unformattedParsed') +-- | Pretty print the internal IR for debugging +printIR :: FilePath -> Text -> Either String Text +printIR filename = + bimap errorBundlePretty (toStrict . pShow . fixup . pretty) + . Megaparsec.parse Parser.file filename + -- Same functionality as `format`, but add sanity checks to guarantee the following properties of the formatter: -- - Correctness: The formatted output parses, and the parse tree is identical to the input's -- - Idempotency: Formatting the output again will not modify it From d7c42a90f389cffa6874d84a1079226bf7d5f984 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 3 Oct 2024 11:18:57 +0200 Subject: [PATCH 4/6] Refactor: renderSimple in prettyApp --- src/Nixfmt/Pretty.hs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index c74e039..f7cfc35 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -457,15 +457,22 @@ prettyApp indentFunction pre hasPost f a = ((\a'@Ann{preTrivia} -> (a'{preTrivia = []}, preTrivia)) . moveTrailingCommentUp) f - renderedF = pre <> group' Transparent (absorbApp fWithoutComment) - renderedFUnexpanded = unexpandSpacing' Nothing renderedF + -- renderSimple will take a document to render, and call one of two callbacks depending on whether + -- it can take a simplified layout (with removed line breaks) or not. + renderSimple :: Doc -> (Doc -> Doc) -> (Doc -> Doc) -> Doc + renderSimple toRender renderIfSimple renderOtherwise = + let renderedF = pre <> group' Transparent toRender + renderedFUnexpanded = unexpandSpacing' Nothing renderedF + in if isSimple (Application f a) && isJust renderedFUnexpanded + then renderIfSimple (fromJust renderedFUnexpanded) + else renderOtherwise renderedF post = if hasPost then line' else mempty in pretty comment' - <> ( if isSimple (Application f a) && isJust renderedFUnexpanded - then group' RegularG $ fromJust renderedFUnexpanded <> hardspace <> absorbLast a - else group' RegularG $ renderedF <> line <> absorbLast a <> post - ) + <> renderSimple + (absorbApp fWithoutComment) + (\fRendered -> group' RegularG $ fRendered <> hardspace <> absorbLast a) + (\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post) <> (if hasPost && not (null comment') then hardline else mempty) prettyWith :: Bool -> Expression -> Doc From 33d549182300d67d22b284c5dd02cc6ca83bd2e2 Mon Sep 17 00:00:00 2001 From: piegames Date: Thu, 3 Oct 2024 11:22:56 +0200 Subject: [PATCH 5/6] application: Special case list arguments 2/2 --- src/Nixfmt/Pretty.hs | 21 ++++++++-- test/diff/apply/out-pure.nix | 5 +-- test/diff/apply_with_lists/out-pure.nix | 55 +++++++++++-------------- test/diff/apply_with_lists/out.nix | 55 +++++++++++-------------- test/diff/idioms_lib_3/out-pure.nix | 5 +-- test/diff/idioms_lib_3/out.nix | 5 +-- 6 files changed, 68 insertions(+), 78 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index f7cfc35..ae3e305 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -469,10 +469,23 @@ prettyApp indentFunction pre hasPost f a = post = if hasPost then line' else mempty in pretty comment' - <> renderSimple - (absorbApp fWithoutComment) - (\fRendered -> group' RegularG $ fRendered <> hardspace <> absorbLast a) - (\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post) + <> case (fWithoutComment, a) of + -- When the two last arguments are lists, render these specially (same as above) + -- Also no need to wrap in renderSimple here, because we know that these kinds of arguments + -- are never "simple" by definition. + (Application fWithoutCommandAndWithoutArg l1@(Term List{}), l2@(Term List{})) -> + group' RegularG $ + (pre <> group' Transparent (absorbApp fWithoutCommandAndWithoutArg)) + <> line + <> nest (group (absorbInner l1)) + <> line + <> nest (group (absorbInner l2)) + <> post + _ -> + renderSimple + (absorbApp fWithoutComment) + (\fRendered -> group' RegularG $ fRendered <> hardspace <> absorbLast a) + (\fRendered -> group' RegularG $ fRendered <> line <> absorbLast a <> post) <> (if hasPost && not (null comment') then hardline else mempty) prettyWith :: Bool -> Expression -> Doc diff --git a/test/diff/apply/out-pure.nix b/test/diff/apply/out-pure.nix index 9911461..9db9ebc 100644 --- a/test/diff/apply/out-pure.nix +++ b/test/diff/apply/out-pure.nix @@ -160,10 +160,7 @@ ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; test = foo [ diff --git a/test/diff/apply_with_lists/out-pure.nix b/test/diff/apply_with_lists/out-pure.nix index 176b458..edf22e6 100644 --- a/test/diff/apply_with_lists/out-pure.nix +++ b/test/diff/apply_with_lists/out-pure.nix @@ -1,20 +1,15 @@ # This file contains an assortment of test cases involving list-heavy function calls [ - (f [ ] [ - rhs - lhs - ]) + (f [ ] [ rhs lhs ]) (lib.mkMerge [ false false ]) - (replaceStrings [ "\${" "''" ] + (replaceStrings + [ "\${" "''" ] #force multiline - [ - "''\${" - "'''" - ] + [ "''\${" "'''" ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) (replaceStrings @@ -39,14 +34,20 @@ "" ] ) - (lists.removePrefix [ - 1 - 2 - ] [ ]) - (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ - 1 - 2 - ] [ ]) + (lists.removePrefix + [ + 1 + 2 + ] + [ ] + ) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [ + 1 + 2 + ] + [ ] + ) (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] (builtins.readFile ./assert_uki_repart_match.py) ) @@ -59,15 +60,12 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) # This line is engineered to exactly hit the line length limit - (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ - "hardware" - "graphics" - ]) + (lib.mkRenamedOptionModule + [ "hardware" "package234" ] + [ "hardware" "graphics" ] + ) (mkRenamedOptionModule [ "services" @@ -76,12 +74,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix index 851779e..580e2b6 100644 --- a/test/diff/apply_with_lists/out.nix +++ b/test/diff/apply_with_lists/out.nix @@ -1,20 +1,15 @@ # This file contains an assortment of test cases involving list-heavy function calls [ - (f [ ] [ - rhs - lhs - ]) + (f [ ] [ rhs lhs ]) (lib.mkMerge [ false false ]) - (replaceStrings [ "\${" "''" ] + (replaceStrings + [ "\${" "''" ] #force multiline - [ - "''\${" - "'''" - ] + [ "''\${" "'''" ] ) (replaceStrings [ ''"'' "\\" ] [ ''\"'' "\\\\" ] name) (replaceStrings @@ -39,14 +34,20 @@ "" ] ) - (lists.removePrefix [ - 1 - 2 - ] [ ]) - (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa [ - 1 - 2 - ] [ ]) + (lists.removePrefix + [ + 1 + 2 + ] + [ ] + ) + (lists.removePrefix aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + [ + 1 + 2 + ] + [ ] + ) (builtins.replaceStrings [ "@NIX_STORE_VERITY@" ] [ partitionTypes.usr-verity ] (builtins.readFile ./assert_uki_repart_match.py) ) @@ -59,15 +60,12 @@ (lib.replaceStrings [ "https://registry" ".io/providers" ] [ "registry" ".io" ] homepage ) - (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ - "console" - "extraTTYs" - ]) + (lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ]) # This line is engineered to exactly hit the line length limit - (lib.mkRenamedOptionModule [ "hardware" "package234" ] [ - "hardware" - "graphics" - ]) + (lib.mkRenamedOptionModule + [ "hardware" "package234" ] + [ "hardware" "graphics" ] + ) (mkRenamedOptionModule [ "services" @@ -76,12 +74,7 @@ "sddm" "enable" ] - [ - "services" - "displayManager" - "sddm" - "enable" - ] + [ "services" "displayManager" "sddm" "enable" ] ) (map ( buildAllowCommand "allow" [ diff --git a/test/diff/idioms_lib_3/out-pure.nix b/test/diff/idioms_lib_3/out-pure.nix index 387e9df..2c9c796 100644 --- a/test/diff/idioms_lib_3/out-pure.nix +++ b/test/diff/idioms_lib_3/out-pure.nix @@ -366,10 +366,7 @@ rec { ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; singlelineResult = ''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"''; multilineResult = diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index 469184e..53474a6 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -379,10 +379,7 @@ rec { ''"'' "\${" ]; - escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ - "''\${" - "'''" - ]; + escapeMultiline = libStr.replaceStrings [ "\${" "''" ] [ "''\${" "'''" ]; singlelineResult = ''"'' + concatStringsSep "\\n" (map escapeSingleline lines) + ''"''; multilineResult = From 07c484fb5686c922e6e92b0e09788a92fa13952c Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Tue, 26 Nov 2024 18:16:40 +0100 Subject: [PATCH 6/6] Increase list length limit to 6 in functions This fixes the poor formatting of the last remaining cases of mkRenamedOptionModule. We're not removing the line length limit completely because this code path doesn't enforce the line length limit. Co-Authored-By: piegames --- src/Nixfmt/Pretty.hs | 2 +- test/diff/apply_with_lists/out-pure.nix | 25 ++----------------------- test/diff/apply_with_lists/out.nix | 17 +---------------- 3 files changed, 4 insertions(+), 40 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index ae3e305..9c80c10 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -398,7 +398,7 @@ prettyApp indentFunction pre hasPost f a = -- If lists have only simple items, try to render them single-line instead of expanding -- This is just a copy of the list rendering code, but with `sepBy line` instead of `sepBy hardline` absorbInner (Term (List paropen@Ann{trailComment = post'} items parclose)) - | length (unItems items) <= 4 && all (isSimple . Term) items = + | length (unItems items) <= 6 && all (isSimple . Term) items = pretty (paropen{trailComment = Nothing}) <> surroundWith sur (nest $ pretty post' <> sepBy line (unItems items)) <> pretty parclose diff --git a/test/diff/apply_with_lists/out-pure.nix b/test/diff/apply_with_lists/out-pure.nix index edf22e6..6c33da7 100644 --- a/test/diff/apply_with_lists/out-pure.nix +++ b/test/diff/apply_with_lists/out-pure.nix @@ -18,22 +18,7 @@ [ ''\"'' "\\\\" ] name ) - (replaceStrings - [ - "@" - ":" - "\\" - "[" - "]" - ] - [ - "-" - "-" - "-" - "" - "" - ] - ) + (replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ]) (lists.removePrefix [ 1 @@ -67,13 +52,7 @@ [ "hardware" "graphics" ] ) (mkRenamedOptionModule - [ - "services" - "xserver" - "displayManager" - "sddm" - "enable" - ] + [ "services" "xserver" "displayManager" "sddm" "enable" ] [ "services" "displayManager" "sddm" "enable" ] ) (map ( diff --git a/test/diff/apply_with_lists/out.nix b/test/diff/apply_with_lists/out.nix index 580e2b6..bee7df4 100644 --- a/test/diff/apply_with_lists/out.nix +++ b/test/diff/apply_with_lists/out.nix @@ -18,22 +18,7 @@ [ ''\"'' "\\\\" ] name ) - (replaceStrings - [ - "@" - ":" - "\\" - "[" - "]" - ] - [ - "-" - "-" - "-" - "" - "" - ] - ) + (replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ]) (lists.removePrefix [ 1