Skip to content

Format constants and static variables #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,15 +823,15 @@ impl<'a> FmtVisitor<'a> {
}
}

fn format_struct(&self,
item_name: &str,
ident: ast::Ident,
vis: ast::Visibility,
struct_def: &ast::VariantData,
generics: Option<&ast::Generics>,
span: Span,
offset: Indent)
-> Option<String> {
pub fn format_struct(&self,
item_name: &str,
ident: ast::Ident,
vis: ast::Visibility,
struct_def: &ast::VariantData,
generics: Option<&ast::Generics>,
span: Span,
offset: Indent)
-> Option<String> {
let mut result = String::with_capacity(1024);

let header_str = self.format_header(item_name, ident, vis);
Expand Down Expand Up @@ -927,27 +927,6 @@ impl<'a> FmtVisitor<'a> {
Some(result)
}

pub fn visit_struct(&mut self,
ident: ast::Ident,
vis: ast::Visibility,
struct_def: &ast::VariantData,
generics: &ast::Generics,
span: Span) {
let indent = self.block_indent;
let result = self.format_struct("struct ",
ident,
vis,
struct_def,
Some(generics),
span,
indent);

if let Some(rewrite) = result {
self.buffer.push_str(&rewrite);
self.last_pos = span.hi;
}
}

fn format_header(&self, item_name: &str, ident: ast::Ident, vis: ast::Visibility) -> String {
format!("{}{}{}", format_visibility(vis), item_name, ident)
}
Expand Down Expand Up @@ -1136,6 +1115,26 @@ impl<'a> FmtVisitor<'a> {
}
}

pub fn rewrite_static(prefix: &str,
ident: ast::Ident,
ty: &ast::Ty,
mutability: ast::Mutability,
expr: &ast::Expr,
context: &RewriteContext)
-> Option<String> {
let prefix = format!("{} {}{}: ", prefix, format_mutability(mutability), ident);
// 2 = " =".len()
let ty_str = try_opt!(ty.rewrite(context,
context.config.max_width - context.block_indent.width() -
prefix.len() - 2,
context.block_indent));
let lhs = format!("{}{} =", prefix, ty_str);

// 1 = ;
let remaining_width = context.config.max_width - context.block_indent.width() - 1;
rewrite_assign_rhs(context, lhs, expr, remaining_width, context.block_indent).map(|s| s + ";")
}

impl Rewrite for ast::FunctionRetTy {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
match *self {
Expand Down
105 changes: 60 additions & 45 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use config::Config;
use rewrite::{Rewrite, RewriteContext};
use comment::rewrite_comment;
use macros::rewrite_macro;
use items::rewrite_static;

pub struct FmtVisitor<'a> {
pub codemap: &'a CodeMap,
Expand All @@ -31,22 +32,8 @@ pub struct FmtVisitor<'a> {
}

impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
// FIXME: We'd rather not format expressions here, as we have little
// context. How are we still reaching this?
fn visit_expr(&mut self, ex: &'v ast::Expr) {
debug!("visit_expr: {:?} {:?}",
self.codemap.lookup_char_pos(ex.span.lo),
self.codemap.lookup_char_pos(ex.span.hi));
self.format_missing(ex.span.lo);

let rewrite = ex.rewrite(&self.get_context(),
self.config.max_width - self.block_indent.width(),
self.block_indent);

if let Some(new_str) = rewrite {
self.buffer.push_str(&new_str);
self.last_pos = ex.span.hi;
}
fn visit_expr(&mut self, _: &'v ast::Expr) {
unreachable!()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\o/

}

fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
Expand All @@ -58,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}
}
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
self.format_missing_with_indent(stmt.span.lo);
let suffix = if semicolon_for_stmt(stmt) {
";"
} else {
Expand All @@ -67,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
let rewrite = ex.rewrite(&self.get_context(),
self.config.max_width - self.block_indent.width() -
suffix.len(),
self.block_indent);

if let Some(new_str) = rewrite {
self.buffer.push_str(&new_str);
self.buffer.push_str(suffix);
self.last_pos = stmt.span.hi;
}
self.block_indent)
.map(|s| s + suffix);
self.push_rewrite(stmt.span, rewrite);
}
ast::Stmt_::StmtMac(ref _mac, _macro_style) => {
self.format_missing_with_indent(stmt.span.lo);
Expand Down Expand Up @@ -104,16 +86,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.visit_stmt(&stmt)
}

match b.expr {
Some(ref e) => {
self.format_missing_with_indent(e.span.lo);
self.visit_expr(e);
if let Some(ref e) = b.expr {
self.format_missing_with_indent(e.span.lo);
let rewrite = e.rewrite(&self.get_context(),
self.config.max_width - self.block_indent.width(),
self.block_indent)
.unwrap_or_else(|| self.snippet(e.span));

if semicolon_for_expr(e) {
self.buffer.push_str(";");
}
self.buffer.push_str(&rewrite);
self.last_pos = e.span.hi;

if semicolon_for_expr(e) {
self.buffer.push_str(";");
}
None => {}
}

self.block_indent = self.block_indent.block_unindent(self.config);
Expand All @@ -131,7 +116,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
b: &'v ast::Block,
s: Span,
_: ast::NodeId) {

let indent = self.block_indent;
let rewrite = match fk {
visit::FnKind::ItemFn(ident, ref generics, unsafety, constness, abi, vis) => {
Expand Down Expand Up @@ -190,6 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
ast::Item_::ItemUse(ref vp) => {
self.format_import(item.vis, vp, item.span);
}
// FIXME(#78): format traits and impl definitions.
ast::Item_::ItemImpl(..) |
ast::Item_::ItemTrait(..) => {
self.block_indent = self.block_indent.block_indent(self.config);
Expand All @@ -203,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.last_pos = item.span.hi;
}
ast::Item_::ItemStruct(ref def, ref generics) => {
self.format_missing_with_indent(item.span.lo);
self.visit_struct(item.ident, item.vis, def, generics, item.span);
let indent = self.block_indent;
let rewrite = self.format_struct("struct ",
item.ident,
item.vis,
def,
Some(generics),
item.span,
indent);
self.push_rewrite(item.span, rewrite);
}
ast::Item_::ItemEnum(ref def, ref generics) => {
self.format_missing_with_indent(item.span.lo);
Expand All @@ -225,7 +217,28 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
self.format_missing_with_indent(item.span.lo);
self.format_foreign_mod(foreign_mod, item.span);
}
_ => {
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => {
let rewrite = rewrite_static("static",
item.ident,
ty,
mutability,
expr,
&self.get_context());
self.push_rewrite(item.span, rewrite);
}
ast::Item_::ItemConst(ref ty, ref expr) => {
let rewrite = rewrite_static("const",
item.ident,
ty,
ast::Mutability::MutImmutable,
expr,
&self.get_context());
self.push_rewrite(item.span, rewrite);
}
// FIXME(#486): format type aliases.
ast::Item_::ItemDefaultImpl(..) |
ast::Item_::ItemFn(..) |
ast::Item_::ItemTy(..) => {
visit::walk_item(self, item);
}
}
Expand All @@ -237,17 +250,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}

if let ast::TraitItem_::MethodTraitItem(ref sig, None) = ti.node {
self.format_missing_with_indent(ti.span.lo);

let indent = self.block_indent;
let new_fn = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);

if let Some(fn_str) = new_fn {
self.buffer.push_str(&fn_str);
self.last_pos = ti.span.hi;
}
let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
self.push_rewrite(ti.span, rewrite);
}
// TODO(#78): format trait types.

visit::walk_trait_item(self, ti)
}
Expand All @@ -272,6 +278,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}

impl<'a> FmtVisitor<'a> {
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
self.format_missing_with_indent(span.lo);

if let Some(res) = rewrite {
self.buffer.push_str(&res);
self.last_pos = span.hi;
}
}

pub fn from_codemap(codemap: &'a CodeMap, config: &'a Config) -> FmtVisitor<'a> {
FmtVisitor {
codemap: codemap,
Expand Down
10 changes: 10 additions & 0 deletions tests/source/static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub const FILE_GENERIC_READ: DWORD =
STANDARD_RIGHTS_READ | FILE_READ_DATA |
FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;

pub static boolnames: &'static[&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo",
"gn", "hc", "km", "hs", "in", "db", "da", "mir", "msgr", "os", "eslok", "xt", "hz", "ul", "xon",
"nxon", "mc5i", "chts", "nrrmc", "npc", "ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy",
"xvpa", "sam", "cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr"];

static mut name: SomeType = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
13 changes: 13 additions & 0 deletions tests/target/static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const FILE_GENERIC_READ: DWORD = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
FILE_READ_EA | SYNCHRONIZE;

static boolnames: &'static [&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc",
"km", "hs", "in", "db", "da", "mir", "msgr", "os",
"eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i",
"chts", "nrrmc", "npc", "ndscr", "ccc", "bce",
"hls", "xhpa", "crxm", "daisy", "xvpa", "sam",
"cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT",
"OTNL", "OTpt", "OTxr"];

static mut name: SomeType =
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;