From 4afecdb5df28dda69913f37f78ef8a43a92581a4 Mon Sep 17 00:00:00 2001 From: Kevin Amado Date: Thu, 10 Apr 2025 15:10:28 -0600 Subject: [PATCH] feat: support bin-op again --- src/alejandra/src/builder.rs | 33 +++++++++++- src/alejandra/src/rules/bin_op.rs | 19 ++----- src/alejandra/src/rules/mod.rs | 2 +- .../tests/cases/default/bin_op/out.nix | 51 ++++++++++++++++--- .../tests/cases/default/idioms_lib_2/out.nix | 11 ++-- .../cases/default/idioms_nixos_1/out.nix | 19 ++++--- .../tests/cases/default/idioms_pkgs_3/out.nix | 19 ++++--- .../tests/cases/default/monsters_5/out.nix | 10 +--- .../tests/cases/default/pipe_operators/in.nix | 2 + .../cases/default/pipe_operators/out.nix | 3 ++ 10 files changed, 115 insertions(+), 54 deletions(-) diff --git a/src/alejandra/src/builder.rs b/src/alejandra/src/builder.rs index 7916516..a264bbf 100644 --- a/src/alejandra/src/builder.rs +++ b/src/alejandra/src/builder.rs @@ -144,63 +144,92 @@ fn format( let rule = match kind { // a b rnix::SyntaxKind::NODE_APPLY => crate::rules::apply::rule, + // assert a; b - rnix::SyntaxKind::NODE_ASSERT => crate::rules::scoped::rule, /* a.b.c */ + rnix::SyntaxKind::NODE_ASSERT => crate::rules::scoped::rule, + + // a.b.c rnix::SyntaxKind::NODE_ATTRPATH => crate::rules::default, + // a = b; rnix::SyntaxKind::NODE_ATTRPATH_VALUE => { crate::rules::key_value::rule } + // { } rnix::SyntaxKind::NODE_ATTR_SET => crate::rules::attr_set::rule, + // a $op b - rnix::SyntaxKind::NODE_BIN_OP => crate::rules::default, + rnix::SyntaxKind::NODE_BIN_OP => crate::rules::bin_op::rule, + // ${a} (interpolation but for NODE_SELECT) rnix::SyntaxKind::NODE_DYNAMIC => crate::rules::dynamic::rule, + // rnix::SyntaxKind::NODE_HAS_ATTR => crate::rules::default, + // $identifier rnix::SyntaxKind::NODE_IDENT => crate::rules::default, rnix::SyntaxKind::NODE_IDENT_PARAM => crate::rules::default, + // if a then b else c rnix::SyntaxKind::NODE_IF_ELSE => crate::rules::if_else::rule, + // inherit NODE_INHERIT_FROM? b+ ; rnix::SyntaxKind::NODE_INHERIT => crate::rules::inherit::rule, + // ( a ) rnix::SyntaxKind::NODE_INHERIT_FROM => { crate::rules::paren::rule } + // ${a} rnix::SyntaxKind::NODE_INTERPOL => crate::rules::paren::rule, + // a: b rnix::SyntaxKind::NODE_LAMBDA => crate::rules::lambda::rule, + // let { } rnix::SyntaxKind::NODE_LEGACY_LET => crate::rules::default, + // let NODE_KEY_VALUE* in b; rnix::SyntaxKind::NODE_LET_IN => crate::rules::let_in::rule, + // [ ... ] rnix::SyntaxKind::NODE_LIST => crate::rules::list::rule, + // 1 | true | null rnix::SyntaxKind::NODE_LITERAL => crate::rules::default, + // ( a ) rnix::SyntaxKind::NODE_PAREN => crate::rules::paren::rule, + // a | a ? b rnix::SyntaxKind::NODE_PAT_BIND => crate::rules::pat_bind::rule, + // NODE_PAT_BIND | TOKEN_ELLIPSIS rnix::SyntaxKind::NODE_PAT_ENTRY => { crate::rules::pat_entry::rule } + + // /path/to/${a} rnix::SyntaxKind::NODE_PATH => crate::rules::default, + // { NODE_PAT_ENTRY* } rnix::SyntaxKind::NODE_PATTERN => crate::rules::pattern::rule, + // implementation detail of rowan rnix::SyntaxKind::NODE_ROOT => crate::rules::root::rule, + // a.b | a.NODE_DYNAMIC rnix::SyntaxKind::NODE_SELECT => crate::rules::default, + // "..." || ''...'' rnix::SyntaxKind::NODE_STRING => crate::rules::string::rule, + // !a rnix::SyntaxKind::NODE_UNARY_OP => crate::rules::default, + // with a; b rnix::SyntaxKind::NODE_WITH => crate::rules::scoped::rule, kind => { diff --git a/src/alejandra/src/rules/bin_op.rs b/src/alejandra/src/rules/bin_op.rs index 32ee0cd..ff7d562 100644 --- a/src/alejandra/src/rules/bin_op.rs +++ b/src/alejandra/src/rules/bin_op.rs @@ -2,14 +2,7 @@ pub(crate) fn rule( build_ctx: &crate::builder::BuildCtx, node: &rnix::SyntaxNode, ) -> std::collections::LinkedList { - rule_with_configuration(build_ctx, node, "bin_op_and_or_default") -} -pub(crate) fn rule_with_configuration( - build_ctx: &crate::builder::BuildCtx, - node: &rnix::SyntaxNode, - parent_kind: &str, -) -> std::collections::LinkedList { let mut steps = std::collections::LinkedList::new(); let mut children = crate::children2::new(build_ctx, node); @@ -30,11 +23,7 @@ pub(crate) fn rule_with_configuration( if vertical { let kind = first.element.kind(); - if (parent_kind == "bin_op_and_or_default" - && matches!(kind, rnix::SyntaxKind::NODE_BIN_OP)) - || (parent_kind == "select" - && matches!(kind, rnix::SyntaxKind::NODE_SELECT)) - { + if matches!(kind, rnix::SyntaxKind::NODE_BIN_OP) { steps.push_back(crate::builder::Step::Format(first.element)); } else { steps.push_back(crate::builder::Step::FormatWider(first.element)); @@ -65,7 +54,7 @@ pub(crate) fn rule_with_configuration( } // second - if !vertical && parent_kind == "bin_op_and_or_default" { + if !vertical { steps.push_back(crate::builder::Step::Whitespace); } steps.push_back(crate::builder::Step::Format(second.element)); @@ -90,9 +79,7 @@ pub(crate) fn rule_with_configuration( crate::children2::Trivia::Newlines => {} } } - } else if !second.has_inline_comment - && parent_kind == "bin_op_and_or_default" - { + } else if !second.has_inline_comment { steps.push_back(crate::builder::Step::Whitespace); } diff --git a/src/alejandra/src/rules/mod.rs b/src/alejandra/src/rules/mod.rs index 1fcc24f..e5b77be 100644 --- a/src/alejandra/src/rules/mod.rs +++ b/src/alejandra/src/rules/mod.rs @@ -1,6 +1,6 @@ pub(crate) mod apply; pub(crate) mod attr_set; -// pub(crate) mod bin_op; +pub(crate) mod bin_op; pub(crate) mod dynamic; pub(crate) mod if_else; pub(crate) mod inherit; diff --git a/src/alejandra/tests/cases/default/bin_op/out.nix b/src/alejandra/tests/cases/default/bin_op/out.nix index f3d2d82..2311b7e 100644 --- a/src/alejandra/tests/cases/default/bin_op/out.nix +++ b/src/alejandra/tests/cases/default/bin_op/out.nix @@ -1,10 +1,49 @@ [ - (1 + 1) - (1 +/**/1) - (1/**/+ 1) - (1/**/+/**/1) - (1/**/+/**/(1/**/+/**/(1/**/+/**/1))) + (1 + 1) + (1 + + + /**/ + 1) + (1 + /**/ + + 1) + (1 + /**/ + + + /**/ + 1) + (1 + /**/ + + + /**/ + (1 + /**/ + + + /**/ + (1 + /**/ + + + /**/ + 1))) (1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) (1 - + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1 + + 1) ] diff --git a/src/alejandra/tests/cases/default/idioms_lib_2/out.nix b/src/alejandra/tests/cases/default/idioms_lib_2/out.nix index ade2318..8721401 100644 --- a/src/alejandra/tests/cases/default/idioms_lib_2/out.nix +++ b/src/alejandra/tests/cases/default/idioms_lib_2/out.nix @@ -98,7 +98,7 @@ builtins.bitAnd or (import ./zip-int-bits.nix (a: b: - if a==1 && b==1 + if a == 1 && b == 1 then 1 else 0)); @@ -109,7 +109,7 @@ builtins.bitOr or (import ./zip-int-bits.nix (a: b: - if a==1 || b==1 + if a == 1 || b == 1 then 1 else 0)); @@ -120,7 +120,7 @@ builtins.bitXor or (import ./zip-int-bits.nix (a: b: - if a!=b + if a != b then 1 else 0)); @@ -482,8 +482,9 @@ Check whether something is a function or something annotated with function args. */ - isFunction = f: builtins.isFunction f || - (f ? __functor && isFunction (f.__functor f)); + isFunction = f: + builtins.isFunction f + || (f ? __functor && isFunction (f.__functor f)); /* Convert the given positive integer to a string of its hexadecimal diff --git a/src/alejandra/tests/cases/default/idioms_nixos_1/out.nix b/src/alejandra/tests/cases/default/idioms_nixos_1/out.nix index d3cf293..ae087c2 100644 --- a/src/alejandra/tests/cases/default/idioms_nixos_1/out.nix +++ b/src/alejandra/tests/cases/default/idioms_nixos_1/out.nix @@ -81,10 +81,11 @@ in { }; boot.kernelParams = mkOption { - type = types.listOf (types.strMatching ''([^"[:space:]]|"[^"]*")+'' // { - name = "kernelParam"; - description = "string, with spaces inside double quotes"; - }); + type = types.listOf (types.strMatching ''([^"[:space:]]|"[^"]*")+'' + // { + name = "kernelParam"; + description = "string, with spaces inside double quotes"; + }); default = []; description = "Parameters added to the kernel command line."; }; @@ -244,7 +245,8 @@ in { "hid_logitech_hidpp" "hid_logitech_dj" "hid_microsoft" - ] ++ optionals pkgs.stdenv.hostPlatform.isx86 [ + ] + ++ optionals pkgs.stdenv.hostPlatform.isx86 [ # Misc. x86 keyboard stuff. "pcips2" "atkbd" @@ -268,8 +270,8 @@ in { # Implement consoleLogLevel both in early boot and using sysctl # (so you don't need to reboot to have changes take effect). boot.kernelParams = - ["loglevel=${toString config.boot.consoleLogLevel}"] ++ - optionals config.boot.vesa ["vga=0x317" "nomodeset"]; + ["loglevel=${toString config.boot.consoleLogLevel}"] + ++ optionals config.boot.vesa ["vga=0x317" "nomodeset"]; boot.kernel.sysctl."kernel.printk" = mkDefault config.boot.consoleLogLevel; @@ -337,7 +339,8 @@ in { # !!! Should this really be needed? (isYes "MODULES") (isYes "BINFMT_ELF") - ] ++ (optional (randstructSeed != "") (isYes "GCC_PLUGIN_RANDSTRUCT")); + ] + ++ (optional (randstructSeed != "") (isYes "GCC_PLUGIN_RANDSTRUCT")); # nixpkgs kernels are assumed to have all required features assertions = diff --git a/src/alejandra/tests/cases/default/idioms_pkgs_3/out.nix b/src/alejandra/tests/cases/default/idioms_pkgs_3/out.nix index d3cf293..ae087c2 100644 --- a/src/alejandra/tests/cases/default/idioms_pkgs_3/out.nix +++ b/src/alejandra/tests/cases/default/idioms_pkgs_3/out.nix @@ -81,10 +81,11 @@ in { }; boot.kernelParams = mkOption { - type = types.listOf (types.strMatching ''([^"[:space:]]|"[^"]*")+'' // { - name = "kernelParam"; - description = "string, with spaces inside double quotes"; - }); + type = types.listOf (types.strMatching ''([^"[:space:]]|"[^"]*")+'' + // { + name = "kernelParam"; + description = "string, with spaces inside double quotes"; + }); default = []; description = "Parameters added to the kernel command line."; }; @@ -244,7 +245,8 @@ in { "hid_logitech_hidpp" "hid_logitech_dj" "hid_microsoft" - ] ++ optionals pkgs.stdenv.hostPlatform.isx86 [ + ] + ++ optionals pkgs.stdenv.hostPlatform.isx86 [ # Misc. x86 keyboard stuff. "pcips2" "atkbd" @@ -268,8 +270,8 @@ in { # Implement consoleLogLevel both in early boot and using sysctl # (so you don't need to reboot to have changes take effect). boot.kernelParams = - ["loglevel=${toString config.boot.consoleLogLevel}"] ++ - optionals config.boot.vesa ["vga=0x317" "nomodeset"]; + ["loglevel=${toString config.boot.consoleLogLevel}"] + ++ optionals config.boot.vesa ["vga=0x317" "nomodeset"]; boot.kernel.sysctl."kernel.printk" = mkDefault config.boot.consoleLogLevel; @@ -337,7 +339,8 @@ in { # !!! Should this really be needed? (isYes "MODULES") (isYes "BINFMT_ELF") - ] ++ (optional (randstructSeed != "") (isYes "GCC_PLUGIN_RANDSTRUCT")); + ] + ++ (optional (randstructSeed != "") (isYes "GCC_PLUGIN_RANDSTRUCT")); # nixpkgs kernels are assumed to have all required features assertions = diff --git a/src/alejandra/tests/cases/default/monsters_5/out.nix b/src/alejandra/tests/cases/default/monsters_5/out.nix index 5a6afb7..18e2ef7 100644 --- a/src/alejandra/tests/cases/default/monsters_5/out.nix +++ b/src/alejandra/tests/cases/default/monsters_5/out.nix @@ -60,10 +60,7 @@ in { type = types.unspecified - -// - -{ + // { merge = mergeEqualOption; }; @@ -84,10 +81,7 @@ in { or []) - -++ - -kernelPatches; + ++ kernelPatches; features = lib.recursiveUpdate diff --git a/src/alejandra/tests/cases/default/pipe_operators/in.nix b/src/alejandra/tests/cases/default/pipe_operators/in.nix index fa45da8..d91ddf1 100644 --- a/src/alejandra/tests/cases/default/pipe_operators/in.nix +++ b/src/alejandra/tests/cases/default/pipe_operators/in.nix @@ -1,4 +1,6 @@ [ (1 |> builtins.add 2 |> builtins.mul 3) (builtins.add 1 <| builtins.mul 2 <| 3) + (1 |> builtins.add 2 |> + builtins.mul 3) ] diff --git a/src/alejandra/tests/cases/default/pipe_operators/out.nix b/src/alejandra/tests/cases/default/pipe_operators/out.nix index fa45da8..2b45b09 100644 --- a/src/alejandra/tests/cases/default/pipe_operators/out.nix +++ b/src/alejandra/tests/cases/default/pipe_operators/out.nix @@ -1,4 +1,7 @@ [ (1 |> builtins.add 2 |> builtins.mul 3) (builtins.add 1 <| builtins.mul 2 <| 3) + (1 + |> builtins.add 2 + |> builtins.mul 3) ]