Skip to content

Commit

Permalink
Merge pull request rust-lang#3189 from scampi/issue3032
Browse files Browse the repository at this point in the history
fix logic for adding or not a newline after a missed span
  • Loading branch information
nrc authored Nov 11, 2018
2 parents 4e2f741 + d121d72 commit 34333ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("/*")
Expand Down
36 changes: 36 additions & 0 deletions tests/target/issue-3032.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub fn get_array_index_from_id(_cx: *mut JSContext, id: HandleId) -> Option<u32> {
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 */
}
}

0 comments on commit 34333ba

Please sign in to comment.