-1

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:

Note how each line became a new paragraph

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:

rodrigocfd
  • 6,450
  • 6
  • 34
  • 68

1 Answers1

0

Looks fine to me when documented via cargo doc with Rust 1.48.0:

properly-formatted documentation

<div class="docblock"><p>This is a doc
with a very long text.</p>
</div>

The issue appears to be with how your editor is showing the documentation.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366