From 908cebf35c4e9be1fa91cb558389b7e80b6d0b77 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Mon, 19 Dec 2022 13:23:51 +0300 Subject: fix(tvix/builtin-macros): parse multi-line docstrings correctly Having a multi-line docstring yields multiple doc-attributes in order, however we were previously discarding all but the first one. This reduces them into a single string instead, which can then be displayed as multi-line documentation. Change-Id: I1f237956cdea2e4c746d3f13744e0373c1c645a6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7594 Reviewed-by: grfn Autosubmit: tazjin Tested-by: BuildkiteCI --- tvix/eval/builtin-macros/src/lib.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'tvix/eval/builtin-macros/src') diff --git a/tvix/eval/builtin-macros/src/lib.rs b/tvix/eval/builtin-macros/src/lib.rs index e815749ec6..ac994954ba 100644 --- a/tvix/eval/builtin-macros/src/lib.rs +++ b/tvix/eval/builtin-macros/src/lib.rs @@ -22,11 +22,14 @@ impl Parse for BuiltinArgs { } } -fn extract_docstring(attrs: &[Attribute]) -> Option { +fn extract_docstring(attrs: &[Attribute]) -> Option { // Rust docstrings are transparently written pre-macro expansion into an attribute that looks // like: // // #[doc = "docstring here"] + // + // Multi-line docstrings yield multiple attributes in order, which we assemble into a single + // string below. #[allow(dead_code)] #[derive(Debug)] @@ -47,8 +50,19 @@ fn extract_docstring(attrs: &[Attribute]) -> Option { attrs .iter() .filter(|attr| attr.path.get_ident().into_iter().any(|id| id == "doc")) - .find_map(|attr| parse2::(attr.tokens.clone()).ok()) - .map(|docstring| docstring.doc) + .filter_map(|attr| parse2::(attr.tokens.clone()).ok()) + .map(|docstring| docstring.doc.value()) + .reduce(|mut fst, snd| { + if snd.is_empty() { + // An empty string represents a spacing newline that was added in the + // original doc comment. + fst.push_str("\n\n"); + } else { + fst.push_str(&snd); + } + + fst + }) } /// Mark the annotated module as a module for defining Nix builtins. -- cgit 1.4.1