// Copyright 2015 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use rewrite::{Rewrite, RewriteContext}; use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic}; use string::{StringFormat, rewrite_string}; use utils::span_after; use syntax::{ast, ptr}; use syntax::codemap::{Pos, Span, BytePos}; use syntax::parse::token; use syntax::print::pprust; impl Rewrite for ast::Expr { fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option { match self.node { ast::Expr_::ExprLit(ref l) => { match l.node { ast::Lit_::LitStr(ref is, _) => { let result = rewrite_string_lit(context, &is, l.span, width, offset); debug!("string lit: `{:?}`", result); return result; } _ => {} } } ast::Expr_::ExprCall(ref callee, ref args) => { return rewrite_call(context, callee, args, self.span, width, offset); } ast::Expr_::ExprParen(ref subexpr) => { return rewrite_paren(context, subexpr, width, offset); } ast::Expr_::ExprStruct(ref path, ref fields, ref base) => { return rewrite_struct_lit(context, path, fields, base.as_ref().map(|e| &**e), self.span, width, offset); } ast::Expr_::ExprTup(ref items) => { return rewrite_tuple_lit(context, items, self.span, width, offset); } _ => {} } context.codemap.span_to_snippet(self.span).ok() } } fn rewrite_string_lit(context: &RewriteContext, s: &str, span: Span, width: usize, offset: usize) -> Option { // Check if there is anything to fix: we always try to fixup multi-line // strings, or if the string is too long for the line. let l_loc = context.codemap.lookup_char_pos(span.lo); let r_loc = context.codemap.lookup_char_pos(span.hi); if l_loc.line == r_loc.line && r_loc.col.to_usize() <= context.config.max_width { return context.codemap.span_to_snippet(span).ok(); } let fmt = StringFormat { opener: "\"", closer: "\"", line_start: " ", line_end: "\\", width: width, offset: offset, trim_end: false }; Some(rewrite_string(&s.escape_default(), &fmt)) } fn rewrite_call(context: &RewriteContext, callee: &ast::Expr, args: &[ptr::P], span: Span, width: usize, offset: usize) -> Option { debug!("rewrite_call, width: {}, offset: {}", width, offset); // TODO using byte lens instead of char lens (and probably all over the place too) let callee_str = try_opt!(callee.rewrite(context, width, offset)); debug!("rewrite_call, callee_str: `{}`", callee_str); if args.len() == 0 { return Some(format!("{}()", callee_str)); } // 2 is for parens. let remaining_width = width - callee_str.len() - 2; let offset = callee_str.len() + 1 + offset; let items = itemize_list(context.codemap, Vec::new(), args.iter(), ",", ")", |item| item.span.lo, |item| item.span.hi, // Take old span when rewrite fails. |item| item.rewrite(context, remaining_width, offset) .unwrap_or(context.codemap.span_to_snippet(item.span) .unwrap()), callee.span.hi + BytePos(1), span.hi); let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical, separator: ",", trailing_separator: SeparatorTactic::Never, indent: offset, h_width: remaining_width, v_width: remaining_width, ends_with_newline: true, }; Some(format!("{}({})", callee_str, write_list(&items, &fmt))) } fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, width: usize, offset: usize) -> Option { debug!("rewrite_paren, width: {}, offset: {}", width, offset); // 1 is for opening paren, 2 is for opening+closing, we want to keep the closing // paren on the same line as the subexpr let subexpr_str = subexpr.rewrite(context, width-2, offset+1); debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str); subexpr_str.map(|s| format!("({})", s)) } fn rewrite_struct_lit<'a>(context: &RewriteContext, path: &ast::Path, fields: &'a [ast::Field], base: Option<&'a ast::Expr>, span: Span, width: usize, offset: usize) -> Option { debug!("rewrite_struct_lit: width {}, offset {}", width, offset); assert!(fields.len() > 0 || base.is_some()); enum StructLitField<'a> { Regular(&'a ast::Field), Base(&'a ast::Expr) } let path_str = pprust::path_to_string(path); // Foo { a: Foo } - indent is +3, width is -5. let indent = offset + path_str.len() + 3; let budget = width - (path_str.len() + 5); let field_iter = fields.into_iter().map(StructLitField::Regular) .chain(base.into_iter().map(StructLitField::Base)); let items = itemize_list(context.codemap, Vec::new(), field_iter, ",", "}", |item| { match *item { StructLitField::Regular(ref field) => field.span.lo, // 2 = .. StructLitField::Base(ref expr) => expr.span.lo - BytePos(2) } }, |item| { match *item { StructLitField::Regular(ref field) => field.span.hi, StructLitField::Base(ref expr) => expr.span.hi } }, |item| { match *item { StructLitField::Regular(ref field) => { rewrite_field(context, &field, budget, indent) .unwrap_or(context.codemap.span_to_snippet(field.span) .unwrap()) }, StructLitField::Base(ref expr) => { // 2 = .. expr.rewrite(context, budget - 2, indent + 2) .map(|s| format!("..{}", s)) .unwrap_or(context.codemap.span_to_snippet(expr.span) .unwrap()) } } }, span_after(span, "{", context.codemap), span.hi); let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical, separator: ",", trailing_separator: if base.is_some() { SeparatorTactic::Never } else { context.config.struct_lit_trailing_comma }, indent: indent, h_width: budget, v_width: budget, ends_with_newline: true, }; let fields_str = write_list(&items, &fmt); Some(format!("{} {{ {} }}", path_str, fields_str)) // FIXME if the usual multi-line layout is too wide, we should fall back to // Foo { // a: ..., // } } fn rewrite_field(context: &RewriteContext, field: &ast::Field, width: usize, offset: usize) -> Option { let name = &token::get_ident(field.ident.node); let overhead = name.len() + 2; let expr = field.expr.rewrite(context, width - overhead, offset + overhead); expr.map(|s| format!("{}: {}", name, s)) } fn rewrite_tuple_lit(context: &RewriteContext, items: &[ptr::P], span: Span, width: usize, offset: usize) -> Option { let indent = offset + 1; let items = itemize_list(context.codemap, Vec::new(), items.into_iter(), ",", ")", |item| item.span.lo, |item| item.span.hi, |item| item.rewrite(context, context.config.max_width - indent - 2, indent) .unwrap_or(context.codemap.span_to_snippet(item.span) .unwrap()), span.lo + BytePos(1), // Remove parens span.hi - BytePos(1)); // In case of length 1, need a trailing comma let trailing_separator_tactic = if items.len() == 1 { SeparatorTactic::Always } else { SeparatorTactic::Never }; let fmt = ListFormatting { tactic: ListTactic::HorizontalVertical, separator: ",", trailing_separator: trailing_separator_tactic, indent: indent, h_width: width - 2, v_width: width - 2, ends_with_newline: true, }; Some(format!("({})", write_list(&items, &fmt))) }