Skip to content

Commit

Permalink
Attrs: Always expand
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Jul 17, 2024
1 parent f09ce41 commit 796445b
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/Nixfmt/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ prettyTerm (List (Ann pre paropen post) items parclose) =
pretty (Ann pre paropen Nothing)
<> surroundWith line (nest $ pretty post <> prettyItems items)
<> pretty parclose
prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose)
prettyTerm (Set krec paropen items parclose) = prettySet True (krec, paropen, items, parclose)
-- Parentheses
prettyTerm (Parenthesized paropen expr (Ann closePre parclose closePost)) =
group $
Expand Down
4 changes: 3 additions & 1 deletion test/correct/float-below-one.nix
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{ foo = 0.3; }
{
foo = 0.3;
}
9 changes: 8 additions & 1 deletion test/correct/quotes-in-inherit.nix
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
{ inherit ({ "in" = 1; }) "in"; }
{
inherit
({
"in" = 1;
})
"in"
;
}
92 changes: 68 additions & 24 deletions test/diff/apply/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,90 @@
}
''
{
name1 = function arg { asdf = 1; };
name1 = function arg {
asdf = 1;
};

name2 = function arg { asdf = 1; } argument;
name2 = function arg {
asdf = 1;
} argument;

name3 = function arg { asdf = 1; } { qwer = 12345; } argument;
name3 =
function arg
{
asdf = 1;
}
{
qwer = 12345;
}
argument;
}
{
name1 = function arg { asdf = 1; };
name1 = function arg {
asdf = 1;
};

name2 = function arg {
asdf = 1;
# multiline
} argument;

name3 = function arg {
asdf = 1;
# multiline
} { qwer = 12345; } argument;
name3 =
function arg
{
asdf = 1;
# multiline
}
{
qwer = 12345;
}
argument;
}
{
name4 = function arg { asdf = 1; } {
qwer = 12345;
qwer2 = 54321;
} argument;
name4 =
function arg
{
asdf = 1;
}
{
qwer = 12345;
qwer2 = 54321;
}
argument;
}
{
option1 = function arg { asdf = 1; } {
qwer = 12345;
qwer2 = 54321;
} lastArg;
option1 =
function arg
{
asdf = 1;
}
{
qwer = 12345;
qwer2 = 54321;
}
lastArg;

option2 = function arg { asdf = 1; } {
qwer = 12345;
qwer2 = 54321;
} lastArg;
option2 =
function arg
{
asdf = 1;
}
{
qwer = 12345;
qwer2 = 54321;
}
lastArg;

option3 = function arg { asdf = 1; } {
qwer = 12345;
qwer2 = 54321;
} lastArg;
option3 =
function arg
{
asdf = 1;
}
{
qwer = 12345;
qwer2 = 54321;
}
lastArg;
}
# https://github.com/kamadorueda/alejandra/issues/372#issuecomment-1435083516
{
Expand Down
16 changes: 12 additions & 4 deletions test/diff/attr_set/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
{
# a
}
{ a = 1; }
{ a = 1; }
{
a = 1;
}
{
a = 1;
}

{

Expand All @@ -16,7 +20,9 @@
};
}

{ b = 1; }
{
b = 1;
}
{
b = 1; # c
}
Expand All @@ -29,7 +35,9 @@
b = 1; # c
}

rec { c = 1; }
rec {
c = 1;
}
rec {
c = 1; # d
}
Expand Down
25 changes: 19 additions & 6 deletions test/diff/idioms_lib_3/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ rec {
''
[${mkSectionName sectName}]
''
+ toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues;
+ toKeyValue {
inherit mkKeyValue listsAsDuplicateKeys;
} sectValues;
in
# map input to ini sections
mapAttrsToStringsSep "\n" mkSection attrsOfAttrs;
Expand Down Expand Up @@ -208,9 +210,14 @@ rec {
if globalSection == { } then
""
else
(toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection) + "\n"
(toKeyValue {
inherit mkKeyValue listsAsDuplicateKeys;
} globalSection)
+ "\n"
)
+ (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections);
+ (toINI {
inherit mkSectionName mkKeyValue listsAsDuplicateKeys;
} sections);

# Generate a git-config file from an attrset.
#
Expand Down Expand Up @@ -263,13 +270,19 @@ rec {
if isAttrs value && !lib.isDerivation value then
lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
else if length path > 1 then
{ ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; }
{
${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value;
}
else
{ ${head path} = value; };
{
${head path} = value;
};
in
attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs));

toINI_ = toINI { inherit mkKeyValue mkSectionName; };
toINI_ = toINI {
inherit mkKeyValue mkSectionName;
};
in
toINI_ (gitFlattenAttrs attrs);

Expand Down
11 changes: 9 additions & 2 deletions test/diff/idioms_lib_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ with lib.lists;
with lib.types;
with lib.attrsets;
with lib.strings;
with (import ./inspect.nix { inherit lib; }).predicates;
with (import ./inspect.nix {
inherit lib;
}).predicates;

let
inherit (lib.options) mergeOneOption;
Expand All @@ -29,7 +31,12 @@ let
mapAttrs (
name: value:
assert type.check value;
setType type.name ({ inherit name; } // value)
setType type.name (
{
inherit name;
}
// value
)
);

in
Expand Down
30 changes: 22 additions & 8 deletions test/diff/idioms_lib_5/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ let
null
else
"key 'meta.${k}' has invalid value; expected ${metaTypes.${k}.description}, got\n ${
lib.generators.toPretty { indent = " "; } v
lib.generators.toPretty {
indent = " ";
} v
}"
else
"key 'meta.${k}' is unrecognized; expected one of: \n [${
Expand Down Expand Up @@ -509,7 +511,9 @@ let
}
# -----
else
{ valid = "yes"; }
{
valid = "yes";
}
);

