Skip to content

Commit

Permalink
Preserve indentation on comments.
Browse files Browse the repository at this point in the history
As indentation is a very handy way to structure comments and increase
their readability as much of it as possible should be preserved.

This removes any trimming of leading white spaces and ensures that
any cbindgen annotations are still recognized afterwards.

... fixes #184
  • Loading branch information
0ndorio authored and emilio committed Aug 8, 2019
1 parent 47e8317 commit 1450e39
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 46 deletions.
22 changes: 13 additions & 9 deletions src/bindgen/ir/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ impl AnnotationSet {
}

pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> {
let lines: Vec<String> = attrs
.get_comment_lines()
.into_iter()
.filter(|x| !x.is_empty() && x.starts_with("cbindgen:"))
let lines = attrs.get_comment_lines();
let lines: Vec<&str> = lines
.iter()
.filter_map(|line| {
let line = line.trim_start();
if !line.starts_with("cbindgen:") {
return None;
}

Some(line)
})
.collect();

let must_use = attrs.has_attr_word("must_use");
Expand All @@ -62,12 +69,9 @@ impl AnnotationSet {

// Look at each line for an annotation
for line in lines {
// Skip lines that don't start with cbindgen
if !line.starts_with("cbindgen:") {
continue;
}
debug_assert!(line.starts_with("cbindgen:"));

// Remove the "cbingen:" prefix
// Remove the "cbindgen:" prefix
let annotation = &line[9..];

// Split the annotation in two
Expand Down
5 changes: 1 addition & 4 deletions src/bindgen/ir/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Documentation {
let doc = attrs
.get_comment_lines()
.into_iter()
.filter(|x| !x.starts_with("cbindgen:"))
.filter(|x| !x.trim_start().starts_with("cbindgen:"))
.collect();

Documentation { doc_comment: doc }
Expand Down Expand Up @@ -72,9 +72,6 @@ impl Source for Documentation {
DocumentationStyle::Auto => unreachable!(), // Auto case should always be covered
}

if line.len() != 0 {
out.write(" ");
}
write!(out, "{}", line);
out.new_line();
}
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl SynAttributeHelpers for [syn::Attribute] {
{
let name = ident.to_string();
if &*name == "doc" {
let text = content.value().trim().to_owned();
let text = content.value().trim_end().to_owned();
comment.push(text);
}
}
Expand Down
9 changes: 5 additions & 4 deletions tests/expectations/both/documentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);
9 changes: 5 additions & 4 deletions tests/expectations/both/documentation.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ extern "C" {
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);

Expand Down
9 changes: 5 additions & 4 deletions tests/expectations/documentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);
9 changes: 5 additions & 4 deletions tests/expectations/documentation.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ extern "C" {
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);

Expand Down
9 changes: 5 additions & 4 deletions tests/expectations/documentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ extern "C" {
///
/// ////////////////////////////////////////////////////////////////////////////
///
/// # Attention
/// Attention:
///
/// Rust is going to trim all leading `/` symbols. If you want to use them as a
/// marker you need to add at least a single whitespace inbetween the tripple
/// slash doc-comment marker and the rest.
///
/// Rust is going to trim all leading `/` symbols. If you want to use them as a
/// marker you need to add at least a single whitespace inbetween the tripple
/// slash doc-comment marker and the rest.
void root();

} // extern "C"
9 changes: 5 additions & 4 deletions tests/expectations/tag/documentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);
9 changes: 5 additions & 4 deletions tests/expectations/tag/documentation.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ extern "C" {
*
* ////////////////////////////////////////////////////////////////////////////
*
* # Attention
* Attention:
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*
* Rust is going to trim all leading `/` symbols. If you want to use them as a
* marker you need to add at least a single whitespace inbetween the tripple
* slash doc-comment marker and the rest.
*/
void root(void);

Expand Down
9 changes: 5 additions & 4 deletions tests/rust/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
///
/// ////////////////////////////////////////////////////////////////////////////
///
/// # Attention
/// Attention:
///
/// Rust is going to trim all leading `/` symbols. If you want to use them as a
/// marker you need to add at least a single whitespace inbetween the tripple
/// slash doc-comment marker and the rest.
///
/// Rust is going to trim all leading `/` symbols. If you want to use them as a
/// marker you need to add at least a single whitespace inbetween the tripple
/// slash doc-comment marker and the rest.
#[no_mangle]
pub extern "C" fn root() {
}

0 comments on commit 1450e39

Please sign in to comment.