I learned from this answer that I can generate docs from macros:
macro_rules! gen_fn {
(
$(#[$attr: meta])*
$name:ident
) => {
$(#[$attr])*
pub fn $name(self) {}
}
}
struct Baz {}
impl Baz {
gen_fn! {
/// This is a doc
/// with a very long text.
my_func
}
}
fn main() {
let o = Baz{};
o.my_func(); // generated method, with correct doc
}
However, if I pass more than one doc line to gen_fn
, it interprets each line as a new #[doc]
, thus transforming each line in to a new paragraph:
struct Baz {}
impl Baz {
gen_fn! {
/// This is a doc
/// with a very long text.
=> my_func
}
}
Instead of "This is a doc with a very long text"*, it becomes two paragraphs:
How can I rewrite gen_fn
so that it doesn't transform each new line into a paragraph?
Addendum:
I hacked out a way by directly inserting strings into a #[doc]
, but it looks ugly, so I wonder if is there a more elegant way:
macro_rules! gen_fn {
($name: ident, $comm: expr) => {
#[doc=$comm]
pub fn $name(self) {}
}
}
struct Baz {}
impl Baz {
gen_fn!(my_func,
"This is a doc
with a very long text.");
}
Update:
This was indeed a bug in rust-analyzer, and it was fixed here: