Format more type variants
This commit is contained in:
parent
00aa232834
commit
01937061a9
42
src/expr.rs
42
src/expr.rs
@ -161,10 +161,10 @@ impl Rewrite for ast::Expr {
|
||||
wrap_str("return".to_owned(), context.config.max_width, width, offset)
|
||||
}
|
||||
ast::Expr_::ExprRet(Some(ref expr)) => {
|
||||
rewrite_unary_prefix(context, "return ", expr, width, offset)
|
||||
rewrite_unary_prefix(context, "return ", &**expr, width, offset)
|
||||
}
|
||||
ast::Expr_::ExprBox(ref expr) => {
|
||||
rewrite_unary_prefix(context, "box ", expr, width, offset)
|
||||
rewrite_unary_prefix(context, "box ", &**expr, width, offset)
|
||||
}
|
||||
ast::Expr_::ExprAddrOf(mutability, ref expr) => {
|
||||
rewrite_expr_addrof(context, mutability, expr, width, offset)
|
||||
@ -210,15 +210,15 @@ impl Rewrite for ast::Expr {
|
||||
}
|
||||
}
|
||||
|
||||
fn rewrite_pair<LHS, RHS>(lhs: &LHS,
|
||||
rhs: &RHS,
|
||||
prefix: &str,
|
||||
infix: &str,
|
||||
suffix: &str,
|
||||
context: &RewriteContext,
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
-> Option<String>
|
||||
pub fn rewrite_pair<LHS, RHS>(lhs: &LHS,
|
||||
rhs: &RHS,
|
||||
prefix: &str,
|
||||
infix: &str,
|
||||
suffix: &str,
|
||||
context: &RewriteContext,
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
-> Option<String>
|
||||
where LHS: Rewrite,
|
||||
RHS: Rewrite
|
||||
{
|
||||
@ -1470,16 +1470,16 @@ fn rewrite_binary_op(context: &RewriteContext,
|
||||
rhs_result))
|
||||
}
|
||||
|
||||
fn rewrite_unary_prefix(context: &RewriteContext,
|
||||
prefix: &str,
|
||||
expr: &ast::Expr,
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
expr.rewrite(context,
|
||||
try_opt!(width.checked_sub(prefix.len())),
|
||||
offset + prefix.len())
|
||||
.map(|r| format!("{}{}", prefix, r))
|
||||
pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,
|
||||
prefix: &str,
|
||||
rewrite: &R,
|
||||
width: usize,
|
||||
offset: Indent)
|
||||
-> Option<String> {
|
||||
rewrite.rewrite(context,
|
||||
try_opt!(width.checked_sub(prefix.len())),
|
||||
offset + prefix.len())
|
||||
.map(|r| format!("{}{}", prefix, r))
|
||||
}
|
||||
|
||||
fn rewrite_unary_op(context: &RewriteContext,
|
||||
|
@ -522,7 +522,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
|
||||
let variadic_arg = if variadic {
|
||||
let variadic_span = codemap::mk_sp(args.last().unwrap().ty.span.hi, span.hi);
|
||||
let variadic_start = span_after(variadic_span, "...", self.codemap) - BytePos(1);
|
||||
let variadic_start = span_after(variadic_span, "...", self.codemap) - BytePos(3);
|
||||
Some(ArgumentKind::Variadic(variadic_start))
|
||||
} else {
|
||||
None
|
||||
|
120
src/types.rs
120
src/types.rs
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::ast::{self, Mutability};
|
||||
use syntax::print::pprust;
|
||||
use syntax::codemap::{self, Span, BytePos, CodeMap};
|
||||
|
||||
@ -16,6 +16,7 @@ use Indent;
|
||||
use lists::{format_item_list, itemize_list, format_fn_args, list_helper, ListTactic};
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use utils::{extra_offset, span_after, format_mutability, wrap_str};
|
||||
use expr::{rewrite_unary_prefix, rewrite_pair};
|
||||
|
||||
impl Rewrite for ast::Path {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
@ -129,21 +130,25 @@ impl<'a> SegmentParam<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Rewrite for SegmentParam<'a> {
|
||||
// FIXME: doesn't always use width, offset.
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
Some(match *self {
|
||||
match *self {
|
||||
SegmentParam::LifeTime(ref lt) => {
|
||||
pprust::lifetime_to_string(lt)
|
||||
wrap_str(pprust::lifetime_to_string(lt),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
SegmentParam::Type(ref ty) => {
|
||||
try_opt!(ty.rewrite(context, width, offset))
|
||||
ty.rewrite(context, width, offset)
|
||||
}
|
||||
SegmentParam::Binding(ref binding) => {
|
||||
format!("{} = {}",
|
||||
binding.ident,
|
||||
try_opt!(binding.ty.rewrite(context, width, offset)))
|
||||
let mut result = format!("{} = ", binding.ident);
|
||||
let budget = try_opt!(width.checked_sub(result.len()));
|
||||
let rewrite = try_opt!(binding.ty.rewrite(context, budget, offset + result.len()));
|
||||
result.push_str(&rewrite);
|
||||
Some(result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,9 +168,7 @@ fn get_path_separator(codemap: &CodeMap,
|
||||
for c in snippet.chars().rev() {
|
||||
if c == ':' {
|
||||
return "::";
|
||||
} else if c.is_whitespace() || c == '<' {
|
||||
continue;
|
||||
} else {
|
||||
} else if !c.is_whitespace() && c != '<' {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@ -271,8 +274,7 @@ fn rewrite_segment(segment: &ast::PathSegment,
|
||||
impl Rewrite for ast::WherePredicate {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
// TODO: dead spans?
|
||||
// TODO: don't assume we'll always fit on one line...
|
||||
Some(match *self {
|
||||
let result = match *self {
|
||||
ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { ref bound_lifetimes,
|
||||
ref bounded_ty,
|
||||
ref bounds,
|
||||
@ -335,23 +337,27 @@ impl Rewrite for ast::WherePredicate {
|
||||
let path_str = try_opt!(path.rewrite(context, budget, offset + used_width));
|
||||
format!("{} = {}", path_str, ty_str)
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
wrap_str(result, context.config.max_width, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
impl Rewrite for ast::LifetimeDef {
|
||||
fn rewrite(&self, _: &RewriteContext, _: usize, _: Indent) -> Option<String> {
|
||||
if self.bounds.is_empty() {
|
||||
Some(pprust::lifetime_to_string(&self.lifetime))
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
let result = if self.bounds.is_empty() {
|
||||
pprust::lifetime_to_string(&self.lifetime)
|
||||
} else {
|
||||
Some(format!("{}: {}",
|
||||
pprust::lifetime_to_string(&self.lifetime),
|
||||
self.bounds
|
||||
.iter()
|
||||
.map(pprust::lifetime_to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" + ")))
|
||||
}
|
||||
format!("{}: {}",
|
||||
pprust::lifetime_to_string(&self.lifetime),
|
||||
self.bounds
|
||||
.iter()
|
||||
.map(pprust::lifetime_to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" + "))
|
||||
};
|
||||
|
||||
wrap_str(result, context.config.max_width, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
@ -366,7 +372,10 @@ impl Rewrite for ast::TyParamBound {
|
||||
Some(format!("?{}", try_opt!(tref.rewrite(context, budget, offset + 1))))
|
||||
}
|
||||
ast::TyParamBound::RegionTyParamBound(ref l) => {
|
||||
Some(pprust::lifetime_to_string(l))
|
||||
wrap_str(pprust::lifetime_to_string(l),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -377,11 +386,10 @@ impl Rewrite for ast::TyParamBounds {
|
||||
let strs: Vec<_> = try_opt!(self.iter()
|
||||
.map(|b| b.rewrite(context, width, offset))
|
||||
.collect());
|
||||
Some(strs.join(" + "))
|
||||
wrap_str(strs.join(" + "), context.config.max_width, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this assumes everything will fit on one line
|
||||
impl Rewrite for ast::TyParam {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
let mut result = String::with_capacity(128);
|
||||
@ -404,11 +412,10 @@ impl Rewrite for ast::TyParam {
|
||||
result.push_str(&rewrite);
|
||||
}
|
||||
|
||||
Some(result)
|
||||
wrap_str(result, context.config.max_width, width, offset)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this assumes everything will fit on one line
|
||||
impl Rewrite for ast::PolyTraitRef {
|
||||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
|
||||
if !self.bound_lifetimes.is_empty() {
|
||||
@ -432,12 +439,8 @@ 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: Indent) -> Option<String> {
|
||||
match self.node {
|
||||
ast::TyPath(None, ref p) => {
|
||||
p.rewrite(context, width, offset)
|
||||
}
|
||||
ast::TyObjectSum(ref ty, ref bounds) => {
|
||||
let ty_str = try_opt!(ty.rewrite(context, width, offset));
|
||||
let overhead = ty_str.len() + 3;
|
||||
@ -447,6 +450,14 @@ impl Rewrite for ast::Ty {
|
||||
try_opt!(width.checked_sub(overhead)),
|
||||
offset + overhead))))
|
||||
}
|
||||
ast::TyPtr(ref mt) => {
|
||||
let prefix = match mt.mutbl {
|
||||
Mutability::MutMutable => "*mut ",
|
||||
Mutability::MutImmutable => "*const ",
|
||||
};
|
||||
|
||||
rewrite_unary_prefix(context, prefix, &*mt.ty, width, offset)
|
||||
}
|
||||
ast::TyRptr(ref lifetime, ref mt) => {
|
||||
let mut_str = format_mutability(mt.mutbl);
|
||||
let mut_len = mut_str.len();
|
||||
@ -470,37 +481,60 @@ impl Rewrite for ast::Ty {
|
||||
}
|
||||
})
|
||||
}
|
||||
// FIXME: we drop any comments here, even though it's a silly place to put
|
||||
// comments.
|
||||
ast::TyParen(ref ty) => {
|
||||
let budget = try_opt!(width.checked_sub(2));
|
||||
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("({})", ty_str))
|
||||
}
|
||||
ast::TyTup(ref tup_ret) => {
|
||||
ast::TyVec(ref ty) => {
|
||||
let budget = try_opt!(width.checked_sub(2));
|
||||
ty.rewrite(context, budget, offset + 1).map(|ty_str| format!("[{}]", ty_str))
|
||||
}
|
||||
ast::TyTup(ref tup_ret) => {
|
||||
if tup_ret.is_empty() {
|
||||
Some("()".to_string())
|
||||
Some("()".to_owned())
|
||||
} else if let [ref item] = &**tup_ret {
|
||||
let budget = try_opt!(width.checked_sub(3));
|
||||
let inner = try_opt!(item.rewrite(context, budget, offset + 1));
|
||||
let ret = format!("({},)", inner);
|
||||
wrap_str(ret, context.config.max_width, budget, offset + 1)
|
||||
} else {
|
||||
let budget = try_opt!(width.checked_sub(2));
|
||||
let items = itemize_list(context.codemap,
|
||||
tup_ret.iter(),
|
||||
")",
|
||||
|item| item.span.lo,
|
||||
|item| item.span.hi,
|
||||
|item| item.rewrite(context, budget, offset + 1),
|
||||
tup_ret[0].span.lo,
|
||||
span_after(self.span, "(", context.codemap),
|
||||
self.span.hi);
|
||||
|
||||
|
||||
list_helper(items, budget, offset + 1, context.config, ListTactic::Mixed)
|
||||
.map(|s| format!("({})", s))
|
||||
}
|
||||
}
|
||||
_ => wrap_str(pprust::ty_to_string(self),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset),
|
||||
ast::TyPolyTraitRef(ref trait_ref) => trait_ref.rewrite(context, width, offset),
|
||||
ast::TyPath(ref q_self, ref path) => {
|
||||
rewrite_path(context, q_self.as_ref(), path, width, offset)
|
||||
}
|
||||
ast::TyFixedLengthVec(ref ty, ref repeats) => {
|
||||
rewrite_pair(&**ty, &**repeats, "[", "; ", "]", context, width, offset)
|
||||
}
|
||||
ast::TyInfer => {
|
||||
if width >= 1 {
|
||||
Some("_".to_owned())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
ast::TyBareFn(..) => {
|
||||
wrap_str(pprust::ty_to_string(self),
|
||||
context.config.max_width,
|
||||
width,
|
||||
offset)
|
||||
}
|
||||
ast::TyMac(..) | ast::TyTypeof(..) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,8 +32,9 @@ pub fn extra_offset(text: &str, offset: Indent) -> usize {
|
||||
#[inline]
|
||||
pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
|
||||
let snippet = codemap.span_to_snippet(original).unwrap();
|
||||
let offset = snippet.find_uncommented(needle).unwrap() + needle.len();
|
||||
|
||||
original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
|
||||
original.lo + BytePos(offset as u32)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
5
tests/source/type.rs
Normal file
5
tests/source/type.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn types() {
|
||||
let x: [ Vec < _ > ] = [];
|
||||
let y: * mut [ SomeType ; konst_funk() ] = expr();
|
||||
let z: (/*#digits*/ usize, /*exp*/ i16) = funk();
|
||||
}
|
5
tests/target/type.rs
Normal file
5
tests/target/type.rs
Normal file
@ -0,0 +1,5 @@
|
||||
fn types() {
|
||||
let x: [Vec<_>] = [];
|
||||
let y: *mut [SomeType; konst_funk()] = expr();
|
||||
let z: (/* #digits */ usize, /* exp */ i16) = funk();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user