rust/src/expr.rs

389 lines
15 KiB
Rust
Raw Normal View History

2015-04-21 21:01:19 +12:00
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2015-06-16 17:29:05 +02:00
use rewrite::{Rewrite, RewriteContext};
2015-06-24 01:11:29 +02:00
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic};
2015-06-23 15:58:58 +02:00
use string::{StringFormat, rewrite_string};
use StructLitStyle;
use utils::{span_after, make_indent};
2015-07-13 21:51:56 +02:00
use visitor::FmtVisitor;
2015-04-21 21:01:19 +12:00
use syntax::{ast, ptr};
2015-07-13 21:51:56 +02:00
use syntax::codemap::{Pos, Span, BytePos, mk_sp};
2015-05-25 19:11:53 +12:00
use syntax::parse::token;
use syntax::print::pprust;
2015-07-13 21:51:56 +02:00
use syntax::visit::Visitor;
2015-04-21 21:01:19 +12:00
2015-06-16 17:29:05 +02:00
impl Rewrite for ast::Expr {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String> {
match self.node {
ast::Expr_::ExprLit(ref l) => {
match l.node {
ast::Lit_::LitStr(ref is, _) => {
rewrite_string_lit(context, &is, l.span, width, offset)
2015-06-16 17:29:05 +02:00
}
_ => context.codemap.span_to_snippet(self.span).ok()
2015-06-16 17:29:05 +02:00
}
}
ast::Expr_::ExprCall(ref callee, ref args) => {
rewrite_call(context, callee, args, self.span, width, offset)
2015-06-16 17:29:05 +02:00
}
ast::Expr_::ExprParen(ref subexpr) => {
rewrite_paren(context, subexpr, width, offset)
}
ast::Expr_::ExprBinary(ref op, ref lhs, ref rhs) => {
rewrite_binary_op(context, op, lhs, rhs, width, offset)
}
ast::Expr_::ExprUnary(ref op, ref subexpr) => {
rewrite_unary_op(context, op, subexpr, width, offset)
2015-06-16 17:29:05 +02:00
}
ast::Expr_::ExprStruct(ref path, ref fields, ref base) => {
rewrite_struct_lit(context,
path,
fields,
base.as_ref().map(|e| &**e),
self.span,
width,
offset)
2015-06-16 17:29:05 +02:00
}
ast::Expr_::ExprTup(ref items) => {
rewrite_tuple_lit(context, items, self.span, width, offset)
2015-06-16 17:29:05 +02:00
}
2015-07-13 21:51:56 +02:00
ast::Expr_::ExprLoop(ref block, _) => {
// FIXME: this drops any comment between "loop" and the block.
// TODO: format label
block.rewrite(context, width, offset).map(|result| {
format!("loop {}", result)
})
}
_ => context.codemap.span_to_snippet(self.span).ok()
2015-04-21 21:01:19 +12:00
}
2015-06-16 17:29:05 +02:00
}
}
2015-04-21 21:01:19 +12:00
2015-07-13 21:51:56 +02:00
impl Rewrite for ast::Block {
fn rewrite(&self, context: &RewriteContext, _: usize, _: usize) -> Option<String> {
let mut visitor = FmtVisitor::from_codemap(context.codemap, context.config);
visitor.last_pos = self.span.lo;
visitor.block_indent = context.block_indent;
visitor.visit_block(self);
// Push text between last block item and end of block
let snippet = visitor.snippet(mk_sp(visitor.last_pos, self.span.hi));
visitor.changes.push_str_span(self.span, &snippet);
// Stringify visitor
let file_name = context.codemap.span_to_filename(self.span);
let string_buffer = visitor.changes.get(&file_name);
Some(string_buffer.to_string())
}
}
2015-06-23 15:58:58 +02:00
fn rewrite_string_lit(context: &RewriteContext,
s: &str,
span: Span,
width: usize,
offset: usize)
2015-07-03 11:13:28 +02:00
-> Option<String> {
2015-06-16 17:29:05 +02:00
// 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 {
2015-06-16 17:29:05 +02:00
return context.codemap.span_to_snippet(span).ok();
}
2015-07-16 13:31:20 +12:00
let fmt = StringFormat {
opener: "\"",
closer: "\"",
line_start: " ",
line_end: "\\",
width: width,
offset: offset,
trim_end: false,
};
2015-04-21 21:01:19 +12:00
2015-06-23 15:58:58 +02:00
Some(rewrite_string(&s.escape_default(), &fmt))
2015-06-16 17:29:05 +02:00
}
fn rewrite_call(context: &RewriteContext,
callee: &ast::Expr,
args: &[ptr::P<ast::Expr>],
2015-06-23 15:58:58 +02:00
span: Span,
width: usize,
offset: usize)
2015-07-03 11:13:28 +02:00
-> Option<String> {
2015-06-16 17:29:05 +02:00
debug!("rewrite_call, width: {}, offset: {}", width, offset);
2015-04-21 21:01:19 +12:00
2015-06-16 17:29:05 +02:00
// 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));
2015-06-23 15:58:58 +02:00
debug!("rewrite_call, callee_str: `{}`", callee_str);
if args.len() == 0 {
return Some(format!("{}()", callee_str));
}
2015-06-16 17:29:05 +02:00
// 2 is for parens.
let remaining_width = try_opt!(width.checked_sub(callee_str.len() + 2));
2015-06-16 17:29:05 +02:00
let offset = callee_str.len() + 1 + offset;
2015-04-21 21:01:19 +12:00
2015-06-23 15:58:58 +02:00
let items = itemize_list(context.codemap,
Vec::new(),
args.iter(),
",",
")",
|item| item.span.lo,
|item| item.span.hi,
2015-06-24 01:11:29 +02:00
// Take old span when rewrite fails.
2015-06-23 15:58:58 +02:00
|item| item.rewrite(context, remaining_width, offset)
2015-06-24 01:11:29 +02:00
.unwrap_or(context.codemap.span_to_snippet(item.span)
.unwrap()),
2015-06-23 15:58:58 +02:00
callee.span.hi + BytePos(1),
span.hi);
2015-07-16 13:31:20 +12:00
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,
};
2015-04-21 21:01:19 +12:00
2015-06-23 15:58:58 +02:00
Some(format!("{}({})", callee_str, write_list(&items, &fmt)))
2015-06-16 17:29:05 +02:00
}
2015-04-21 21:01:19 +12:00
2015-07-03 11:13:28 +02:00
fn rewrite_paren(context: &RewriteContext,
subexpr: &ast::Expr,
width: usize,
offset: usize)
-> Option<String> {
2015-06-16 17:29:05 +02:00
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))
}
2015-05-24 19:57:13 +02:00
2015-06-24 01:11:29 +02:00
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)
2015-07-03 11:13:28 +02:00
-> Option<String> {
2015-06-16 17:29:05 +02:00
debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
assert!(fields.len() > 0 || base.is_some());
2015-05-25 19:11:53 +12:00
2015-06-24 01:11:29 +02:00
enum StructLitField<'a> {
Regular(&'a ast::Field),
2015-07-03 11:13:28 +02:00
Base(&'a ast::Expr),
2015-06-24 01:11:29 +02:00
}
2015-06-16 17:29:05 +02:00
let path_str = pprust::path_to_string(path);
2015-07-16 14:03:52 +12:00
let (indent, h_budget, v_budget) = match context.config.struct_lit_style {
StructLitStyle::VisualIndent => {
2015-07-16 14:03:52 +12:00
// Foo { a: Foo } - indent is +3, width is -5.
let budget = width - (path_str.len() + 5);
(offset + path_str.len() + 3, budget, budget)
}
StructLitStyle::BlockIndent => {
2015-07-16 14:03:52 +12:00
// If we are all on one line, then we'll ignore the indent, and we
// have a smaller budget.
let indent = context.block_indent + context.config.tab_spaces;
2015-07-16 14:03:52 +12:00
(indent, width - (path_str.len() + 5), width - indent)
}
};
2015-05-25 19:11:53 +12:00
2015-06-24 01:11:29 +02:00
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) => {
2015-07-16 14:03:52 +12:00
rewrite_field(context, &field, h_budget, indent)
2015-06-24 01:11:29 +02:00
.unwrap_or(context.codemap.span_to_snippet(field.span)
.unwrap())
},
StructLitField::Base(ref expr) => {
// 2 = ..
2015-07-16 14:03:52 +12:00
expr.rewrite(context, h_budget - 2, indent + 2)
2015-06-24 01:11:29 +02:00
.map(|s| format!("..{}", s))
.unwrap_or(context.codemap.span_to_snippet(expr.span)
.unwrap())
}
}
},
span_after(span, "{", context.codemap),
span.hi);
2015-07-16 13:31:20 +12:00
let fmt = ListFormatting {
tactic: ListTactic::HorizontalVertical,
separator: ",",
trailing_separator: if base.is_some() {
2015-06-16 17:29:05 +02:00
SeparatorTactic::Never
} else {
context.config.struct_lit_trailing_comma
2015-06-16 17:29:05 +02:00
},
2015-07-16 13:31:20 +12:00
indent: indent,
2015-07-16 14:03:52 +12:00
h_width: h_budget,
v_width: v_budget,
2015-07-16 13:31:20 +12:00
ends_with_newline: true,
};
2015-06-24 01:11:29 +02:00
let fields_str = write_list(&items, &fmt);
2015-05-25 19:11:53 +12:00
match context.config.struct_lit_style {
StructLitStyle::BlockIndent if fields_str.contains('\n') => {
let inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
let outer_indent = make_indent(context.block_indent);
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
}
_ => Some(format!("{} {{ {} }}", path_str, fields_str)),
}
// FIXME if context.config.struct_lit_style == VisualIndent, but we run out
// of space, we should fall back to BlockIndent.
2015-06-16 17:29:05 +02:00
}
2015-05-25 19:11:53 +12:00
2015-07-03 11:13:28 +02:00
fn rewrite_field(context: &RewriteContext,
field: &ast::Field,
width: usize,
offset: usize)
-> Option<String> {
2015-06-16 17:29:05 +02:00
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))
}
2015-05-25 19:11:53 +12:00
fn rewrite_tuple_lit(context: &RewriteContext,
items: &[ptr::P<ast::Expr>],
2015-06-23 15:58:58 +02:00
span: Span,
width: usize,
offset: usize)
-> Option<String> {
debug!("rewrite_tuple_lit: width: {}, offset: {}", width, offset);
2015-06-23 15:58:58 +02:00
let indent = offset + 1;
// In case of length 1, need a trailing comma
if items.len() == 1 {
// 3 = "(" + ",)"
return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
}
2015-06-23 15:58:58 +02:00
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 - 1,
2015-06-23 15:58:58 +02:00
indent)
2015-06-24 01:11:29 +02:00
.unwrap_or(context.codemap.span_to_snippet(item.span)
.unwrap()),
2015-06-23 15:58:58 +02:00
span.lo + BytePos(1), // Remove parens
span.hi - BytePos(1));
2015-07-16 13:31:20 +12:00
let fmt = ListFormatting {
tactic: ListTactic::HorizontalVertical,
separator: ",",
trailing_separator: SeparatorTactic::Never,
indent: indent,
h_width: width - 2,
v_width: width - 2,
ends_with_newline: true,
};
2015-06-23 15:58:58 +02:00
Some(format!("({})", write_list(&items, &fmt)))
}
fn rewrite_binary_op(context: &RewriteContext,
op: &ast::BinOp,
lhs: &ast::Expr,
rhs: &ast::Expr,
width: usize,
offset: usize)
-> Option<String> {
// FIXME: format comments between operands and operator
let operator_str = context.codemap.span_to_snippet(op.span).unwrap();
// 1 = space between lhs expr and operator
2015-07-03 00:50:55 +02:00
let mut result =
2015-07-16 14:23:48 +12:00
try_opt!(lhs.rewrite(context,
context.config.max_width - offset - 1 - operator_str.len(),
offset));
result.push(' ');
result.push_str(&operator_str);
let remaining_width = match result.rfind('\n') {
2015-07-03 00:50:55 +02:00
Some(idx) => (offset + width + idx).checked_sub(result.len()).unwrap_or(0),
None => width.checked_sub(result.len()).unwrap_or(0)
};
// Get "full width" rhs and see if it fits on the current line. This
// usually works fairly well since it tends to place operands of
// operations with high precendence close together.
let rhs_result = try_opt!(rhs.rewrite(context, width, offset));
2015-07-03 00:50:55 +02:00
// Second condition is needed in case of line break not caused by a
// shortage of space, but by end-of-line comments, for example.
if rhs_result.len() > remaining_width || rhs_result.contains('\n') {
result.push('\n');
result.push_str(&make_indent(offset));
} else {
result.push(' ');
};
result.push_str(&rhs_result);
Some(result)
}
fn rewrite_unary_op(context: &RewriteContext,
op: &ast::UnOp,
expr: &ast::Expr,
width: usize,
offset: usize)
-> Option<String> {
// For some reason, an UnOp is not spanned like BinOp!
let operator_str = match *op {
ast::UnOp::UnUniq => "&",
ast::UnOp::UnDeref => "*",
ast::UnOp::UnNot => "!",
ast::UnOp::UnNeg => "-"
};
Some(format!("{}{}", operator_str, try_opt!(expr.rewrite(context, width - 1, offset))))
}