Skip to content

Commit

Permalink
avoid not to truncate necessary chars (#3640)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchaser53 authored and topecongiro committed Jun 23, 2019
1 parent 1d19a08 commit 5b0ce0e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
visitor.format_separate_mod(module, &*source_file);
};

debug_assert_eq!(visitor.line_number, count_newlines(&visitor.buffer));
debug_assert_eq!(
visitor.line_number,
count_newlines(&visitor.buffer),
"failed in format_file visitor.buffer:\n {:?}",
&visitor.buffer
);

// For some reason, the source_map does not include terminating
// newlines so we must add one on for each file. This is sad.
Expand Down
15 changes: 14 additions & 1 deletion src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,20 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
self.config.tab_spaces()
};
self.buffer.truncate(total_len - chars_too_many);

// FIXME this is a temporaly fix
// should be remove truncate logic in close_block
// avoid not to truncate necessary chars
let truncate_start = total_len - chars_too_many;
let target_str = &self.buffer[truncate_start..total_len];
let truncate_length = target_str.len() - target_str.trim().len();

if let Some(last_char) = target_str.chars().last() {
self.buffer.truncate(total_len - truncate_length);
if last_char == '\n' {
self.buffer.push_str("\n");
}
}
}
self.push_str("}");
self.block_indent = self.block_indent.block_unindent(self.config);
Expand Down
10 changes: 10 additions & 0 deletions tests/source/issue-3636.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-3636.rs","range":[4,7]}]

fn foo() {
let x =
42;
let y =
42;
let z = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let z = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
}
8 changes: 8 additions & 0 deletions tests/target/issue-3636.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-file_lines: [{"file":"tests/source/issue-3636.rs","range":[4,7]}]

fn foo() {
let x = 42;
let y = 42;
let z = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
let z = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
}

0 comments on commit 5b0ce0e

Please sign in to comment.