1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use std::iter::ExactSizeIterator;
15 use syntax::ast::{self, Mutability, FunctionRetTy};
16 use syntax::codemap::{self, Span, BytePos};
17 use syntax::print::pprust;
18 use syntax::symbol::keywords;
21 use codemap::SpanUtils;
22 use lists::{format_item_list, itemize_list, format_fn_args};
23 use rewrite::{Rewrite, RewriteContext};
24 use utils::{extra_offset, format_mutability, wrap_str};
25 use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
26 use config::TypeDensity;
27 use itertools::Itertools;
29 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
30 pub enum PathContext {
36 // Does not wrap on simple segments.
37 pub fn rewrite_path(context: &RewriteContext,
38 path_context: PathContext,
39 qself: Option<&ast::QSelf>,
43 let skip_count = qself.map_or(0, |x| x.position);
45 let mut result = if path.is_global() && qself.is_none() &&
46 path_context != PathContext::Import {
52 let mut span_lo = path.span.lo;
54 if let Some(qself) = qself {
56 if context.config.spaces_within_angle_brackets {
60 let fmt_ty = try_opt!(qself.ty.rewrite(context, shape));
61 result.push_str(&fmt_ty);
64 result.push_str(" as ");
65 if path.is_global() && path_context != PathContext::Import {
66 result.push_str("::");
69 let extra_offset = extra_offset(&result, shape);
71 let shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(3));
73 result = try_opt!(rewrite_path_segments(PathContext::Type,
75 path.segments.iter().take(skip_count),
82 if context.config.spaces_within_angle_brackets {
86 result.push_str(">::");
87 span_lo = qself.ty.span.hi + BytePos(1);
90 let extra_offset = extra_offset(&result, shape);
91 let shape = try_opt!(shape.shrink_left(extra_offset));
92 rewrite_path_segments(path_context,
94 path.segments.iter().skip(skip_count),
101 fn rewrite_path_segments<'a, I>(path_context: PathContext,
104 mut span_lo: BytePos,
106 context: &RewriteContext,
109 where I: Iterator<Item = &'a ast::PathSegment>
111 let mut first = true;
112 let shape = shape.visual_indent(0);
114 for segment in iter {
115 // Indicates a global path, shouldn't be rendered.
116 if segment.identifier.name == keywords::CrateRoot.name() {
122 buffer.push_str("::");
125 let extra_offset = extra_offset(&buffer, shape);
126 let new_shape = try_opt!(shape.shrink_left(extra_offset));
127 let segment_string = try_opt!(rewrite_segment(path_context,
134 buffer.push_str(&segment_string);
141 enum SegmentParam<'a> {
142 LifeTime(&'a ast::Lifetime),
144 Binding(&'a ast::TypeBinding),
147 impl<'a> SegmentParam<'a> {
148 fn get_span(&self) -> Span {
150 SegmentParam::LifeTime(lt) => lt.span,
151 SegmentParam::Type(ty) => ty.span,
152 SegmentParam::Binding(binding) => binding.span,
157 impl<'a> Rewrite for SegmentParam<'a> {
158 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
160 SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
161 SegmentParam::Type(ty) => ty.rewrite(context, shape),
162 SegmentParam::Binding(binding) => {
163 let mut result = format!("{} = ", binding.ident);
164 let budget = try_opt!(shape.width.checked_sub(result.len()));
165 let rewrite = try_opt!(binding.ty
166 .rewrite(context, Shape::legacy(budget, shape.indent + result.len())));
167 result.push_str(&rewrite);
174 // Formats a path segment. There are some hacks involved to correctly determine
175 // the segment's associated span since it's not part of the AST.
177 // The span_lo is assumed to be greater than the end of any previous segment's
178 // parameters and lesser or equal than the start of current segment.
180 // span_hi is assumed equal to the end of the entire path.
182 // When the segment contains a positive number of parameters, we update span_lo
183 // so that invariants described above will hold for the next segment.
184 fn rewrite_segment(path_context: PathContext,
185 segment: &ast::PathSegment,
186 span_lo: &mut BytePos,
188 context: &RewriteContext,
191 let ident_len = segment.identifier.to_string().len();
192 let shape = try_opt!(shape.shrink_left(ident_len));
194 let params = if let Some(ref params) = segment.parameters {
196 ast::PathParameters::AngleBracketed(ref data) if !data.lifetimes.is_empty() ||
197 !data.types.is_empty() ||
198 !data.bindings.is_empty() => {
199 let param_list = data.lifetimes
201 .map(SegmentParam::LifeTime)
202 .chain(data.types.iter().map(|x| SegmentParam::Type(&*x)))
203 .chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
204 .collect::<Vec<_>>();
206 let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
207 let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
208 let separator = if path_context == PathContext::Expr {
215 let extra_offset = 1 + separator.len();
217 // TODO bad visual indent
218 let list_shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(1)).visual_indent(0);
220 let items = itemize_list(context.codemap,
221 param_list.into_iter(),
223 |param| param.get_span().lo,
224 |param| param.get_span().hi,
225 |seg| seg.rewrite(context, list_shape),
228 let list_str = try_opt!(format_item_list(items, list_shape, context.config));
230 // Update position of last bracket.
231 *span_lo = next_span_lo;
233 if context.config.spaces_within_angle_brackets && list_str.len() > 0 {
234 format!("{}< {} >", separator, list_str)
236 format!("{}<{}>", separator, list_str)
239 ast::PathParameters::Parenthesized(ref data) => {
240 let output = match data.output {
241 Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
242 None => FunctionRetTy::Default(codemap::DUMMY_SP),
244 try_opt!(format_function_type(data.inputs.iter().map(|x| &**x),
257 Some(format!("{}{}", segment.identifier, params))
260 fn format_function_type<'a, I>(inputs: I,
261 output: &FunctionRetTy,
264 context: &RewriteContext,
267 where I: ExactSizeIterator,
268 <I as Iterator>::Item: Deref,
269 <I::Item as Deref>::Target: Rewrite + Spanned + 'a
271 // Code for handling variadics is somewhat duplicated for items, but they
272 // are different enough to need some serious refactoring to share code.
275 <T as Deref>::Target: Rewrite + Spanned
281 let variadic_arg = if variadic {
282 let variadic_start = context.codemap.span_before(span, "...");
283 Some(ArgumentKind::Variadic(variadic_start))
289 let budget = try_opt!(shape.width.checked_sub(2));
291 let offset = shape.indent + 1;
292 let list_lo = context.codemap.span_after(span, "(");
293 let items = itemize_list(context.codemap,
294 // FIXME Would be nice to avoid this allocation,
295 // but I couldn't get the types to work out.
296 inputs.map(|i| ArgumentKind::Regular(Box::new(i)))
297 .chain(variadic_arg),
300 ArgumentKind::Regular(ref ty) => ty.span().lo,
301 ArgumentKind::Variadic(start) => start,
304 ArgumentKind::Regular(ref ty) => ty.span().hi,
305 ArgumentKind::Variadic(start) => start + BytePos(3),
308 ArgumentKind::Regular(ref ty) => {
309 ty.rewrite(context, Shape::legacy(budget, offset))
311 ArgumentKind::Variadic(_) => Some("...".to_owned()),
316 let list_str = try_opt!(format_fn_args(items, Shape::legacy(budget, offset), context.config));
318 let output = match *output {
319 FunctionRetTy::Ty(ref ty) => {
320 let budget = try_opt!(shape.width.checked_sub(4));
321 let type_str = try_opt!(ty.rewrite(context, Shape::legacy(budget, offset + 4)));
322 format!(" -> {}", type_str)
324 FunctionRetTy::Default(..) => String::new(),
327 let infix = if !output.is_empty() && output.len() + list_str.len() > shape.width {
328 format!("\n{}", (offset - 1).to_string(context.config))
333 Some(if context.config.spaces_within_parens {
334 format!("( {} ){}{}", list_str, infix, output)
336 format!("({}){}{}", list_str, infix, output)
340 fn type_bound_colon(context: &RewriteContext) -> &'static str {
341 match (context.config.space_before_bound, context.config.space_after_bound_colon) {
342 (true, true) => " : ",
343 (true, false) => " :",
344 (false, true) => ": ",
345 (false, false) => ":",
349 impl Rewrite for ast::WherePredicate {
350 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
352 let result = match *self {
353 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { ref bound_lifetimes,
357 let type_str = try_opt!(bounded_ty.rewrite(context, shape));
359 let colon = type_bound_colon(context);
361 if !bound_lifetimes.is_empty() {
362 let lifetime_str: String = try_opt!(bound_lifetimes.iter()
364 lt.rewrite(context, shape)
366 .intersperse(Some(", ".to_string()))
369 // 6 = "for<> ".len()
370 let used_width = lifetime_str.len() + type_str.len() + colon.len() + 6;
371 let budget = try_opt!(shape.width.checked_sub(used_width));
372 let bounds_str: String = try_opt!(bounds.iter()
374 ty_bound.rewrite(context,
375 Shape::legacy(budget,
376 shape.indent + used_width))
378 .intersperse(Some(" + ".to_string()))
381 if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
382 format!("for< {} > {}{}{}", lifetime_str, type_str, colon, bounds_str)
384 format!("for<{}> {}{}{}", lifetime_str, type_str, colon, bounds_str)
387 let used_width = type_str.len() + colon.len();
388 let budget = try_opt!(shape.width.checked_sub(used_width));
389 let bounds_str: String = try_opt!(bounds.iter()
391 ty_bound.rewrite(context,
392 Shape::legacy(budget,
393 shape.indent + used_width))
395 .intersperse(Some(" + ".to_string()))
398 format!("{}{}{}", type_str, colon, bounds_str)
401 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { ref lifetime,
404 try_opt!(rewrite_bounded_lifetime(lifetime, bounds.iter(), context, shape))
406 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate { ref lhs_ty,
409 let lhs_ty_str = try_opt!(lhs_ty.rewrite(context, shape));
411 let used_width = 3 + lhs_ty_str.len();
412 let budget = try_opt!(shape.width.checked_sub(used_width));
413 let rhs_ty_str = try_opt!(rhs_ty.rewrite(context,
414 Shape::legacy(budget, shape.indent + used_width)));
415 format!("{} = {}", lhs_ty_str, rhs_ty_str)
419 wrap_str(result, context.config.max_width, shape)
423 impl Rewrite for ast::LifetimeDef {
424 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
425 rewrite_bounded_lifetime(&self.lifetime, self.bounds.iter(), context, shape)
429 fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime,
431 context: &RewriteContext,
434 where I: ExactSizeIterator<Item = &'b ast::Lifetime>
436 let result = try_opt!(lt.rewrite(context, shape));
438 if bounds.len() == 0 {
441 let appendix: Vec<_> = try_opt!(bounds.into_iter()
442 .map(|b| b.rewrite(context, shape))
444 let colon = type_bound_colon(context);
445 let result = format!("{}{}{}", result, colon, appendix.join(" + "));
446 wrap_str(result, context.config.max_width, shape)
450 impl Rewrite for ast::TyParamBound {
451 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
453 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
454 tref.rewrite(context, shape)
456 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => {
457 let budget = try_opt!(shape.width.checked_sub(1));
459 try_opt!(tref.rewrite(context,
460 Shape::legacy(budget, shape.indent + 1)))))
462 ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape),
467 impl Rewrite for ast::Lifetime {
468 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
469 wrap_str(pprust::lifetime_to_string(self),
470 context.config.max_width,
475 impl Rewrite for ast::TyParamBounds {
476 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
477 let joiner = match context.config.type_punctuation_density {
478 TypeDensity::Compressed => "+",
479 TypeDensity::Wide => " + ",
481 let strs: Vec<_> = try_opt!(self.iter()
482 .map(|b| b.rewrite(context, shape))
484 wrap_str(strs.join(joiner), context.config.max_width, shape)
488 impl Rewrite for ast::TyParam {
489 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
490 let mut result = String::with_capacity(128);
491 result.push_str(&self.ident.to_string());
492 if !self.bounds.is_empty() {
493 if context.config.space_before_bound {
494 result.push_str(" ");
496 result.push_str(":");
497 if context.config.space_after_bound_colon {
498 result.push_str(" ");
501 let bounds: String = try_opt!(self.bounds
503 .map(|ty_bound| ty_bound.rewrite(context, shape))
504 .intersperse(Some(" + ".to_string()))
507 result.push_str(&bounds);
509 if let Some(ref def) = self.default {
511 let eq_str = match context.config.type_punctuation_density {
512 TypeDensity::Compressed => "=",
513 TypeDensity::Wide => " = ",
515 result.push_str(eq_str);
516 let budget = try_opt!(shape.width.checked_sub(result.len()));
518 try_opt!(def.rewrite(context, Shape::legacy(budget, shape.indent + result.len())));
519 result.push_str(&rewrite);
522 wrap_str(result, context.config.max_width, shape)
526 impl Rewrite for ast::PolyTraitRef {
527 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
528 if !self.bound_lifetimes.is_empty() {
529 let lifetime_str: String = try_opt!(self.bound_lifetimes
531 .map(|lt| lt.rewrite(context, shape))
532 .intersperse(Some(", ".to_string()))
535 // 6 is "for<> ".len()
536 let extra_offset = lifetime_str.len() + 6;
537 let max_path_width = try_opt!(shape.width.checked_sub(extra_offset));
538 let path_str = try_opt!(self.trait_ref
540 Shape::legacy(max_path_width, shape.indent + extra_offset)));
542 Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
543 format!("for< {} > {}", lifetime_str, path_str)
545 format!("for<{}> {}", lifetime_str, path_str)
548 self.trait_ref.rewrite(context, shape)
553 impl Rewrite for ast::TraitRef {
554 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
555 rewrite_path(context, PathContext::Type, None, &self.path, shape)
559 impl Rewrite for ast::Ty {
560 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
562 ast::TyKind::TraitObject(ref bounds) => bounds.rewrite(context, shape),
563 ast::TyKind::Ptr(ref mt) => {
564 let prefix = match mt.mutbl {
565 Mutability::Mutable => "*mut ",
566 Mutability::Immutable => "*const ",
569 rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
571 ast::TyKind::Rptr(ref lifetime, ref mt) => {
572 let mut_str = format_mutability(mt.mutbl);
573 let mut_len = mut_str.len();
574 Some(match *lifetime {
575 Some(ref lifetime) => {
576 let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
577 let lt_str = try_opt!(lifetime.rewrite(context,
578 Shape::legacy(lt_budget,
581 let lt_len = lt_str.len();
582 let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));
588 Shape::legacy(budget,
589 shape.indent + 2 + mut_len + lt_len))))
592 let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
595 try_opt!(mt.ty.rewrite(context,
596 Shape::legacy(budget,
597 shape.indent + 1 + mut_len))))
601 // FIXME: we drop any comments here, even though it's a silly place to put
603 ast::TyKind::Paren(ref ty) => {
604 let budget = try_opt!(shape.width.checked_sub(2));
605 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
606 .map(|ty_str| if context.config.spaces_within_parens {
607 format!("( {} )", ty_str)
609 format!("({})", ty_str)
612 ast::TyKind::Slice(ref ty) => {
613 let budget = if context.config.spaces_within_square_brackets {
614 try_opt!(shape.width.checked_sub(4))
616 try_opt!(shape.width.checked_sub(2))
618 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
619 .map(|ty_str| if context.config.spaces_within_square_brackets {
620 format!("[ {} ]", ty_str)
622 format!("[{}]", ty_str)
625 ast::TyKind::Tup(ref items) => {
626 rewrite_tuple(context, items.iter().map(|x| &**x), self.span, shape)
628 ast::TyKind::Path(ref q_self, ref path) => {
629 rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
631 ast::TyKind::Array(ref ty, ref repeats) => {
632 let use_spaces = context.config.spaces_within_square_brackets;
633 let lbr = if use_spaces { "[ " } else { "[" };
634 let rbr = if use_spaces { " ]" } else { "]" };
635 rewrite_pair(&**ty, &**repeats, lbr, "; ", rbr, context, shape)
637 ast::TyKind::Infer => {
638 if shape.width >= 1 {
644 ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
645 ast::TyKind::Never => Some(String::from("!")),
646 ast::TyKind::Mac(..) => None,
647 ast::TyKind::ImplicitSelf => Some(String::from("")),
648 ast::TyKind::ImplTrait(ref it) => {
649 it.rewrite(context, shape).map(|it_str| format!("impl {}", it_str))
651 ast::TyKind::Typeof(..) => unreachable!(),
656 fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
658 context: &RewriteContext,
661 let mut result = String::with_capacity(128);
663 if !bare_fn.lifetimes.is_empty() {
664 result.push_str("for<");
665 // 6 = "for<> ".len(), 4 = "for<".
666 // This doesn't work out so nicely for mutliline situation with lots of
667 // rightward drift. If that is a problem, we could use the list stuff.
668 result.push_str(&try_opt!(bare_fn.lifetimes
672 Shape::legacy(try_opt!(shape.width.checked_sub(6)), shape.indent + 4))
674 .intersperse(Some(", ".to_string()))
675 .collect::<Option<String>>()));
676 result.push_str("> ");
679 result.push_str(::utils::format_unsafety(bare_fn.unsafety));
681 if bare_fn.abi != abi::Abi::Rust {
682 result.push_str(&::utils::format_abi(bare_fn.abi, context.config.force_explicit_abi));
685 result.push_str("fn");
687 let budget = try_opt!(shape.width.checked_sub(result.len()));
688 let indent = shape.indent + result.len();
690 let rewrite = try_opt!(format_function_type(bare_fn.decl.inputs.iter(),
691 &bare_fn.decl.output,
692 bare_fn.decl.variadic,
695 Shape::legacy(budget, indent)));
697 result.push_str(&rewrite);