diff --git a/src/bindgen/utilities.rs b/src/bindgen/utilities.rs index 963b0a641..c29afa90d 100644 --- a/src/bindgen/utilities.rs +++ b/src/bindgen/utilities.rs @@ -272,8 +272,7 @@ impl SynAttributeHelpers for [syn::Attribute] { })) = attr.parse_meta() { if path.is_ident("doc") { - let text = content.value().trim_end().to_owned(); - comment.push(text); + comment.extend(split_doc_attr(&content.value())); } } } @@ -282,3 +281,15 @@ impl SynAttributeHelpers for [syn::Attribute] { comment } } + +fn split_doc_attr(input: &str) -> Vec { + input + // Convert two newline (indicate "new paragraph") into two line break. + .replace("\n\n", " \n \n") + // Convert newline after two spaces (indicate "line break") into line break. + .split(" \n") + // Convert single newline (indicate hard-wrapped) into space. + .map(|s| s.replace('\n', " ")) + .map(|s| s.trim_end().to_string()) + .collect() +} diff --git a/tests/expectations/both/documentation_attr.c b/tests/expectations/both/documentation_attr.c new file mode 100644 index 000000000..d9f6eff1f --- /dev/null +++ b/tests/expectations/both/documentation_attr.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); diff --git a/tests/expectations/both/documentation_attr.compat.c b/tests/expectations/both/documentation_attr.compat.c new file mode 100644 index 000000000..e7fe43bc2 --- /dev/null +++ b/tests/expectations/both/documentation_attr.compat.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/documentation_attr.c b/tests/expectations/documentation_attr.c new file mode 100644 index 000000000..d9f6eff1f --- /dev/null +++ b/tests/expectations/documentation_attr.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); diff --git a/tests/expectations/documentation_attr.compat.c b/tests/expectations/documentation_attr.compat.c new file mode 100644 index 000000000..e7fe43bc2 --- /dev/null +++ b/tests/expectations/documentation_attr.compat.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/expectations/documentation_attr.cpp b/tests/expectations/documentation_attr.cpp new file mode 100644 index 000000000..e5ef4c945 --- /dev/null +++ b/tests/expectations/documentation_attr.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +extern "C" { + +///With doc attr, each attr contribute to one line of document +///like this one with a new line character at its end +///and this one as well. So they are in the same paragraph +/// +///Line ends with one new line should not break +/// +///Line ends with two spaces and a new line +///should break to next line +/// +///Line ends with two new lines +/// +///Should break to next paragraph +void root(); + +} // extern "C" diff --git a/tests/expectations/tag/documentation_attr.c b/tests/expectations/tag/documentation_attr.c new file mode 100644 index 000000000..d9f6eff1f --- /dev/null +++ b/tests/expectations/tag/documentation_attr.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); diff --git a/tests/expectations/tag/documentation_attr.compat.c b/tests/expectations/tag/documentation_attr.compat.c new file mode 100644 index 000000000..e7fe43bc2 --- /dev/null +++ b/tests/expectations/tag/documentation_attr.compat.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/** + *With doc attr, each attr contribute to one line of document + *like this one with a new line character at its end + *and this one as well. So they are in the same paragraph + * + *Line ends with one new line should not break + * + *Line ends with two spaces and a new line + *should break to next line + * + *Line ends with two new lines + * + *Should break to next paragraph + */ +void root(void); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus diff --git a/tests/rust/documentation_attr.rs b/tests/rust/documentation_attr.rs new file mode 100644 index 000000000..d88ce806b --- /dev/null +++ b/tests/rust/documentation_attr.rs @@ -0,0 +1,12 @@ +#[doc="With doc attr, each attr contribute to one line of document"] +#[doc="like this one with a new line character at its end"] +#[doc="and this one as well. So they are in the same paragraph"] +#[doc=""] +#[doc="Line ends with one new line\nshould not break"] +#[doc=""] +#[doc="Line ends with two spaces and a new line \nshould break to next line"] +#[doc=""] +#[doc="Line ends with two new lines\n\nShould break to next paragraph"] +#[no_mangle] +pub extern "C" fn root() { +}