Skip to content

handle unicode chars in closures #3632

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

Merged
merged 1 commit into from
Jun 16, 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
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文字化ã"),
});
}