Merge pull request #246 from nrc/bugs2

Format <foo + 'bar> nicely
This commit is contained in:
Nick Cameron 2015-09-04 17:07:34 +12:00
commit 17302f8967
4 changed files with 64 additions and 28 deletions

View File

@ -35,7 +35,8 @@ impl<'a> FmtVisitor<'a> {
if let Some(ref ty) = local.ty {
infix.push_str(": ");
infix.push_str(&pprust::ty_to_string(ty));
// FIXME silly width, indent
infix.push_str(&ty.rewrite(&self.get_context(), 1000, 0).unwrap());
}
if local.init.is_some() {
@ -205,7 +206,7 @@ impl<'a> FmtVisitor<'a> {
codemap::mk_sp(span.lo,
span_for_return(&fd.output).lo)));
let ret_str = self.rewrite_return(&fd.output);
let ret_str = self.rewrite_return(&fd.output, indent);
// Args.
let (one_line_budget, multi_line_budget, mut arg_indent) =
@ -504,7 +505,11 @@ impl<'a> FmtVisitor<'a> {
")",
|arg| arg.ty.span.lo,
|arg| arg.ty.span.hi,
|arg| pprust::ty_to_string(&arg.ty),
|arg| {
// FIXME silly width, indent
arg.ty.rewrite(&self.get_context(), 1000, 0)
.unwrap()
},
span_after(field.span, "(", self.codemap),
next_span_start);
@ -731,7 +736,8 @@ impl<'a> FmtVisitor<'a> {
ast::StructFieldKind::NamedField(_, vis) |
ast::StructFieldKind::UnnamedField(vis) => format_visibility(vis),
};
let typ = pprust::ty_to_string(&field.node.ty);
// FIXME silly width, indent
let typ = field.node.ty.rewrite(&self.get_context(), 1000, 0).unwrap();
let indent = self.block_indent + self.config.tab_spaces;
let mut attr_str = field.node.attrs
@ -877,11 +883,14 @@ impl<'a> FmtVisitor<'a> {
}
}
fn rewrite_return(&self, ret: &ast::FunctionRetTy) -> String {
fn rewrite_return(&self, ret: &ast::FunctionRetTy, indent: usize) -> String {
match *ret {
ast::FunctionRetTy::DefaultReturn(_) => String::new(),
ast::FunctionRetTy::NoReturn(_) => "-> !".to_owned(),
ast::FunctionRetTy::Return(ref ty) => "-> ".to_owned() + &pprust::ty_to_string(ty),
ast::FunctionRetTy::Return(ref ty) => {
let ctxt = &self.get_context();
format!("-> {}", ty.rewrite(ctxt, ctxt.config.max_width, indent).unwrap())
}
}
}
}

View File

@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::fmt;
use syntax::ast;
use syntax::print::pprust;
use syntax::codemap::{self, Span, BytePos, CodeMap};
@ -108,6 +106,7 @@ fn rewrite_path_segments<'a, I>(mut buffer: String,
Some(buffer)
}
#[derive(Debug)]
enum SegmentParam<'a> {
LifeTime(&'a ast::Lifetime),
Type(&'a ast::Ty),
@ -124,19 +123,20 @@ impl<'a> SegmentParam<'a> {
}
}
impl<'a> fmt::Display for SegmentParam<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SegmentParam::LifeTime(ref lt) => {
write!(f, "{}", pprust::lifetime_to_string(lt))
}
SegmentParam::Type(ref ty) => {
write!(f, "{}", pprust::ty_to_string(ty))
}
SegmentParam::Binding(ref binding) => {
write!(f, "{} = {}", binding.ident, pprust::ty_to_string(&*binding.ty))
}
}
impl<'a> Rewrite for SegmentParam<'a> {
// FIXME doesn't always use width, offset
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
Some(match *self {
SegmentParam::LifeTime(ref lt) => {
pprust::lifetime_to_string(lt)
}
SegmentParam::Type(ref ty) => {
try_opt!(ty.rewrite(context, width, offset))
}
SegmentParam::Binding(ref binding) => {
format!("{} = {}", binding.ident, try_opt!(binding.ty.rewrite(context, width, offset)))
}
})
}
}
@ -205,19 +205,22 @@ fn rewrite_segment(segment: &ast::PathSegment,
let list_lo = span_after(codemap::mk_sp(*span_lo, span_hi), "<", context.codemap);
let separator = get_path_separator(context.codemap, *span_lo, list_lo);
// 1 for <
let extra_offset = 1 + separator.len();
// 1 for >
let list_width = try_opt!(width.checked_sub(extra_offset + 1));
let items = itemize_list(context.codemap,
param_list.into_iter(),
">",
|param| param.get_span().lo,
|param| param.get_span().hi,
ToString::to_string,
|seg| {
seg.rewrite(context, list_width, offset + extra_offset).unwrap()
},
list_lo,
span_hi);
// 1 for <
let extra_offset = 1 + separator.len();
// 1 for >
let list_width = try_opt!(width.checked_sub(extra_offset + 1));
let fmt = ListFormatting::for_fn(list_width, offset + extra_offset);
// update pos
@ -346,6 +349,13 @@ impl Rewrite for ast::TyParamBound {
}
}
impl Rewrite for ast::TyParamBounds {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
let strs: Vec<_> = self.iter().map(|b| b.rewrite(context, width, offset).unwrap()).collect();
Some(strs.join(" + "))
}
}
// FIXME: this assumes everything will fit on one line
impl Rewrite for ast::TyParam {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
@ -389,3 +399,20 @@ impl Rewrite for ast::PolyTraitRef {
}
}
}
impl Rewrite for ast::Ty {
// FIXME doesn't always use width, offset
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
match self.node {
ast::TyPath(None, ref p) => {
p.rewrite(context, width, offset)
}
ast::TyObjectSum(ref ty, ref bounds) => {
Some(format!("{} + {}",
try_opt!(ty.rewrite(context, width, offset)),
try_opt!(bounds.rewrite(context, width, offset))))
}
_ => Some(pprust::ty_to_string(self)),
}
}
}

View File

@ -22,7 +22,7 @@ mod other;
}
fn foo() where 'a: 'b, for<'a> D<'b>: 'a {
fn foo()->Box<Write+'static> where 'a: 'b, for<'a> D<'b>: 'a {
hello!()
}

View File

@ -28,7 +28,7 @@ fn foo(a: isize, b: u32 /* blah blah */, c: f64) {
}
fn foo()
fn foo() -> Box<Write + 'static>
where 'a: 'b,
for<'a> D<'b>: 'a
{