Skip to content

Commit

Permalink
Add support for handling line break in document attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKK committed Jan 20, 2020
1 parent 5b227c1 commit 37763ec
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/bindgen/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand All @@ -282,3 +281,15 @@ impl SynAttributeHelpers for [syn::Attribute] {
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()
}
20 changes: 20 additions & 0 deletions tests/expectations/both/documentation_attr.c
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 28 additions & 0 deletions tests/expectations/both/documentation_attr.compat.c
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions tests/expectations/documentation_attr.c
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 28 additions & 0 deletions tests/expectations/documentation_attr.compat.c
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions tests/expectations/documentation_attr.cpp
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions tests/expectations/tag/documentation_attr.c
Original file line number Diff line number Diff line change
@@ -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);
28 changes: 28 additions & 0 deletions tests/expectations/tag/documentation_attr.compat.c
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions tests/rust/documentation_attr.rs
Original file line number Diff line number Diff line change
@@ -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() {
}

0 comments on commit 37763ec

Please sign in to comment.