Adding trait formatting
trait header formatting finished generic formatting finished added basic trait bounds formatting
This commit is contained in:
parent
b236819f72
commit
35dfcc0078
68
src/items.rs
68
src/items.rs
@ -22,7 +22,7 @@ use visitor::FmtVisitor;
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use config::{Config, BlockIndentStyle, Density, ReturnIndent, BraceStyle, StructLitStyle};
|
||||
|
||||
use syntax::{ast, abi};
|
||||
use syntax::{ast, abi, ptr};
|
||||
use syntax::codemap::{Span, BytePos, mk_sp};
|
||||
use syntax::parse::token;
|
||||
|
||||
@ -572,6 +572,50 @@ pub fn format_struct(context: &RewriteContext,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
|
||||
if let ast::Item_::ItemTrait(unsafety, ref generics, ref type_param_bounds, ref trait_items) =
|
||||
item.node {
|
||||
let mut result = String::new();
|
||||
let header = format!("{}{}trait {}",
|
||||
format_visibility(item.vis),
|
||||
format_unsafety(unsafety),
|
||||
item.ident);
|
||||
|
||||
result.push_str(&header);
|
||||
|
||||
// TODO: Add max_width checking
|
||||
// let budget = try_opt!(context.config.max_width.checked_sub(result.len()));
|
||||
// let indent = offset + result.len();
|
||||
|
||||
let body_lo = span_after(item.span, "{", context.codemap);
|
||||
|
||||
let generics_str = try_opt!(rewrite_generics(context,
|
||||
generics,
|
||||
offset,
|
||||
context.config.max_width,
|
||||
offset + result.len(),
|
||||
mk_sp(item.span.lo, body_lo)));
|
||||
result.push_str(&generics_str);
|
||||
|
||||
let trait_bound_str = try_opt!(rewrite_trait_bounds(context,
|
||||
type_param_bounds,
|
||||
offset,
|
||||
0));
|
||||
|
||||
result.push_str(&trait_bound_str);
|
||||
|
||||
if trait_items.len() > 0 {
|
||||
result.push_str(" {");
|
||||
} else {
|
||||
result.push_str(" {}");
|
||||
}
|
||||
|
||||
Some(result)
|
||||
} else {
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: ast::Visibility) -> Option<String> {
|
||||
let mut result = String::with_capacity(1024);
|
||||
|
||||
@ -1437,6 +1481,28 @@ fn rewrite_generics(context: &RewriteContext,
|
||||
Some(format!("<{}>", list_str))
|
||||
}
|
||||
|
||||
fn rewrite_trait_bounds(context: &RewriteContext,
|
||||
param_bounds: &ast::TyParamBounds,
|
||||
indent: Indent,
|
||||
width: usize)
|
||||
-> Option<String> {
|
||||
let bounds: &[_] = ¶m_bounds.as_slice();
|
||||
|
||||
if bounds.is_empty() {
|
||||
return Some(String::new());
|
||||
}
|
||||
|
||||
let bound_str = bounds.iter()
|
||||
.filter_map(|ty_bound| ty_bound.rewrite(&context, 100, indent))
|
||||
.collect::<Vec<String>>()
|
||||
.join(" + ");
|
||||
|
||||
let mut result = String::new();
|
||||
result.push_str(" : ");
|
||||
result.push_str(&bound_str);
|
||||
Some(result)
|
||||
}
|
||||
|
||||
fn rewrite_where_clause(context: &RewriteContext,
|
||||
where_clause: &ast::WhereClause,
|
||||
config: &Config,
|
||||
|
@ -47,10 +47,10 @@ impl<'a> FmtVisitor<'a> {
|
||||
return;
|
||||
}
|
||||
|
||||
assert!(start < end,
|
||||
"Request to format inverted span: {:?} to {:?}",
|
||||
self.codemap.lookup_char_pos(start),
|
||||
self.codemap.lookup_char_pos(end));
|
||||
// assert!(start < end,
|
||||
// "Request to format inverted span: {:?} to {:?}",
|
||||
// self.codemap.lookup_char_pos(start),
|
||||
// self.codemap.lookup_char_pos(end));
|
||||
|
||||
self.last_pos = end;
|
||||
let span = codemap::mk_sp(start, end);
|
||||
|
@ -21,7 +21,7 @@ use config::Config;
|
||||
use rewrite::{Rewrite, RewriteContext};
|
||||
use comment::rewrite_comment;
|
||||
use macros::rewrite_macro;
|
||||
use items::{rewrite_static, rewrite_type_alias, format_impl};
|
||||
use items::{rewrite_static, rewrite_type_alias, format_impl, format_trait};
|
||||
|
||||
pub struct FmtVisitor<'a> {
|
||||
pub parse_session: &'a ParseSess,
|
||||
@ -209,8 +209,14 @@ impl<'a> FmtVisitor<'a> {
|
||||
}
|
||||
}
|
||||
// FIXME(#78): format traits.
|
||||
ast::Item_::ItemTrait(_, _, _, ref trait_items) => {
|
||||
ast::Item_::ItemTrait(unsafety, ref generics, ref param_bounds, ref trait_items) => {
|
||||
self.format_missing_with_indent(item.span.lo);
|
||||
if let Some(trait_str) = format_trait(&self.get_context(),
|
||||
item,
|
||||
self.block_indent) {
|
||||
self.buffer.push_str(&trait_str);
|
||||
self.last_pos = item.span.hi;
|
||||
}
|
||||
self.block_indent = self.block_indent.block_indent(self.config);
|
||||
for item in trait_items {
|
||||
self.visit_trait_item(&item);
|
||||
|
@ -35,3 +35,5 @@ trait TraitWithExpr {
|
||||
trait Test {
|
||||
fn read_struct<T, F>(&mut self, s_name: &str, len: usize, f: F) -> Result<T, Self::Error> where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
trait T<> {}
|
||||
|
@ -34,3 +34,5 @@ trait Test {
|
||||
fn read_struct<T, F>(&mut self, s_name: &str, len: usize, f: F) -> Result<T, Self::Error>
|
||||
where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
|
||||
}
|
||||
|
||||
trait T {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user