From 48d17f54d34c5457e88ef7ffa234601c6e342a70 Mon Sep 17 00:00:00 2001 From: Marcus Klaas Date: Wed, 9 Sep 2015 23:15:37 +0200 Subject: [PATCH] Rebase onto master --- src/expr.rs | 28 ++------ src/lists.rs | 152 +++++++++++++++++++++--------------------- tests/target/match.rs | 2 +- 3 files changed, 85 insertions(+), 97 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 986b5d50a1f..4da191e8bf8 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -723,11 +723,8 @@ impl Rewrite for ast::Arm { // Let's try and get the arm body on the same line as the condition. // 4 = ` => `.len() if context.config.max_width > line_start + comma.len() + 4 { - let inner_offset = line_start + 4; let budget = context.config.max_width - line_start - comma.len() - 4; - if let Some(ref body_str) = body.rewrite(context, - budget, - line_start + 4) { + if let Some(ref body_str) = body.rewrite(context, budget, line_start + 4) { if first_line_width(body_str) <= budget { return Some(format!("{}{} => {}{}", attr_str.trim_left(), @@ -928,8 +925,12 @@ fn rewrite_call_inner(context: &RewriteContext, None => return Err(Ordering::Greater), }; let offset = offset + extra_offset + 1; - let inner_indent = expr_indent(context, offset); - let inner_context = context.overflow_context(inner_indent - context.block_indent); + let block_indent = if args.len() == 1 { + context.block_indent + } else { + offset + }; + let inner_context = &RewriteContext { block_indent: block_indent, ..*context }; let items = itemize_list(context.codemap, args.iter(), @@ -953,21 +954,6 @@ fn rewrite_call_inner(context: &RewriteContext, Ok(format!("{}({})", callee_str, list_str)) } -macro_rules! block_indent_helper { - ($name:ident, $option:ident) => ( - fn $name(context: &RewriteContext, offset: usize) -> usize { - match context.config.$option { - BlockIndentStyle::Inherit => context.block_indent, - BlockIndentStyle::Tabbed => context.block_indent + context.config.tab_spaces, - BlockIndentStyle::Visual => offset, - } - } - ); -} - -block_indent_helper!(expr_indent, expr_indent_style); -block_indent_helper!(closure_indent, closure_indent_style); - fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, width: usize, diff --git a/src/lists.rs b/src/lists.rs index 0afee8f79ac..3435bd8170b 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -269,18 +269,21 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3> fn next(&mut self) -> Option { let white_space: &[_] = &[' ', '\t']; - self.inner.next().map(|item| { - let mut new_lines = false; - // Pre-comment - let pre_snippet = self.codemap.span_to_snippet(codemap::mk_sp(self.prev_span_end, - (self.get_lo)(&item))) - .unwrap(); - let trimmed_pre_snippet = pre_snippet.trim(); - let pre_comment = if !trimmed_pre_snippet.is_empty() { - Some(trimmed_pre_snippet.to_owned()) - } else { - None - }; + self.inner + .next() + .map(|item| { + let mut new_lines = false; + // Pre-comment + let pre_snippet = self.codemap + .span_to_snippet(codemap::mk_sp(self.prev_span_end, + (self.get_lo)(&item))) + .unwrap(); + let trimmed_pre_snippet = pre_snippet.trim(); + let pre_comment = if !trimmed_pre_snippet.is_empty() { + Some(trimmed_pre_snippet.to_owned()) + } else { + None + }; // Post-comment let next_start = match self.inner.peek() { @@ -300,76 +303,75 @@ impl<'a, T, I, F1, F2, F3> Iterator for ListItems<'a, I, F1, F2, F3> match (block_open_index, newline_index) { // Separator before comment, with the next item on same line. - // Comment belongs to next item. - (Some(i), None) if i > separator_index => { - separator_index + 1 + // Comment belongs to next item. + (Some(i), None) if i > separator_index => { + separator_index + 1 + } + // Block-style post-comment before the separator. + (Some(i), None) => { + cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, + separator_index + 1) + } + // Block-style post-comment. Either before or after the separator. + (Some(i), Some(j)) if i < j => { + cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, + separator_index + 1) + } + // Potential *single* line comment. + (_, Some(j)) => j + 1, + _ => post_snippet.len(), } - // Block-style post-comment before the separator. - (Some(i), None) => { - cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, - separator_index + 1) - } - // Block-style post-comment. Either before or after the separator. - (Some(i), Some(j)) if i < j => { - cmp::max(find_comment_end(&post_snippet[i..]).unwrap() + i, - separator_index + 1) - } - // Potential *single* line comment. - (_, Some(j)) => j + 1, - _ => post_snippet.len() } - }, - None => { - post_snippet.find_uncommented(self.terminator) - .unwrap_or(post_snippet.len()) + None => { + post_snippet.find_uncommented(self.terminator).unwrap_or(post_snippet.len()) + } + }; + + if !post_snippet.is_empty() && comment_end > 0 { + // Account for extra whitespace between items. This is fiddly + // because of the way we divide pre- and post- comments. + + // Everything from the separator to the next item. + let test_snippet = &post_snippet[comment_end-1..]; + let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); + // From the end of the first line of comments. + let test_snippet = &test_snippet[first_newline..]; + let first = test_snippet.find(|c: char| !c.is_whitespace()) + .unwrap_or(test_snippet.len()); + // From the end of the first line of comments to the next non-whitespace char. + let test_snippet = &test_snippet[..first]; + + if test_snippet.chars().filter(|c| c == &'\n').count() > 1 { + // There were multiple line breaks which got trimmed to nothing. + new_lines = true; + } } - }; - if !post_snippet.is_empty() && comment_end > 0 { - // Account for extra whitespace between items. This is fiddly - // because of the way we divide pre- and post- comments. + // Cleanup post-comment: strip separators and whitespace. + self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32); + let post_snippet = post_snippet[..comment_end].trim(); - // Everything from the separator to the next item. - let test_snippet = &post_snippet[comment_end-1..]; - let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len()); - // From the end of the first line of comments. - let test_snippet = &test_snippet[first_newline..]; - let first = test_snippet.find(|c: char| !c.is_whitespace()) - .unwrap_or(test_snippet.len()); - // From the end of the first line of comments to the next non-whitespace char. - let test_snippet = &test_snippet[..first]; + let post_snippet_trimmed = if post_snippet.starts_with(',') { + post_snippet[1..].trim_matches(white_space) + } else if post_snippet.ends_with(",") { + post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) + } else { + post_snippet + }; - if test_snippet.chars().filter(|c| c == &'\n').count() > 1 { - // There were multiple line breaks which got trimmed to nothing. - new_lines = true; + let post_comment = if !post_snippet_trimmed.is_empty() { + Some(post_snippet_trimmed.to_owned()) + } else { + None + }; + + ListItem { + pre_comment: pre_comment, + item: (self.get_item_string)(&item), + post_comment: post_comment, + new_lines: new_lines, } - } - - // Cleanup post-comment: strip separators and whitespace. - self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32); - let post_snippet = post_snippet[..comment_end].trim(); - - let post_snippet_trimmed = if post_snippet.starts_with(',') { - post_snippet[1..].trim_matches(white_space) - } else if post_snippet.ends_with(",") { - post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) - } else { - post_snippet - }; - - let post_comment = if !post_snippet_trimmed.is_empty() { - Some(post_snippet_trimmed.to_owned()) - } else { - None - }; - - ListItem { - pre_comment: pre_comment, - item: (self.get_item_string)(&item), - post_comment: post_comment, - new_lines: new_lines, - } - }) + }) } } diff --git a/tests/target/match.rs b/tests/target/match.rs index eab8c604084..5cfbb5a7072 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -57,7 +57,7 @@ fn foo() { // Test that a match on an overflow line is laid out properly. fn main() { let sub_span = - match self.span.sub_span_after_keywooooooooooooooooooooord(use_item.span, keywords::As) { + match xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx { Some(sub_span) => Some(sub_span), None => sub_span, };