Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid not to truncate necessary chars #3640

Merged
merged 3 commits into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}