From 1450e39d8f6e094ed2b8e6bdb771432acaebbe0d Mon Sep 17 00:00:00 2001 From: Bruno Kirschner Date: Wed, 7 Aug 2019 17:04:35 +0200 Subject: [PATCH] Preserve indentation on comments. 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 --- src/bindgen/ir/annotation.rs | 22 +++++++++++-------- src/bindgen/ir/documentation.rs | 5 +---- src/bindgen/utilities.rs | 2 +- tests/expectations/both/documentation.c | 9 ++++---- .../expectations/both/documentation.compat.c | 9 ++++---- tests/expectations/documentation.c | 9 ++++---- tests/expectations/documentation.compat.c | 9 ++++---- tests/expectations/documentation.cpp | 9 ++++---- tests/expectations/tag/documentation.c | 9 ++++---- tests/expectations/tag/documentation.compat.c | 9 ++++---- tests/rust/documentation.rs | 9 ++++---- 11 files changed, 55 insertions(+), 46 deletions(-) diff --git a/src/bindgen/ir/annotation.rs b/src/bindgen/ir/annotation.rs index 6a15223..34bf91c 100644 --- a/src/bindgen/ir/annotation.rs +++ b/src/bindgen/ir/annotation.rs @@ -50,10 +50,17 @@ impl AnnotationSet { } pub fn load(attrs: &[syn::Attribute]) -> Result { - let lines: Vec = 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"); @@ -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 diff --git a/src/bindgen/ir/documentation.rs b/src/bindgen/ir/documentation.rs index 2ab7708..4e87cdf 100644 --- a/src/bindgen/ir/documentation.rs +++ b/src/bindgen/ir/documentation.rs @@ -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 } @@ -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(); } diff --git a/src/bindgen/utilities.rs b/src/bindgen/utilities.rs index d918bcd..1c59c45 100644 --- a/src/bindgen/utilities.rs +++ b/src/bindgen/utilities.rs @@ -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); } } diff --git a/tests/expectations/both/documentation.c b/tests/expectations/both/documentation.c index 16d5492..e455b8d 100644 --- a/tests/expectations/both/documentation.c +++ b/tests/expectations/both/documentation.c @@ -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); diff --git a/tests/expectations/both/documentation.compat.c b/tests/expectations/both/documentation.compat.c index 03d8c7b..fcdb544 100644 --- a/tests/expectations/both/documentation.compat.c +++ b/tests/expectations/both/documentation.compat.c @@ -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); diff --git a/tests/expectations/documentation.c b/tests/expectations/documentation.c index 16d5492..e455b8d 100644 --- a/tests/expectations/documentation.c +++ b/tests/expectations/documentation.c @@ -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); diff --git a/tests/expectations/documentation.compat.c b/tests/expectations/documentation.compat.c index 03d8c7b..fcdb544 100644 --- a/tests/expectations/documentation.compat.c +++ b/tests/expectations/documentation.compat.c @@ -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); diff --git a/tests/expectations/documentation.cpp b/tests/expectations/documentation.cpp index 47878b3..ba86fa2 100644 --- a/tests/expectations/documentation.cpp +++ b/tests/expectations/documentation.cpp @@ -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" diff --git a/tests/expectations/tag/documentation.c b/tests/expectations/tag/documentation.c index 16d5492..e455b8d 100644 --- a/tests/expectations/tag/documentation.c +++ b/tests/expectations/tag/documentation.c @@ -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); diff --git a/tests/expectations/tag/documentation.compat.c b/tests/expectations/tag/documentation.compat.c index 03d8c7b..fcdb544 100644 --- a/tests/expectations/tag/documentation.compat.c +++ b/tests/expectations/tag/documentation.compat.c @@ -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); diff --git a/tests/rust/documentation.rs b/tests/rust/documentation.rs index de2ecdc..56da3c3 100644 --- a/tests/rust/documentation.rs +++ b/tests/rust/documentation.rs @@ -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() { }