Add support for handling line break in document attribute

This commit is contained in:
yingrueil
2020-01-13 13:30:29 +08:00
committed by Emilio Cobos Álvarez
parent ff8e5d591d
commit 8a5662b4a7
9 changed files with 191 additions and 2 deletions
+13 -2
View File
@@ -272,8 +272,7 @@ impl SynAttributeHelpers for [syn::Attribute] {
})) = attr.parse_meta() })) = attr.parse_meta()
{ {
if path.is_ident("doc") { if path.is_ident("doc") {
let text = content.value().trim_end().to_owned(); comment.extend(split_doc_attr(&content.value()));
comment.push(text);
} }
} }
} }
@@ -282,3 +281,15 @@ impl SynAttributeHelpers for [syn::Attribute] {
comment comment
} }
} }
fn split_doc_attr(input: &str) -> Vec<String> {
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()
}
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
*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);
@@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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
+20
View File
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
*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);
@@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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
+22
View File
@@ -0,0 +1,22 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
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"
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
*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);
@@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#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
+12
View File
@@ -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() {
}