A basic impl of Rewrite for ast::Expr
This commit is contained in:
parent
c10bfecdc2
commit
c012d311c4
4 changed files with 233 additions and 189 deletions
174
src/expr.rs
174
src/expr.rs
|
@ -8,9 +8,9 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use visitor::FmtVisitor;
|
|
||||||
use utils::*;
|
use utils::*;
|
||||||
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
|
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
|
||||||
|
use rewrite::{Rewrite, RewriteContext};
|
||||||
|
|
||||||
use syntax::{ast, ptr};
|
use syntax::{ast, ptr};
|
||||||
use syntax::codemap::{Pos, Span};
|
use syntax::codemap::{Pos, Span};
|
||||||
|
@ -19,16 +19,51 @@ use syntax::print::pprust;
|
||||||
|
|
||||||
use MIN_STRING;
|
use MIN_STRING;
|
||||||
|
|
||||||
impl<'a> FmtVisitor<'a> {
|
impl Rewrite for ast::Expr {
|
||||||
fn rewrite_string_lit(&mut self, s: &str, span: Span, width: usize, offset: usize) -> String {
|
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, _) => {
|
||||||
|
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, 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),
|
||||||
|
width,
|
||||||
|
offset);
|
||||||
|
}
|
||||||
|
ast::Expr_::ExprTup(ref items) => {
|
||||||
|
return rewrite_tuple_lit(context, items, 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<String> {
|
||||||
// FIXME I bet this stomps unicode escapes in the source string
|
// FIXME I bet this stomps unicode escapes in the source string
|
||||||
|
|
||||||
// Check if there is anything to fix: we always try to fixup multi-line
|
// 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.
|
// strings, or if the string is too long for the line.
|
||||||
let l_loc = self.codemap.lookup_char_pos(span.lo);
|
let l_loc = context.codemap.lookup_char_pos(span.lo);
|
||||||
let r_loc = self.codemap.lookup_char_pos(span.hi);
|
let r_loc = context.codemap.lookup_char_pos(span.hi);
|
||||||
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= config!(max_width) {
|
if l_loc.line == r_loc.line && r_loc.col.to_usize() <= config!(max_width) {
|
||||||
return self.snippet(span);
|
return context.codemap.span_to_snippet(span).ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO if lo.col > IDEAL - 10, start a new line (need cur indent for that)
|
// TODO if lo.col > IDEAL - 10, start a new line (need cur indent for that)
|
||||||
|
@ -86,30 +121,37 @@ impl<'a> FmtVisitor<'a> {
|
||||||
}
|
}
|
||||||
result.push('"');
|
result.push('"');
|
||||||
|
|
||||||
result
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_call(&mut self,
|
fn rewrite_call(context: &RewriteContext,
|
||||||
callee: &ast::Expr,
|
callee: &ast::Expr,
|
||||||
args: &[ptr::P<ast::Expr>],
|
args: &[ptr::P<ast::Expr>],
|
||||||
width: usize,
|
width: usize,
|
||||||
offset: usize)
|
offset: usize)
|
||||||
-> String
|
-> Option<String>
|
||||||
{
|
{
|
||||||
debug!("rewrite_call, width: {}, offset: {}", width, offset);
|
debug!("rewrite_call, width: {}, offset: {}", width, offset);
|
||||||
|
|
||||||
// TODO using byte lens instead of char lens (and probably all over the place too)
|
// TODO using byte lens instead of char lens (and probably all over the place too)
|
||||||
let callee_str = self.rewrite_expr(callee, width, offset);
|
let callee_str = match callee.rewrite(context, width, offset) {
|
||||||
debug!("rewrite_call, callee_str: `{}`", callee_str);
|
Some(s) => s,
|
||||||
|
None => { return None; }
|
||||||
|
};
|
||||||
|
debug!("rewrite_call, callee_str: `{:?}`", callee_str);
|
||||||
// 2 is for parens.
|
// 2 is for parens.
|
||||||
let remaining_width = width - callee_str.len() - 2;
|
let remaining_width = width - callee_str.len() - 2;
|
||||||
let offset = callee_str.len() + 1 + offset;
|
let offset = callee_str.len() + 1 + offset;
|
||||||
let arg_count = args.len();
|
let arg_count = args.len();
|
||||||
|
|
||||||
let args_str = if arg_count > 0 {
|
let args_str = if arg_count > 0 {
|
||||||
let args: Vec<_> = args.iter().map(|e| (self.rewrite_expr(e,
|
let mut args_rewritten = Vec::with_capacity(args.len());
|
||||||
remaining_width,
|
for arg in args.iter() {
|
||||||
offset), String::new())).collect();
|
match arg.rewrite(context, remaining_width, offset) {
|
||||||
|
Some(s) => { args_rewritten.push((s, String::new())); }
|
||||||
|
None => { return None; }
|
||||||
|
}
|
||||||
|
}
|
||||||
let fmt = ListFormatting {
|
let fmt = ListFormatting {
|
||||||
tactic: ListTactic::HorizontalVertical,
|
tactic: ListTactic::HorizontalVertical,
|
||||||
separator: ",",
|
separator: ",",
|
||||||
|
@ -118,30 +160,30 @@ impl<'a> FmtVisitor<'a> {
|
||||||
h_width: remaining_width,
|
h_width: remaining_width,
|
||||||
v_width: remaining_width,
|
v_width: remaining_width,
|
||||||
};
|
};
|
||||||
write_list(&args, &fmt)
|
write_list(&args_rewritten, &fmt)
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
format!("{}({})", callee_str, args_str)
|
Some(format!("{}({})", callee_str, args_str))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String {
|
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, width: usize, offset: usize) -> Option<String> {
|
||||||
debug!("rewrite_paren, width: {}, offset: {}", width, offset);
|
debug!("rewrite_paren, width: {}, offset: {}", width, offset);
|
||||||
// 1 is for opening paren, 2 is for opening+closing, we want to keep the closing
|
// 1 is for opening paren, 2 is for opening+closing, we want to keep the closing
|
||||||
// paren on the same line as the subexpr
|
// paren on the same line as the subexpr
|
||||||
let subexpr_str = self.rewrite_expr(subexpr, width-2, offset+1);
|
let subexpr_str = subexpr.rewrite(context, width-2, offset+1);
|
||||||
debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str);
|
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
|
||||||
format!("({})", subexpr_str)
|
subexpr_str.map(|s| format!("({})", s))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_struct_lit(&mut self,
|
fn rewrite_struct_lit(context: &RewriteContext,
|
||||||
path: &ast::Path,
|
path: &ast::Path,
|
||||||
fields: &[ast::Field],
|
fields: &[ast::Field],
|
||||||
base: Option<&ast::Expr>,
|
base: Option<&ast::Expr>,
|
||||||
width: usize,
|
width: usize,
|
||||||
offset: usize)
|
offset: usize)
|
||||||
-> String
|
-> Option<String>
|
||||||
{
|
{
|
||||||
debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
|
debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
|
||||||
assert!(fields.len() > 0 || base.is_some());
|
assert!(fields.len() > 0 || base.is_some());
|
||||||
|
@ -151,11 +193,19 @@ impl<'a> FmtVisitor<'a> {
|
||||||
let indent = offset + path_str.len() + 3;
|
let indent = offset + path_str.len() + 3;
|
||||||
let budget = width - (path_str.len() + 5);
|
let budget = width - (path_str.len() + 5);
|
||||||
|
|
||||||
let mut field_strs: Vec<_> =
|
let mut field_strs = Vec::with_capacity(fields.len());
|
||||||
fields.iter().map(|f| self.rewrite_field(f, budget, indent)).collect();
|
for field in fields.iter() {
|
||||||
|
match rewrite_field(context, field, budget, indent) {
|
||||||
|
Some(s) => { field_strs.push(s); }
|
||||||
|
None => { return None; }
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(expr) = base {
|
if let Some(expr) = base {
|
||||||
// Another 2 on the width/indent for the ..
|
// Another 2 on the width/indent for the ..
|
||||||
field_strs.push(format!("..{}", self.rewrite_expr(expr, budget - 2, indent + 2)))
|
field_strs.push(match expr.rewrite(context, budget - 2, indent + 2) {
|
||||||
|
Some(s) => format!("..{}", s),
|
||||||
|
None => { return None; }
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME comments
|
// FIXME comments
|
||||||
|
@ -173,7 +223,7 @@ impl<'a> FmtVisitor<'a> {
|
||||||
v_width: budget,
|
v_width: budget,
|
||||||
};
|
};
|
||||||
let fields_str = write_list(&field_strs, &fmt);
|
let fields_str = write_list(&field_strs, &fmt);
|
||||||
format!("{} {{ {} }}", path_str, fields_str)
|
Some(format!("{} {{ {} }}", path_str, fields_str))
|
||||||
|
|
||||||
// FIXME if the usual multi-line layout is too wide, we should fall back to
|
// FIXME if the usual multi-line layout is too wide, we should fall back to
|
||||||
// Foo {
|
// Foo {
|
||||||
|
@ -181,32 +231,34 @@ impl<'a> FmtVisitor<'a> {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_field(&mut self, field: &ast::Field, width: usize, offset: usize) -> String {
|
fn rewrite_field(context: &RewriteContext, field: &ast::Field, width: usize, offset: usize) -> Option<String> {
|
||||||
let name = &token::get_ident(field.ident.node);
|
let name = &token::get_ident(field.ident.node);
|
||||||
let overhead = name.len() + 2;
|
let overhead = name.len() + 2;
|
||||||
let expr = self.rewrite_expr(&field.expr, width - overhead, offset + overhead);
|
let expr = field.expr.rewrite(context, width - overhead, offset + overhead);
|
||||||
format!("{}: {}", name, expr)
|
expr.map(|s| format!("{}: {}", name, s))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rewrite_tuple_lit(&mut self, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
|
fn rewrite_tuple_lit(context: &RewriteContext, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
|
||||||
-> String {
|
-> Option<String> {
|
||||||
// opening paren
|
// opening paren
|
||||||
let indent = offset + 1;
|
let indent = offset + 1;
|
||||||
// In case of length 1, need a trailing comma
|
// In case of length 1, need a trailing comma
|
||||||
if items.len() == 1 {
|
if items.len() == 1 {
|
||||||
return format!("({},)", self.rewrite_expr(&*items[0], width - 3, indent));
|
return items[0].rewrite(context, width - 3, indent).map(|s| format!("({},)", s));
|
||||||
}
|
}
|
||||||
// Only last line has width-1 as budget, other may take max_width
|
// Only last line has width-1 as budget, other may take max_width
|
||||||
let item_strs: Vec<_> =
|
let mut item_strs = Vec::with_capacity(items.len());
|
||||||
items.iter()
|
for (i, item) in items.iter().enumerate() {
|
||||||
.enumerate()
|
let rem_width = if i == items.len() - 1 {
|
||||||
.map(|(i, item)| self.rewrite_expr(
|
width - 2
|
||||||
item,
|
} else {
|
||||||
// last line : given width (minus "("+")"), other lines : max_width
|
config!(max_width) - indent - 2
|
||||||
// (minus "("+","))
|
};
|
||||||
if i == items.len() - 1 { width - 2 } else { config!(max_width) - indent - 2 },
|
match item.rewrite(context, rem_width, indent) {
|
||||||
indent))
|
Some(s) => { item_strs.push(s); }
|
||||||
.collect();
|
None => {return None; }
|
||||||
|
}
|
||||||
|
}
|
||||||
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
|
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
|
||||||
ListTactic::Vertical
|
ListTactic::Vertical
|
||||||
} else {
|
} else {
|
||||||
|
@ -223,41 +275,5 @@ impl<'a> FmtVisitor<'a> {
|
||||||
v_width: width - 2,
|
v_width: width - 2,
|
||||||
};
|
};
|
||||||
let item_str = write_list(&item_strs, &fmt);
|
let item_str = write_list(&item_strs, &fmt);
|
||||||
format!("({})", item_str)
|
Some(format!("({})", item_str))
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
|
|
||||||
match expr.node {
|
|
||||||
ast::Expr_::ExprLit(ref l) => {
|
|
||||||
match l.node {
|
|
||||||
ast::Lit_::LitStr(ref is, _) => {
|
|
||||||
let result = self.rewrite_string_lit(&is, l.span, width, offset);
|
|
||||||
debug!("string lit: `{}`", result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ast::Expr_::ExprCall(ref callee, ref args) => {
|
|
||||||
return self.rewrite_call(callee, args, width, offset);
|
|
||||||
}
|
|
||||||
ast::Expr_::ExprParen(ref subexpr) => {
|
|
||||||
return self.rewrite_paren(subexpr, width, offset);
|
|
||||||
}
|
|
||||||
ast::Expr_::ExprStruct(ref path, ref fields, ref base) => {
|
|
||||||
return self.rewrite_struct_lit(path,
|
|
||||||
fields,
|
|
||||||
base.as_ref().map(|e| &**e),
|
|
||||||
width,
|
|
||||||
offset);
|
|
||||||
}
|
|
||||||
ast::Expr_::ExprTup(ref items) => {
|
|
||||||
return self.rewrite_tuple_lit(items, width, offset);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.snippet(expr.span)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,7 @@ mod types;
|
||||||
mod expr;
|
mod expr;
|
||||||
mod imports;
|
mod imports;
|
||||||
mod issues;
|
mod issues;
|
||||||
|
mod rewrite;
|
||||||
|
|
||||||
const MIN_STRING: usize = 10;
|
const MIN_STRING: usize = 10;
|
||||||
// When we get scoped annotations, we should have rustfmt::skip.
|
// When we get scoped annotations, we should have rustfmt::skip.
|
||||||
|
|
19
src/rewrite.rs
Normal file
19
src/rewrite.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
use syntax::codemap::CodeMap;
|
||||||
|
|
||||||
|
pub trait Rewrite {
|
||||||
|
fn rewrite(&self, context: &RewriteContext, width: usize, offset: usize) -> Option<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RewriteContext<'a> {
|
||||||
|
pub codemap: &'a CodeMap,
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ use utils;
|
||||||
|
|
||||||
use SKIP_ANNOTATION;
|
use SKIP_ANNOTATION;
|
||||||
use changes::ChangeSet;
|
use changes::ChangeSet;
|
||||||
|
use rewrite::{Rewrite, RewriteContext};
|
||||||
|
|
||||||
pub struct FmtVisitor<'a> {
|
pub struct FmtVisitor<'a> {
|
||||||
pub codemap: &'a CodeMap,
|
pub codemap: &'a CodeMap,
|
||||||
|
@ -32,10 +33,17 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
|
||||||
self.codemap.lookup_char_pos(ex.span.hi));
|
self.codemap.lookup_char_pos(ex.span.hi));
|
||||||
self.format_missing(ex.span.lo);
|
self.format_missing(ex.span.lo);
|
||||||
let offset = self.changes.cur_offset_span(ex.span);
|
let offset = self.changes.cur_offset_span(ex.span);
|
||||||
let new_str = self.rewrite_expr(ex, config!(max_width) - offset, offset);
|
match ex.rewrite(&RewriteContext { codemap: self.codemap },
|
||||||
|
config!(max_width) - offset,
|
||||||
|
offset) {
|
||||||
|
Some(new_str) => {
|
||||||
|
//let new_str = self.rewrite_expr(ex, config!(max_width) - offset, offset);
|
||||||
self.changes.push_str_span(ex.span, &new_str);
|
self.changes.push_str_span(ex.span, &new_str);
|
||||||
self.last_pos = ex.span.hi;
|
self.last_pos = ex.span.hi;
|
||||||
}
|
}
|
||||||
|
None => { self.last_pos = ex.span.lo; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
|
fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
|
||||||
// If the stmt is actually an item, then we'll handle any missing spans
|
// If the stmt is actually an item, then we'll handle any missing spans
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue