Skip to content

Commit

Permalink
102: Function application: Expand non-last arguments when rest fits o…
Browse files Browse the repository at this point in the history
…nto a single line

Format using commit number 102 from nixfmt PR 2: piegamesde/nixfmt@7b1fcf5
  • Loading branch information
GitHub Actions committed Feb 6, 2024
1 parent 142cc3e commit 22c2ec5
Show file tree
Hide file tree
Showing 1,065 changed files with 743,080 additions and 802,611 deletions.
29 changes: 13 additions & 16 deletions doc/doc-support/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,19 @@ let
opt:
opt
// {
declarations =
map
(
decl:
if hasPrefix (toString ../..) (toString decl) then
let
subpath = removePrefix "/" (removePrefix (toString ../..) (toString decl));
in
{
url = "https://github.com/NixOS/nixpkgs/blob/master/${subpath}";
name = subpath;
}
else
decl
)
opt.declarations;
declarations = map (
decl:
if hasPrefix (toString ../..) (toString decl) then
let
subpath = removePrefix "/" (removePrefix (toString ../..) (toString decl));
in
{
url = "https://github.com/NixOS/nixpkgs/blob/master/${subpath}";
name = subpath;
}
else
decl
) opt.declarations;
};
};
in
Expand Down
14 changes: 6 additions & 8 deletions doc/doc-support/lib-function-docs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ stdenv.mkDerivation {
<root xmlns:xi="http://www.w3.org/2001/XInclude">
EOF
${lib.concatMapStrings
(
{ name, description }:
''
docgen ${name} ${lib.escapeShellArg description}
''
)
libsets}
${lib.concatMapStrings (
{ name, description }:
''
docgen ${name} ${lib.escapeShellArg description}
''
) libsets}
echo "</root>" >> "$out/index.xml"
Expand Down
72 changes: 32 additions & 40 deletions doc/doc-support/lib-function-locations.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,34 @@ let

libDefPos =
prefix: set:
builtins.concatMap
(
name:
[
{
name = builtins.concatStringsSep "." (prefix ++ [ name ]);
location = builtins.unsafeGetAttrPos name set;
}
]
++ nixpkgsLib.optionals (builtins.length prefix == 0 && builtins.isAttrs set.${name}) (
libDefPos (prefix ++ [ name ]) set.${name}
)
builtins.concatMap (
name:
[
{
name = builtins.concatStringsSep "." (prefix ++ [ name ]);
location = builtins.unsafeGetAttrPos name set;
}
]
++ nixpkgsLib.optionals (builtins.length prefix == 0 && builtins.isAttrs set.${name}) (
libDefPos (prefix ++ [ name ]) set.${name}
)
(builtins.attrNames set);
) (builtins.attrNames set);

libset =
toplib:
builtins.map
(subsetname: {
subsetname = subsetname;
functions = libDefPos [ ] toplib.${subsetname};
})
(builtins.map (x: x.name) libsets);
builtins.map (subsetname: {
subsetname = subsetname;
functions = libDefPos [ ] toplib.${subsetname};
}) (builtins.map (x: x.name) libsets);

nixpkgsLib = pkgs.lib;

flattenedLibSubset =
{ subsetname, functions }:
builtins.map
(fn: {
name = "lib.${subsetname}.${fn.name}";
value = fn.location;
})
functions;
builtins.map (fn: {
name = "lib.${subsetname}.${fn.name}";
value = fn.location;
}) functions;

locatedlibsets = libs: builtins.map flattenedLibSubset (libset libs);
removeFilenamePrefix =
Expand Down Expand Up @@ -73,21 +67,19 @@ let

urlPrefix = "https://github.com/NixOS/nixpkgs/blob/${revision}";
xmlstrings = (
nixpkgsLib.strings.concatMapStrings
(
{ name, value }:
''
<section><title>${name}</title>
<para xml:id="${sanitizeId name}">
Located at
<link
xlink:href="${urlPrefix}/${value.file}#L${builtins.toString value.line}">${value.file}:${builtins.toString value.line}</link>
in <literal>&lt;nixpkgs&gt;</literal>.
</para>
</section>
''
)
relativeLocs
nixpkgsLib.strings.concatMapStrings (
{ name, value }:
''
<section><title>${name}</title>
<para xml:id="${sanitizeId name}">
Located at
<link
xlink:href="${urlPrefix}/${value.file}#L${builtins.toString value.line}">${value.file}:${builtins.toString value.line}</link>
in <literal>&lt;nixpkgs&gt;</literal>.
</para>
</section>
''
) relativeLocs
);
in
pkgs.writeText "locations.xml" ''
Expand Down
96 changes: 41 additions & 55 deletions lib/attrsets.nix
Original file line number Diff line number Diff line change
Expand Up @@ -348,15 +348,13 @@ rec {
# The attribute set to filter
set:
listToAttrs (
concatMap
(
name:
let
v = set.${name};
in
if pred name v then [ (nameValuePair name v) ] else [ ]
)
(attrNames set)
concatMap (
name:
let
v = set.${name};
in
if pred name v then [ (nameValuePair name v) ] else [ ]
) (attrNames set)
);

/* Filter an attribute set recursively by removing all attributes for
Expand All @@ -375,18 +373,16 @@ rec {
# The attribute set to filter
set:
listToAttrs (
concatMap
(
name:
let
v = set.${name};
in
if pred name v then
[ (nameValuePair name (if isAttrs v then filterAttrsRecursive pred v else v)) ]
else
[ ]
)
(attrNames set)
concatMap (
name:
let
v = set.${name};
in
if pred name v then
[ (nameValuePair name (if isAttrs v then filterAttrsRecursive pred v else v)) ]
else
[ ]
) (attrNames set)
);

/* Like builtins.foldl' but for attribute sets.
Expand Down Expand Up @@ -462,9 +458,9 @@ rec {
nul:
# A list of attribute sets to fold together by key.
list_of_attrs:
foldr (n: a: foldr (name: o: o // { ${name} = op n.${name} (a.${name} or nul); }) a (attrNames n))
{ }
list_of_attrs;
foldr (
n: a: foldr (name: o: o // { ${name} = op n.${name} (a.${name} or nul); }) a (attrNames n)
) { } list_of_attrs;

/* Recursively collect sets that verify a given predicate named `pred`
from the set `attrs`. The recursion is stopped when the predicate is
Expand Down Expand Up @@ -509,14 +505,12 @@ rec {
cartesianProductOfSets =
# Attribute set with attributes that are lists of values
attrsOfLists:
foldl'
(
listOfAttrs: attrName:
concatMap (attrs: map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName})
listOfAttrs
)
[ { } ]
(attrNames attrsOfLists);
foldl' (
listOfAttrs: attrName:
concatMap (
attrs: map (listValue: attrs // { ${attrName} = listValue; }) attrsOfLists.${attrName}
) listOfAttrs
) [ { } ] (attrNames attrsOfLists);

/* Utility function that creates a `{name, value}` pair as expected by `builtins.listToAttrs`.
Expand Down Expand Up @@ -549,12 +543,10 @@ rec {
builtins.mapAttrs or (
f: set:
listToAttrs (
map
(attr: {
name = attr;
value = f attr set.${attr};
})
(attrNames set)
map (attr: {
name = attr;
value = f attr set.${attr};
}) (attrNames set)
)
);

Expand Down Expand Up @@ -750,12 +742,10 @@ rec {
# List of values from the list of attribute sets.
sets:
listToAttrs (
map
(name: {
inherit name;
value = f name (catAttrs name sets);
})
names
map (name: {
inherit name;
value = f name (catAttrs name sets);
}) names
);

/* Merge sets of attributes and use the function f to merge attribute values.
Expand Down Expand Up @@ -873,13 +863,10 @@ rec {
lhs:
# Right attribute set of the merge.
rhs:
recursiveUpdateUntil
(
path: lhs: rhs:
!(isAttrs lhs && isAttrs rhs)
)
lhs
rhs;
recursiveUpdateUntil (
path: lhs: rhs:
!(isAttrs lhs && isAttrs rhs)
) lhs rhs;

/* Returns true if the pattern is contained in the set. False otherwise.
Expand Down Expand Up @@ -1071,10 +1058,9 @@ rec {
let
intersection = builtins.intersectAttrs x y;
collisions = lib.concatStringsSep " " (builtins.attrNames intersection);
mask =
builtins.mapAttrs
(name: value: builtins.throw "unionOfDisjoint: collision on ${name}; complete list: ${collisions}")
intersection;
mask = builtins.mapAttrs (
name: value: builtins.throw "unionOfDisjoint: collision on ${name}; complete list: ${collisions}"
) intersection;
in
(x // y) // mask;

Expand Down
56 changes: 26 additions & 30 deletions lib/debug.nix
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,11 @@ rec {
let
res = f v;
in
lib.traceSeqN (depth + 1)
{
fn = name;
from = v;
to = res;
}
res;
lib.traceSeqN (depth + 1) {
fn = name;
from = v;
to = res;
} res;

# -- TESTING --

Expand Down Expand Up @@ -260,29 +258,27 @@ rec {
tests:
concatLists (
attrValues (
mapAttrs
(
name: test:
let
testsToRun = if tests ? tests then tests.tests else [ ];
in
if
(substring 0 4 name == "test" || elem name testsToRun)
&& ((testsToRun == [ ]) || elem name tests.tests)
&& (test.expr != test.expected)

then
[
{
inherit name;
expected = test.expected;
result = test.expr;
}
]
else
[ ]
)
tests
mapAttrs (
name: test:
let
testsToRun = if tests ? tests then tests.tests else [ ];
in
if
(substring 0 4 name == "test" || elem name testsToRun)
&& ((testsToRun == [ ]) || elem name tests.tests)
&& (test.expr != test.expected)

then
[
{
inherit name;
expected = test.expected;
result = test.expr;
}
]
else
[ ]
) tests
)
);

Expand Down
Loading

0 comments on commit 22c2ec5

Please sign in to comment.