Skip to content

Commit 96f0417

Browse files
MattSturgeonfricklerhandwerk
authored andcommitted
doc/packages/treefmt: add location to function reference docs
Get the function's source code location using `unsafeGetAttrPos` and pass that to `nixdoc` so that links to definitions are included in the docs.
1 parent 301824d commit 96f0417

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pkgs/by-name/tr/treefmt/functions-doc.nix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
{
2+
lib,
3+
writers,
24
nixdoc,
35
runCommand,
6+
treefmt,
47
}:
8+
let
9+
root = toString ./.;
10+
revision = lib.trivial.revisionWithDefault "master";
11+
removeRoot = file: lib.removePrefix "/" (lib.removePrefix root file);
12+
13+
# Import and apply `./lib.nix`, which contains treefmt's public functions
14+
#
15+
# NOTE: we cannot access them via `treefmt.passthru` or `callPackages ./lib.nix { }`,
16+
# because that would be opaque to `unsafeGetAttrPos`.
17+
attrs =
18+
let
19+
fn = import ./lib.nix;
20+
args = builtins.mapAttrs (_: _: null) (builtins.functionArgs fn);
21+
in
22+
fn args;
23+
in
524
{
25+
locations = lib.pipe attrs [
26+
builtins.attrNames
27+
(builtins.map (
28+
name:
29+
let
30+
pos = builtins.unsafeGetAttrPos name attrs;
31+
file = removeRoot pos.file;
32+
line = toString pos.line;
33+
subpath = "pkgs/by-name/tr/treefmt/${file}";
34+
url = "https://github.com/NixOS/nixpkgs/blob/${revision}/${subpath}#L${line}";
35+
in
36+
assert lib.hasPrefix root pos.file;
37+
lib.nameValuePair "pkgs.treefmt.${name}" "[${subpath}:${line}](${url}) in `<nixpkgs>`"
38+
))
39+
builtins.listToAttrs
40+
(writers.writeJSON "treefmt-function-locations")
41+
];
42+
643
markdown =
744
runCommand "treefmt-functions-doc"
845
{
946
nativeBuildInputs = [ nixdoc ];
1047
}
1148
''
1249
nixdoc --file ${./lib.nix} \
50+
--locs ${treefmt.functionsDoc.locations} \
1351
--description "Functions Reference" \
1452
--prefix "pkgs" \
1553
--category "treefmt" \

0 commit comments

Comments
 (0)