Use multiline when args exceeds max_width in fn_call_style = "block"

This commit is contained in:
topecongiro 2017-05-10 00:27:57 +09:00
parent 35e1ca73fc
commit 30a8050559
3 changed files with 91 additions and 62 deletions

View File

@ -9,7 +9,6 @@
// except according to those terms. // except according to those terms.
use std::cmp::{Ordering, min}; use std::cmp::{Ordering, min};
use std::borrow::Borrow;
use std::mem::swap; use std::mem::swap;
use std::ops::Deref; use std::ops::Deref;
use std::iter::ExactSizeIterator; use std::iter::ExactSizeIterator;
@ -1638,47 +1637,75 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
-> Result<String, Ordering> -> Result<String, Ordering>
where R: Rewrite where R: Rewrite
{ {
let callee = callee.borrow();
// FIXME using byte lens instead of char lens (and probably all over the // FIXME using byte lens instead of char lens (and probably all over the
// place too) // place too)
let callee_str = match callee.rewrite(context, let callee_shape = Shape {
Shape { width: max_callee_width,
width: max_callee_width, ..shape
..shape };
}) { let callee_str = callee
Some(string) => { .rewrite(context, callee_shape)
if !string.contains('\n') && string.len() > max_callee_width { .ok_or(Ordering::Greater)?;
panic!("{:?} {}", string, max_callee_width);
} else { // 4 = `( )`, 2 = `()`
string let paren_overhead = if context.config.spaces_within_parens {
4
} else {
2
};
let used_width = extra_offset(&callee_str, shape);
let one_line_width = shape
.width
.checked_sub(used_width + paren_overhead)
.ok_or(Ordering::Greater)?;
let nested_shape = match context.config.fn_call_style {
IndentStyle::Block => shape.block().block_left(context.config.tab_spaces),
// 1 = (
IndentStyle::Visual => {
shape
.visual_indent(used_width + 1)
.sub_width(used_width + paren_overhead)
} }
} }
None => return Err(Ordering::Greater), .ok_or(Ordering::Greater)?;
};
let span_lo = context.codemap.span_after(span, "("); let span_lo = context.codemap.span_after(span, "(");
let span = mk_sp(span_lo, span.hi); let span = mk_sp(span_lo, span.hi);
let used_width = extra_offset(&callee_str, shape); let list_str = rewrite_call_args(context,
args,
span,
nested_shape,
one_line_width,
force_no_trailing_comma)
.ok_or(Ordering::Less)?;
let nested_shape = match context.config.fn_call_style { let result = if context.config.fn_call_style == IndentStyle::Visual ||
IndentStyle::Block => { (!list_str.contains('\n') && list_str.chars().last().unwrap_or(' ') != ',') {
shape if context.config.spaces_within_parens && list_str.len() > 0 {
.block() format!("{}( {} )", callee_str, list_str)
.block_indent(context.config.tab_spaces) } else {
.sub_width(context.config.tab_spaces) format!("{}({})", callee_str, list_str)
}
// 1 = (, 2 = ().
IndentStyle::Visual => {
shape
.visual_indent(used_width + 1)
.sub_width(used_width + 2)
} }
} else {
format!("{}(\n{}{}\n{})",
callee_str,
nested_shape.indent.to_string(context.config),
list_str,
shape.block().indent.to_string(context.config))
}; };
let nested_shape = match nested_shape {
Some(s) => s, Ok(result)
None => return Err(Ordering::Greater), }
};
fn rewrite_call_args(context: &RewriteContext,
args: &[ptr::P<ast::Expr>],
span: Span,
shape: Shape,
one_line_width: usize,
force_no_trailing_comma: bool)
-> Option<String> {
let arg_count = args.len(); let arg_count = args.len();
let items = itemize_list(context.codemap, let items = itemize_list(context.codemap,
@ -1686,7 +1713,7 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
")", ")",
|item| item.span.lo, |item| item.span.lo,
|item| item.span.hi, |item| item.span.hi,
|item| item.rewrite(context, nested_shape), |item| item.rewrite(context, shape),
span.lo, span.lo,
span.hi); span.hi);
let mut item_vec: Vec<_> = items.collect(); let mut item_vec: Vec<_> = items.collect();
@ -1708,8 +1735,8 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
// first arguments. // first arguments.
if overflow_last { if overflow_last {
let nested_shape = Shape { let nested_shape = Shape {
indent: nested_shape.indent.block_only(), indent: shape.indent.block_only(),
..nested_shape ..shape
}; };
let rewrite = args.last().unwrap().rewrite(context, nested_shape); let rewrite = args.last().unwrap().rewrite(context, nested_shape);
@ -1722,15 +1749,11 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
} }
} }
let one_line_width = shape.width.checked_sub(used_width + 2);
let one_line_width = match one_line_width {
Some(olw) => olw,
None => return Err(Ordering::Greater),
};
let one_line_shape = Shape { let one_line_shape = Shape {
width: one_line_width, width: one_line_width,
..nested_shape ..shape
}; };
let tactic = let tactic =
definitive_tactic(&item_vec, definitive_tactic(&item_vec,
ListTactic::LimitedHorizontalVertical(context.config.fn_call_width), ListTactic::LimitedHorizontalVertical(context.config.fn_call_width),
@ -1748,12 +1771,12 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
(false, _, _) => {} (false, _, _) => {}
} }
let fmt = ListFormatting { let mut fmt = ListFormatting {
tactic: tactic, tactic: tactic,
separator: ",", separator: ",",
trailing_separator: if force_no_trailing_comma || trailing_separator: if force_no_trailing_comma ||
context.config.fn_call_style == IndentStyle::Visual || context.config.fn_call_style == IndentStyle::Visual ||
args.len() <= 1 { arg_count <= 1 {
SeparatorTactic::Never SeparatorTactic::Never
} else { } else {
context.config.trailing_comma context.config.trailing_comma
@ -1763,27 +1786,13 @@ fn rewrite_call_inner<R>(context: &RewriteContext,
config: context.config, config: context.config,
}; };
let list_str = match write_list(&item_vec, &fmt) { match write_list(&item_vec, &fmt) {
Some(str) => str, Some(ref s) if !s.contains('\n') && s.len() > one_line_width => {
None => return Err(Ordering::Less), fmt.trailing_separator = SeparatorTactic::Vertical;
}; write_list(&item_vec, &fmt)
let result = if context.config.fn_call_style == IndentStyle::Visual ||
!list_str.contains('\n') {
if context.config.spaces_within_parens && list_str.len() > 0 {
format!("{}( {} )", callee_str, list_str)
} else {
format!("{}({})", callee_str, list_str)
} }
} else { rewrite @ _ => rewrite,
format!("{}(\n{}{}\n{})", }
callee_str,
nested_shape.indent.to_string(context.config),
list_str,
shape.block().indent.to_string(context.config))
};
Ok(result)
} }
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> { fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {

View File

@ -6,3 +6,12 @@ fn main() {
// #1501 // #1501
let hyper = Arc::new(Client::with_connector(HttpsConnector::new(TlsClient::new()))); let hyper = Arc::new(Client::with_connector(HttpsConnector::new(TlsClient::new())));
} }
// #1521
impl Foo {
fn map_pixel_to_coords(&self, point: &Vector2i, view: &View) -> Vector2f {
unsafe {
Vector2f::from_raw(ffi::sfRenderTexture_mapPixelToCoords(self.render_texture, point.raw(), view.raw()))
}
}
}

View File

@ -15,3 +15,14 @@ fn main() {
// #1501 // #1501
let hyper = Arc::new(Client::with_connector(HttpsConnector::new(TlsClient::new()))); let hyper = Arc::new(Client::with_connector(HttpsConnector::new(TlsClient::new())));
} }
// #1521
impl Foo {
fn map_pixel_to_coords(&self, point: &Vector2i, view: &View) -> Vector2f {
unsafe {
Vector2f::from_raw(
ffi::sfRenderTexture_mapPixelToCoords(self.render_texture, point.raw(), view.raw()),
)
}
}
}