# The meta attribute is passed in the resulting attribute set,
Expand Down Expand Up @@ -595,12 +599,22 @@ let
# or, alternatively, just output a warning message.
handled =
{
no = handleEvalIssue { inherit meta attrs; } {
inherit (validity) reason errormsg;
};
warn = handleEvalWarning { inherit meta attrs; } {
inherit (validity) reason errormsg;
};
no =
handleEvalIssue
{
inherit meta attrs;
}
{
inherit (validity) reason errormsg;
};
warn =
handleEvalWarning
{
inherit meta attrs;
}
{
inherit (validity) reason errormsg;
};
yes = true;
}
.${validity.valid};
Expand Down
9 changes: 7 additions & 2 deletions test/diff/idioms_nixos_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ let
post_max_size = cfg.maxUploadSize;
memory_limit = cfg.maxUploadSize;
}
// cfg.phpOptions // optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; };
// cfg.phpOptions
// optionalAttrs cfg.caching.apcu {
"apc.enable_cli" = "1";
};

occ = pkgs.writeScriptBin "nextcloud-occ" ''
#! ${pkgs.runtimeShell}
Expand Down Expand Up @@ -1051,7 +1054,9 @@ in
ln -sfT \
${
pkgs.linkFarm "nix-apps" (
mapAttrsToList (name: path: { inherit name path; }) cfg.extraApps
mapAttrsToList (name: path: {
inherit name path;
}) cfg.extraApps
)
} \
${cfg.home}/nix-apps
Expand Down
4 changes: 3 additions & 1 deletion test/diff/idioms_pkgs_1/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
stdenv.mkDerivation rec {
pname = "test";
version = "0.0";
src = fetchFrom { url = "example/${version}"; };
src = fetchFrom {
url = "example/${version}";
};
meta = with lib; {
maintainers = with maintainers; [ someone ];
description = "something";
Expand Down
8 changes: 6 additions & 2 deletions test/diff/idioms_pkgs_2/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ stdenv.mkDerivation rec {
doCheck = true;

passthru.tests = {
version = testVersion { package = hello; };
version = testVersion {
package = hello;
};

invariant-under-noXlibs =
testEqualDerivation "hello must not be rebuilt when environment.noXlibs is set."
hello
(nixos { environment.noXlibs = true; }).pkgs.hello;
(nixos {
environment.noXlibs = true;
}).pkgs.hello;
};

meta = with lib; {
Expand Down
14 changes: 10 additions & 4 deletions test/diff/idioms_pkgs_4/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,13 @@ in
# First build a stdenv based only on tools outside the store.
(prevStage: {
inherit config overlays;
stdenv = makeStdenv { inherit (prevStage) cc fetchurl; } // {
inherit (prevStage) fetchurl;
};
stdenv =
makeStdenv {
inherit (prevStage) cc fetchurl;
}
// {
inherit (prevStage) fetchurl;
};
})

# Using that, build a stdenv that adds the ‘xz’ command (which most systems
Expand All @@ -209,7 +213,9 @@ in
stdenv = makeStdenv {
inherit (prevStage.stdenv) cc fetchurl;
extraPath = [ prevStage.xz ];
overrides = self: super: { inherit (prevStage) xz; };
overrides = self: super: {
inherit (prevStage) xz;
};
extraNativeBuildInputs =
if localSystem.isLinux then [ prevStage.patchelf ] else [ ];
};
Expand Down
Loading

0 comments on commit 796445b

Please sign in to comment.