summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-09-13 12:56:14 -0400
committerGitHub <noreply@github.com>2018-09-13 12:56:14 -0400
commitd00b54c5d6df35b5ed21f89c72a9dd1a1b90f7c8 (patch)
tree746cf1f6240195e490e7faa8d438e695352e3411 /src
parent83751c8789b027c7b643e2d01768930c35241712 (diff)
parent7ad57a4c76043ac1ca720de727ea340f2dd5103b (diff)
Auto merge of #1384 - emilio:whitespace-comment, r=fitzgen
ir: Preserve better whitespace in comments. Fixes #1341.
Diffstat (limited to 'src')
-rw-r--r--src/ir/comment.rs25
1 files changed, 9 insertions, 16 deletions
diff --git a/src/ir/comment.rs b/src/ir/comment.rs
index 6a87c492..1a76542c 100644
--- a/src/ir/comment.rs
+++ b/src/ir/comment.rs
@@ -35,7 +35,6 @@ fn kind(comment: &str) -> Option<Kind> {
fn make_indent(indent: usize) -> String {
const RUST_INDENTATION: usize = 4;
-
iter::repeat(' ').take(indent * RUST_INDENTATION).collect()
}
@@ -49,12 +48,11 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
let mut is_first = true;
let lines: Vec<_> = comment
.lines()
- .map(|l| l.trim_left_matches('/').trim())
+ .map(|l| l.trim().trim_left_matches('/'))
.map(|l| {
let indent = if is_first { "" } else { &*indent };
is_first = false;
- let maybe_space = if l.is_empty() { "" } else { " " };
- format!("{}///{}{}", indent, maybe_space, l)
+ format!("{}///{}", indent, l)
})
.collect();
lines.join("\n")
@@ -63,30 +61,24 @@ fn preprocess_single_lines(comment: &str, indent: usize) -> String {
fn preprocess_multi_line(comment: &str, indent: usize) -> String {
let comment = comment
.trim_left_matches('/')
- .trim_left_matches('*')
- .trim_left_matches('!')
.trim_right_matches('/')
- .trim_right_matches('*')
- .trim();
+ .trim_right_matches('*');
let indent = make_indent(indent);
// Strip any potential `*` characters preceding each line.
let mut is_first = true;
let mut lines: Vec<_> = comment.lines()
- .map(|line| line.trim().trim_left_matches('*').trim())
- .skip_while(|line| line.is_empty()) // Skip the first empty lines.
+ .map(|line| line.trim().trim_left_matches('*').trim_left_matches('!'))
+ .skip_while(|line| line.trim().is_empty()) // Skip the first empty lines.
.map(|line| {
let indent = if is_first { "" } else { &*indent };
is_first = false;
- let maybe_space = if line.is_empty() { "" } else { " " };
- format!("{}///{}{}", indent, maybe_space, line)
+ format!("{}///{}", indent, line)
})
.collect();
// Remove the trailing line corresponding to the `*/`.
- let last_line_is_empty = lines.last().map_or(false, |l| l.is_empty());
-
- if last_line_is_empty {
+ if lines.last().map_or(false, |l| l.trim().is_empty() || l.trim() == "///") {
lines.pop();
}
@@ -107,6 +99,7 @@ mod test {
fn processes_single_lines_correctly() {
assert_eq!(preprocess("/// hello", 0), "/// hello");
assert_eq!(preprocess("// hello", 0), "/// hello");
+ assert_eq!(preprocess("// hello", 0), "/// hello");
}
#[test]
@@ -118,7 +111,7 @@ mod test {
assert_eq!(
preprocess("/**\nhello\n*world\n*foo\n*/", 0),
- "/// hello\n/// world\n/// foo"
+ "///hello\n///world\n///foo"
);
}
}