Struct literals
This commit is contained in:
parent
972f494e4e
commit
46818d405a
@ -24,8 +24,8 @@ fn main() {
|
||||
let mut def_config = String::new();
|
||||
def_config_file.read_to_string(&mut def_config).unwrap();
|
||||
|
||||
run(args, WriteMode::Display, &def_config);
|
||||
//run(args, WriteMode::Overwrite, &def_config);
|
||||
//run(args, WriteMode::Display, &def_config);
|
||||
run(args, WriteMode::Overwrite, &def_config);
|
||||
|
||||
std::env::set_exit_status(0);
|
||||
|
||||
|
@ -20,6 +20,7 @@ pub struct Config {
|
||||
pub fn_brace_style: ::BraceStyle,
|
||||
pub fn_return_indent: ::ReturnIndent,
|
||||
pub struct_trailing_comma: bool,
|
||||
pub struct_lit_trailing_comma: ::lists::SeparatorTactic,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
@ -6,3 +6,4 @@ newline_style = "Unix"
|
||||
fn_brace_style = "SameLineWhere"
|
||||
fn_return_indent = "WithArgs"
|
||||
struct_trailing_comma = true
|
||||
struct_lit_trailing_comma = "Vertical"
|
||||
|
69
src/expr.rs
69
src/expr.rs
@ -13,7 +13,9 @@ use utils::*;
|
||||
use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
|
||||
|
||||
use syntax::{ast, ptr};
|
||||
use syntax::codemap::{Span, Pos};
|
||||
use syntax::codemap::{Pos, Span};
|
||||
use syntax::parse::token;
|
||||
use syntax::print::pprust;
|
||||
|
||||
use MIN_STRING;
|
||||
|
||||
@ -134,6 +136,64 @@ impl<'a> FmtVisitor<'a> {
|
||||
format!("({})", subexpr_str)
|
||||
}
|
||||
|
||||
fn rewrite_struct_lit(&mut self,
|
||||
path: &ast::Path,
|
||||
fields: &[ast::Field],
|
||||
base: Option<&ast::Expr>,
|
||||
width: usize,
|
||||
offset: usize)
|
||||
-> String
|
||||
{
|
||||
debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
|
||||
assert!(fields.len() > 0 || base.is_some());
|
||||
|
||||
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 mut field_strs: Vec<_> =
|
||||
fields.iter().map(|f| self.rewrite_field(f, budget, indent)).collect();
|
||||
if let Some(expr) = base {
|
||||
// Another 2 on the width/indent for the ..
|
||||
field_strs.push(format!("..{}", self.rewrite_expr(expr, budget - 2, indent + 2)))
|
||||
}
|
||||
|
||||
// FIXME comments
|
||||
let field_strs: Vec<_> = field_strs.into_iter().map(|s| (s, String::new())).collect();
|
||||
let tactics = if field_strs.iter().any(|&(ref s, _)| s.contains('\n')) {
|
||||
ListTactic::Vertical
|
||||
} else {
|
||||
ListTactic::HorizontalVertical
|
||||
};
|
||||
let fmt = ListFormatting {
|
||||
tactic: tactics,
|
||||
separator: ",",
|
||||
trailing_separator: if base.is_some() {
|
||||
SeparatorTactic::Never
|
||||
} else {
|
||||
config!(struct_lit_trailing_comma)
|
||||
},
|
||||
indent: indent,
|
||||
h_width: budget,
|
||||
v_width: budget,
|
||||
};
|
||||
let fields_str = write_list(&field_strs, &fmt);
|
||||
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(&mut self, field: &ast::Field, width: usize, offset: usize) -> String {
|
||||
let name = &token::get_ident(field.ident.node);
|
||||
let overhead = name.len() + 2;
|
||||
let expr = self.rewrite_expr(&field.expr, width - overhead, offset + overhead);
|
||||
format!("{}: {}", name, expr)
|
||||
}
|
||||
|
||||
pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String {
|
||||
match expr.node {
|
||||
ast::Expr_::ExprLit(ref l) => {
|
||||
@ -152,6 +212,13 @@ impl<'a> FmtVisitor<'a> {
|
||||
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);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,7 @@ impl<'a> FmtVisitor<'a> {
|
||||
path: &ast::Path,
|
||||
path_list: &[ast::PathListItem],
|
||||
visibility: ast::Visibility) -> String {
|
||||
let path_str = pprust::path_to_string(&path);
|
||||
let path_str = pprust::path_to_string(path);
|
||||
|
||||
let vis = match visibility {
|
||||
ast::Public => "pub ",
|
||||
|
14
src/lists.rs
14
src/lists.rs
@ -9,6 +9,7 @@
|
||||
// except according to those terms.
|
||||
|
||||
use utils::make_indent;
|
||||
use rustc_serialize::{Decodable, Decoder};
|
||||
|
||||
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
||||
pub enum ListTactic {
|
||||
@ -29,6 +30,19 @@ pub enum SeparatorTactic {
|
||||
Vertical,
|
||||
}
|
||||
|
||||
// TODO could use a macro for all these Decodable impls.
|
||||
impl Decodable for SeparatorTactic {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
||||
let s = try!(d.read_str());
|
||||
match &*s {
|
||||
"Always" => Ok(SeparatorTactic::Always),
|
||||
"Never" => Ok(SeparatorTactic::Never),
|
||||
"Vertical" => Ok(SeparatorTactic::Vertical),
|
||||
_ => Err(d.error("Bad variant")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO having some helpful ctors for ListFormatting would be nice.
|
||||
pub struct ListFormatting<'a> {
|
||||
pub tactic: ListTactic,
|
||||
|
24
tests/idem/struct_lits.rs
Normal file
24
tests/idem/struct_lits.rs
Normal file
@ -0,0 +1,24 @@
|
||||
// Struct literal expressions.
|
||||
|
||||
fn main() {
|
||||
let x = Bar;
|
||||
|
||||
// Comment
|
||||
let y = Foo { a: x };
|
||||
|
||||
Foo { a: Bar, b: foo() };
|
||||
|
||||
Foo { a: foo(), b: bar(), ..something };
|
||||
|
||||
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(), b: bar() };
|
||||
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(),
|
||||
b: bar(), };
|
||||
|
||||
Fooooooooooooooooooooooooooooooooooooooooooooooooooooo { a: foo(),
|
||||
b: bar(),
|
||||
c: bar(),
|
||||
d: bar(),
|
||||
e: bar(),
|
||||
f: bar(),
|
||||
..baz() };
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user