Reduce dependency on pprust

This commit is contained in:
Marcus Klaas 2015-12-25 20:59:46 +01:00
parent 954dd0869e
commit 2069abcca4
3 changed files with 57 additions and 47 deletions

View File

@ -27,7 +27,6 @@ use config::BlockIndentStyle;
use syntax::{ast, ptr};
use syntax::codemap::{mk_sp, Span};
use syntax::print::pprust;
pub fn rewrite_chain(mut expr: &ast::Expr,
context: &RewriteContext,
@ -233,7 +232,9 @@ fn rewrite_method_call(method_name: ast::Ident,
let (lo, type_str) = if types.is_empty() {
(args[0].span.hi, String::new())
} else {
let type_list = types.iter().map(|ty| pprust::ty_to_string(ty)).collect::<Vec<_>>();
let type_list: Vec<_> = try_opt!(types.iter()
.map(|ty| ty.rewrite(context, width, offset))
.collect());
(types.last().unwrap().span.hi,
format!("::<{}>", type_list.join(", ")))

View File

@ -23,7 +23,6 @@ use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, Struct
use syntax::{ast, abi};
use syntax::codemap::{Span, BytePos, mk_sp};
use syntax::print::pprust;
use syntax::parse::token;
// Statements of the form
@ -901,31 +900,36 @@ impl Rewrite for ast::FunctionRetTy {
impl Rewrite for ast::Arg {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
if is_named_arg(self) {
if let ast::Ty_::TyInfer = self.ty.node {
wrap_str(pprust::pat_to_string(&self.pat),
context.config.max_width,
width,
offset)
} else {
let mut result = pprust::pat_to_string(&self.pat);
let mut result = try_opt!(self.pat.rewrite(context, width, offset));
if self.ty.node != ast::Ty_::TyInfer {
result.push_str(": ");
let max_width = try_opt!(width.checked_sub(result.len()));
let ty_str = try_opt!(self.ty.rewrite(context, max_width, offset + result.len()));
result.push_str(&ty_str);
Some(result)
}
Some(result)
} else {
self.ty.rewrite(context, width, offset)
}
}
}
fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf, args: &[ast::Arg]) -> Option<String> {
fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
args: &[ast::Arg],
context: &RewriteContext)
-> Option<String> {
match explicit_self.node {
ast::ExplicitSelf_::SelfRegion(lt, m, _) => {
let mut_str = format_mutability(m);
match lt {
Some(ref l) => Some(format!("&{} {}self", pprust::lifetime_to_string(l), mut_str)),
Some(ref l) => {
let lifetime_str = try_opt!(l.rewrite(context,
usize::max_value(),
Indent::empty()));
Some(format!("&{} {}self", lifetime_str, mut_str))
}
None => Some(format!("&{}self", mut_str)),
}
}
@ -933,10 +937,9 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf, args: &[ast::Arg]) -
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
let mutability = explicit_self_mutability(&args[0]);
let type_str = try_opt!(ty.rewrite(context, usize::max_value(), Indent::empty()));
Some(format!("{}self: {}",
format_mutability(mutability),
pprust::ty_to_string(ty)))
Some(format!("{}self: {}", format_mutability(mutability), type_str))
}
ast::ExplicitSelf_::SelfValue(_) => {
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
@ -1237,7 +1240,7 @@ fn rewrite_args(context: &RewriteContext,
// FIXME: the comment for the self argument is dropped. This is blocked
// on rust issue #27522.
let min_args = explicit_self.and_then(|explicit_self| {
rewrite_explicit_self(explicit_self, args)
rewrite_explicit_self(explicit_self, args, context)
})
.map(|self_str| {
arg_item_strs[0] = self_str;

View File

@ -135,12 +135,7 @@ impl<'a> SegmentParam<'a> {
impl<'a> Rewrite for SegmentParam<'a> {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
match *self {
SegmentParam::LifeTime(ref lt) => {
wrap_str(pprust::lifetime_to_string(lt),
context.config.max_width,
width,
offset)
}
SegmentParam::LifeTime(ref lt) => lt.rewrite(context, width, offset),
SegmentParam::Type(ref ty) => ty.rewrite(context, width, offset),
SegmentParam::Binding(ref binding) => {
let mut result = format!("{} = ", binding.ident);
@ -332,12 +327,7 @@ impl Rewrite for ast::WherePredicate {
ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { ref lifetime,
ref bounds,
.. }) => {
format!("{}: {}",
pprust::lifetime_to_string(lifetime),
bounds.iter()
.map(pprust::lifetime_to_string)
.collect::<Vec<_>>()
.join(" + "))
try_opt!(rewrite_bounded_lifetime(lifetime, bounds.iter(), context, width, offset))
}
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref path, ref ty, .. }) => {
let ty_str = try_opt!(ty.rewrite(context, width, offset));
@ -360,18 +350,27 @@ impl Rewrite for ast::WherePredicate {
impl Rewrite for ast::LifetimeDef {
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 {
format!("{}: {}",
pprust::lifetime_to_string(&self.lifetime),
self.bounds
.iter()
.map(pprust::lifetime_to_string)
.collect::<Vec<_>>()
.join(" + "))
};
rewrite_bounded_lifetime(&self.lifetime, self.bounds.iter(), context, width, offset)
}
}
fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime,
bounds: I,
context: &RewriteContext,
width: usize,
offset: Indent)
-> Option<String>
where I: ExactSizeIterator<Item = &'b ast::Lifetime>
{
let result = try_opt!(lt.rewrite(context, width, offset));
if bounds.len() == 0 {
Some(result)
} else {
let appendix: Vec<_> = try_opt!(bounds.into_iter()
.map(|b| b.rewrite(context, width, offset))
.collect());
let result = format!("{}: {}", result, appendix.join(" + "));
wrap_str(result, context.config.max_width, width, offset)
}
}
@ -386,16 +385,20 @@ impl Rewrite for ast::TyParamBound {
let budget = try_opt!(width.checked_sub(1));
Some(format!("?{}", try_opt!(tref.rewrite(context, budget, offset + 1))))
}
ast::TyParamBound::RegionTyParamBound(ref l) => {
wrap_str(pprust::lifetime_to_string(l),
context.config.max_width,
width,
offset)
}
ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, width, offset),
}
}
}
impl Rewrite for ast::Lifetime {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
wrap_str(pprust::lifetime_to_string(self),
context.config.max_width,
width,
offset)
}
}
impl Rewrite for ast::TyParamBounds {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
let strs: Vec<_> = try_opt!(self.iter()
@ -483,7 +486,10 @@ impl Rewrite for ast::Ty {
let mut_len = mut_str.len();
Some(match *lifetime {
Some(ref lifetime) => {
let lt_str = pprust::lifetime_to_string(lifetime);
let lt_budget = try_opt!(width.checked_sub(2 + mut_len));
let lt_str = try_opt!(lifetime.rewrite(context,
lt_budget,
offset + 2 + mut_len));
let lt_len = lt_str.len();
let budget = try_opt!(width.checked_sub(2 + mut_len + lt_len));
format!("&{} {}{}",