Skip to content

Commit 1a7d390

Browse files
committed
Format constants and static variables
1 parent d326a29 commit 1a7d390

File tree

4 files changed

+88
-27
lines changed

4 files changed

+88
-27
lines changed

src/items.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,26 @@ impl<'a> FmtVisitor<'a> {
11361136
}
11371137
}
11381138

1139+
pub fn rewrite_static(prefix: &str,
1140+
ident: ast::Ident,
1141+
ty: &ast::Ty,
1142+
mutability: ast::Mutability,
1143+
expr: &ast::Expr,
1144+
context: &RewriteContext)
1145+
-> Option<String> {
1146+
let prefix = format!("{} {}{}: ", prefix, format_mutability(mutability), ident);
1147+
// 2 = " =".len()
1148+
let ty_str = try_opt!(ty.rewrite(context,
1149+
context.config.max_width - context.block_indent.width() -
1150+
prefix.len() - 2,
1151+
context.block_indent));
1152+
let lhs = format!("{}{} =", prefix, ty_str);
1153+
1154+
// 1 = ;
1155+
let remaining_width = context.config.max_width - context.block_indent.width() - 1;
1156+
rewrite_assign_rhs(context, lhs, expr, remaining_width, context.block_indent).map(|s| s + ";")
1157+
}
1158+
11391159
impl Rewrite for ast::FunctionRetTy {
11401160
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
11411161
match *self {

src/visitor.rs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use config::Config;
2020
use rewrite::{Rewrite, RewriteContext};
2121
use comment::rewrite_comment;
2222
use macros::rewrite_macro;
23+
use items::rewrite_static;
2324

2425
pub struct FmtVisitor<'a> {
2526
pub codemap: &'a CodeMap,
@@ -31,22 +32,8 @@ pub struct FmtVisitor<'a> {
3132
}
3233

3334
impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
34-
// FIXME: We'd rather not format expressions here, as we have little
35-
// context. How are we still reaching this?
36-
fn visit_expr(&mut self, ex: &'v ast::Expr) {
37-
debug!("visit_expr: {:?} {:?}",
38-
self.codemap.lookup_char_pos(ex.span.lo),
39-
self.codemap.lookup_char_pos(ex.span.hi));
40-
self.format_missing(ex.span.lo);
41-
42-
let rewrite = ex.rewrite(&self.get_context(),
43-
self.config.max_width - self.block_indent.width(),
44-
self.block_indent);
45-
46-
if let Some(new_str) = rewrite {
47-
self.buffer.push_str(&new_str);
48-
self.last_pos = ex.span.hi;
49-
}
35+
fn visit_expr(&mut self, _: &'v ast::Expr) {
36+
unreachable!()
5037
}
5138

5239
fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
@@ -104,16 +91,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
10491
self.visit_stmt(&stmt)
10592
}
10693

107-
match b.expr {
108-
Some(ref e) => {
109-
self.format_missing_with_indent(e.span.lo);
110-
self.visit_expr(e);
94+
if let Some(ref e) = b.expr {
95+
self.format_missing_with_indent(e.span.lo);
96+
let rewrite = e.rewrite(&self.get_context(),
97+
self.config.max_width - self.block_indent.width(),
98+
self.block_indent)
99+
.unwrap_or_else(|| self.snippet(e.span));
111100

112-
if semicolon_for_expr(e) {
113-
self.buffer.push_str(";");
114-
}
101+
self.buffer.push_str(&rewrite);
102+
self.last_pos = e.span.hi;
103+
104+
if semicolon_for_expr(e) {
105+
self.buffer.push_str(";");
115106
}
116-
None => {}
117107
}
118108

119109
self.block_indent = self.block_indent.block_unindent(self.config);
@@ -131,7 +121,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
131121
b: &'v ast::Block,
132122
s: Span,
133123
_: ast::NodeId) {
134-
135124
let indent = self.block_indent;
136125
let rewrite = match fk {
137126
visit::FnKind::ItemFn(ident, ref generics, unsafety, constness, abi, vis) => {
@@ -190,6 +179,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
190179
ast::Item_::ItemUse(ref vp) => {
191180
self.format_import(item.vis, vp, item.span);
192181
}
182+
// TODO(#78): format traits and impl definitions.
193183
ast::Item_::ItemImpl(..) |
194184
ast::Item_::ItemTrait(..) => {
195185
self.block_indent = self.block_indent.block_indent(self.config);
@@ -225,7 +215,36 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
225215
self.format_missing_with_indent(item.span.lo);
226216
self.format_foreign_mod(foreign_mod, item.span);
227217
}
228-
_ => {
218+
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => {
219+
self.format_missing_with_indent(item.span.lo);
220+
let rewrite = rewrite_static("static",
221+
item.ident,
222+
ty,
223+
mutability,
224+
expr,
225+
&self.get_context());
226+
if let Some(s) = rewrite {
227+
self.buffer.push_str(&s);
228+
self.last_pos = item.span.hi;
229+
}
230+
}
231+
ast::Item_::ItemConst(ref ty, ref expr) => {
232+
self.format_missing_with_indent(item.span.lo);
233+
let rewrite = rewrite_static("const",
234+
item.ident,
235+
ty,
236+
ast::Mutability::MutImmutable,
237+
expr,
238+
&self.get_context());
239+
if let Some(s) = rewrite {
240+
self.buffer.push_str(&s);
241+
self.last_pos = item.span.hi;
242+
}
243+
}
244+
// TODO(#486): format type aliases.
245+
ast::Item_::ItemDefaultImpl(..) |
246+
ast::Item_::ItemFn(..) |
247+
ast::Item_::ItemTy(..) => {
229248
visit::walk_item(self, item);
230249
}
231250
}
@@ -247,7 +266,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
247266
self.last_pos = ti.span.hi;
248267
}
249268
}
250-
// TODO(#78): format trait types.
251269

252270
visit::walk_trait_item(self, ti)
253271
}

tests/source/static.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub const FILE_GENERIC_READ: DWORD =
2+
STANDARD_RIGHTS_READ | FILE_READ_DATA |
3+
FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
4+
5+
pub static boolnames: &'static[&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo",
6+
"gn", "hc", "km", "hs", "in", "db", "da", "mir", "msgr", "os", "eslok", "xt", "hz", "ul", "xon",
7+
"nxon", "mc5i", "chts", "nrrmc", "npc", "ndscr", "ccc", "bce", "hls", "xhpa", "crxm", "daisy",
8+
"xvpa", "sam", "cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT", "OTNL", "OTpt", "OTxr"];
9+
10+
static mut name: SomeType = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;

tests/target/static.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const FILE_GENERIC_READ: DWORD = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
2+
FILE_READ_EA | SYNCHRONIZE;
3+
4+
static boolnames: &'static [&'static str] = &["bw", "am", "xsb", "xhp", "xenl", "eo", "gn", "hc",
5+
"km", "hs", "in", "db", "da", "mir", "msgr", "os",
6+
"eslok", "xt", "hz", "ul", "xon", "nxon", "mc5i",
7+
"chts", "nrrmc", "npc", "ndscr", "ccc", "bce",
8+
"hls", "xhpa", "crxm", "daisy", "xvpa", "sam",
9+
"cpix", "lpix", "OTbs", "OTns", "OTnc", "OTMT",
10+
"OTNL", "OTpt", "OTxr"];
11+
12+
static mut name: SomeType =
13+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;

0 commit comments

Comments
 (0)