From d121d7205f7a41859013de8ae6cea97ea623f1b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Fri, 9 Nov 2018 20:50:07 +0100 Subject: [PATCH] fix logic for adding or not a newline after a missed span --- src/missed_spans.rs | 17 +++++++++-------- src/visitor.rs | 2 +- tests/target/issue-3032.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 tests/target/issue-3032.rs diff --git a/src/missed_spans.rs b/src/missed_spans.rs index 237a31e86b883..3c67108b1f197 100644 --- a/src/missed_spans.rs +++ b/src/missed_spans.rs @@ -259,15 +259,16 @@ impl<'a> FmtVisitor<'a> { status.last_wspace = None; status.line_start = offset + subslice.len(); - if let Some('/') = subslice.chars().nth(1) { - // Only add a newline if the last line is a line comment - if !subslice.trim_end().ends_with("*/") { - self.push_str("\n"); - } - } else if status.line_start <= snippet.len() { - // For other comments add a newline if there isn't one at the end already + // Add a newline: + // - if there isn't one already + // - otherwise, only if the last line is a line comment + if status.line_start <= snippet.len() { match snippet[status.line_start..].chars().next() { - Some('\n') | Some('\r') => (), + Some('\n') | Some('\r') => { + if !subslice.trim_end().ends_with("*/") { + self.push_str("\n"); + } + } _ => self.push_str("\n"), } } diff --git a/src/visitor.rs b/src/visitor.rs index b286465b2765f..612c8a3b8eb67 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -236,7 +236,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { } } - let unindent_comment = (self.is_if_else_block && !b.stmts.is_empty()) && { + let unindent_comment = self.is_if_else_block && !b.stmts.is_empty() && { let end_pos = source!(self, b.span).hi() - brace_compensation - remove_len; let snippet = self.snippet(mk_sp(self.last_pos, end_pos)); snippet.contains("//") || snippet.contains("/*") diff --git a/tests/target/issue-3032.rs b/tests/target/issue-3032.rs new file mode 100644 index 0000000000000..a0ebf4b7c7387 --- /dev/null +++ b/tests/target/issue-3032.rs @@ -0,0 +1,36 @@ +pub fn get_array_index_from_id(_cx: *mut JSContext, id: HandleId) -> Option { + let raw_id = id.into(); + unsafe { + if RUST_JSID_IS_INT(raw_id) { + return Some(RUST_JSID_TO_INT(raw_id) as u32); + } + None + } + // if id is length atom, -1, otherwise + /*return if JSID_IS_ATOM(id) { + let atom = JSID_TO_ATOM(id); + //let s = *GetAtomChars(id); + if s > 'a' && s < 'z' { + return -1; + } + + let i = 0; + let str = AtomToLinearString(JSID_TO_ATOM(id)); + return if StringIsArray(str, &mut i) != 0 { i } else { -1 } + } else { + IdToInt32(cx, id); + }*/ +} + +impl Foo { + fn bar() -> usize { + 42 + /* a block comment */ + } + + fn baz() -> usize { + 42 + // this is a line + /* a block comment */ + } +}