Skip to content

Commit 1cea171

Browse files
scampitopecongiro
authored andcommitted
Improve handling of unicode characters (#3618)
1 parent 04add0c commit 1cea171

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/chains.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ impl<'a> ChainFormatterShared<'a> {
557557
let almost_total = if extendable {
558558
prev_last_line_width
559559
} else {
560-
self.rewrites.iter().map(String::len).sum()
560+
self.rewrites
561+
.iter()
562+
.map(|rw| utils::unicode_str_width(&rw))
563+
.sum()
561564
} + last.tries;
562565
let one_line_budget = if self.child_count == 1 {
563566
shape.width

src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::types::{rewrite_path, PathContext};
3333
use crate::utils::{
3434
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
3535
last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
36-
semicolon_for_expr, semicolon_for_stmt, wrap_str,
36+
semicolon_for_expr, semicolon_for_stmt, unicode_str_width, wrap_str,
3737
};
3838
use crate::vertical::rewrite_with_alignment;
3939
use crate::visitor::FmtVisitor;
@@ -1973,7 +1973,9 @@ fn choose_rhs<R: Rewrite>(
19731973
rhs_tactics: RhsTactics,
19741974
) -> Option<String> {
19751975
match orig_rhs {
1976-
Some(ref new_str) if !new_str.contains('\n') && new_str.len() <= shape.width => {
1976+
Some(ref new_str)
1977+
if !new_str.contains('\n') && unicode_str_width(new_str) <= shape.width =>
1978+
{
19771979
Some(format!(" {}", new_str))
19781980
}
19791981
_ => {

tests/source/unicode.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn foo() {
4+
let s = "this line goes to 100: ͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶ";
5+
let s = 42;
6+
7+
// a comment of length 80, with the starting sigil: ҘҘҘҘҘҘҘҘҘҘ ҘҘҘҘҘҘҘҘҘҘҘҘҘҘ
8+
let s = 42;
9+
}
10+
11+
pub fn bar(config: &Config) {
12+
let csv = RefCell::new(create_csv(config, "foo"));
13+
{
14+
let mut csv = csv.borrow_mut();
15+
for (i1, i2, i3) in iproduct!(0..2, 0..3, 0..3) {
16+
csv.write_field(format!("γ[{}.{}.{}]", i1, i2, i3))
17+
.unwrap();
18+
csv.write_field(format!("d[{}.{}.{}]", i1, i2, i3))
19+
.unwrap();
20+
csv.write_field(format!("i[{}.{}.{}]", i1, i2, i3))
21+
.unwrap();
22+
}
23+
csv.write_record(None::<&[u8]>).unwrap();
24+
}
25+
}

tests/target/unicode.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// rustfmt-wrap_comments: true
2+
3+
fn foo() {
4+
let s = "this line goes to 100: ͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶͶ";
5+
let s = 42;
6+
7+
// a comment of length 80, with the starting sigil: ҘҘҘҘҘҘҘҘҘҘ ҘҘҘҘҘҘҘҘҘҘҘҘҘҘ
8+
let s = 42;
9+
}
10+
11+
pub fn bar(config: &Config) {
12+
let csv = RefCell::new(create_csv(config, "foo"));
13+
{
14+
let mut csv = csv.borrow_mut();
15+
for (i1, i2, i3) in iproduct!(0..2, 0..3, 0..3) {
16+
csv.write_field(format!("γ[{}.{}.{}]", i1, i2, i3)).unwrap();
17+
csv.write_field(format!("d[{}.{}.{}]", i1, i2, i3)).unwrap();
18+
csv.write_field(format!("i[{}.{}.{}]", i1, i2, i3)).unwrap();
19+
}
20+
csv.write_record(None::<&[u8]>).unwrap();
21+
}
22+
}

0 commit comments

Comments
 (0)