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.
This commit is contained in:
parent
301824d216
commit
96f041725f
@ -1,8 +1,45 @@
|
|||||||
{
|
{
|
||||||
|
lib,
|
||||||
|
writers,
|
||||||
nixdoc,
|
nixdoc,
|
||||||
runCommand,
|
runCommand,
|
||||||
|
treefmt,
|
||||||
}:
|
}:
|
||||||
|
let
|
||||||
|
root = toString ./.;
|
||||||
|
revision = lib.trivial.revisionWithDefault "master";
|
||||||
|
removeRoot = file: lib.removePrefix "/" (lib.removePrefix root file);
|
||||||
|
|
||||||
|
# Import and apply `./lib.nix`, which contains treefmt's public functions
|
||||||
|
#
|
||||||
|
# NOTE: we cannot access them via `treefmt.passthru` or `callPackages ./lib.nix { }`,
|
||||||
|
# because that would be opaque to `unsafeGetAttrPos`.
|
||||||
|
attrs =
|
||||||
|
let
|
||||||
|
fn = import ./lib.nix;
|
||||||
|
args = builtins.mapAttrs (_: _: null) (builtins.functionArgs fn);
|
||||||
|
in
|
||||||
|
fn args;
|
||||||
|
in
|
||||||
{
|
{
|
||||||
|
locations = lib.pipe attrs [
|
||||||
|
builtins.attrNames
|
||||||
|
(builtins.map (
|
||||||
|
name:
|
||||||
|
let
|
||||||
|
pos = builtins.unsafeGetAttrPos name attrs;
|
||||||
|
file = removeRoot pos.file;
|
||||||
|
line = toString pos.line;
|
||||||
|
subpath = "pkgs/by-name/tr/treefmt/${file}";
|
||||||
|
url = "https://github.com/NixOS/nixpkgs/blob/${revision}/${subpath}#L${line}";
|
||||||
|
in
|
||||||
|
assert lib.hasPrefix root pos.file;
|
||||||
|
lib.nameValuePair "pkgs.treefmt.${name}" "[${subpath}:${line}](${url}) in `<nixpkgs>`"
|
||||||
|
))
|
||||||
|
builtins.listToAttrs
|
||||||
|
(writers.writeJSON "treefmt-function-locations")
|
||||||
|
];
|
||||||
|
|
||||||
markdown =
|
markdown =
|
||||||
runCommand "treefmt-functions-doc"
|
runCommand "treefmt-functions-doc"
|
||||||
{
|
{
|
||||||
@ -10,6 +47,7 @@
|
|||||||
}
|
}
|
||||||
''
|
''
|
||||||
nixdoc --file ${./lib.nix} \
|
nixdoc --file ${./lib.nix} \
|
||||||
|
--locs ${treefmt.functionsDoc.locations} \
|
||||||
--description "Functions Reference" \
|
--description "Functions Reference" \
|
||||||
--prefix "pkgs" \
|
--prefix "pkgs" \
|
||||||
--category "treefmt" \
|
--category "treefmt" \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user