Skip to content

Commit

Permalink
handle unicode chars in closures (rust-lang#3632)
Browse files Browse the repository at this point in the history
The `NotUnicode` branch was unecessarily put on a new line, although it
was within max width:

```diff
 fn baz() {
     let our_error_b = result_b_from_func.or_else(|e| match e {
         NotPresent => Err(e).chain_err(|| "env var wasn't provided"),
-        NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"),
+        NotUnicode(_) => {
+            Err(e).chain_err(|| "env var was very very very bork文字化ã")
+        }
     });
 }
```
  • Loading branch information
scampi authored and topecongiro committed Jun 16, 2019
1 parent 944fc57 commit 84c2356
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,15 @@ fn shape_from_rhs_tactic(
}
}

/// Returns true if formatting next_line_rhs is better on a new line when compared to the
/// original's line formatting.
///
/// It is considered better if:
/// 1. the tactic is ForceNextLineWithoutIndent
/// 2. next_line_rhs doesn't have newlines
/// 3. the original line has more newlines than next_line_rhs
/// 4. the original formatting of the first line ends with `(`, `{`, or `[` and next_line_rhs
/// doesn't
pub(crate) fn prefer_next_line(
orig_rhs: &str,
next_line_rhs: &str,
Expand Down
9 changes: 6 additions & 3 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::config::lists::*;
use crate::config::{Config, IndentStyle};
use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape};
use crate::utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline};
use crate::utils::{
count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline,
unicode_str_width,
};
use crate::visitor::SnippetProvider;

pub(crate) struct ListFormatting<'a> {
Expand Down Expand Up @@ -386,7 +389,7 @@ where
result.push('\n');
result.push_str(indent_str);
// This is the width of the item (without comments).
line_len = item.item.as_ref().map_or(0, String::len);
line_len = item.item.as_ref().map_or(0, |s| unicode_str_width(&s));
}
} else {
result.push(' ');
Expand Down Expand Up @@ -808,7 +811,7 @@ where
pub(crate) fn total_item_width(item: &ListItem) -> usize {
comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..]))
+ comment_len(item.post_comment.as_ref().map(|x| &(*x)[..]))
+ item.item.as_ref().map_or(0, String::len)
+ &item.item.as_ref().map_or(0, |s| unicode_str_width(&s))
}

fn comment_len(comment: Option<&str>) -> usize {
Expand Down
3 changes: 3 additions & 0 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ fn rewrite_match_body(
next_line_body_shape.width,
);
match (orig_body, next_line_body) {
(Some(ref orig_str), Some(ref next_line_str)) if orig_str == next_line_str => {
combine_orig_body(orig_str)
}
(Some(ref orig_str), Some(ref next_line_str))
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>
{
Expand Down
8 changes: 8 additions & 0 deletions tests/source/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ pub fn bar(config: &Config) {
csv.write_record(None::<&[u8]>).unwrap();
}
}

// The NotUnicode line is below 100 wrt chars but over it wrt String::len
fn baz() {
let our_error_b = result_b_from_func.or_else(|e| match e {
NotPresent => Err(e).chain_err(|| "env var wasn't provided"),
NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"),
});
}
8 changes: 8 additions & 0 deletions tests/target/unicode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ pub fn bar(config: &Config) {
csv.write_record(None::<&[u8]>).unwrap();
}
}

// The NotUnicode line is below 100 wrt chars but over it wrt String::len
fn baz() {
let our_error_b = result_b_from_func.or_else(|e| match e {
NotPresent => Err(e).chain_err(|| "env var wasn't provided"),
NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"),
});
}

0 comments on commit 84c2356

Please sign in to comment.