Skip to content

Commit 8a9bbd9

Browse files
committed
Merge pull request #487 from marcusklaas/konsts-n-statix
Format constants and static variables
2 parents 2995e1b + 3ce425c commit 8a9bbd9

File tree

4 files changed

+112
-75
lines changed

4 files changed

+112
-75
lines changed

src/items.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -825,15 +825,15 @@ impl<'a> FmtVisitor<'a> {
825825
}
826826
}
827827

828-
fn format_struct(&self,
829-
item_name: &str,
830-
ident: ast::Ident,
831-
vis: ast::Visibility,
832-
struct_def: &ast::VariantData,
833-
generics: Option<&ast::Generics>,
834-
span: Span,
835-
offset: Indent)
836-
-> Option<String> {
828+
pub fn format_struct(&self,
829+
item_name: &str,
830+
ident: ast::Ident,
831+
vis: ast::Visibility,
832+
struct_def: &ast::VariantData,
833+
generics: Option<&ast::Generics>,
834+
span: Span,
835+
offset: Indent)
836+
-> Option<String> {
837837
let mut result = String::with_capacity(1024);
838838

839839
let header_str = self.format_header(item_name, ident, vis);
@@ -929,27 +929,6 @@ impl<'a> FmtVisitor<'a> {
929929
Some(result)
930930
}
931931

932-
pub fn visit_struct(&mut self,
933-
ident: ast::Ident,
934-
vis: ast::Visibility,
935-
struct_def: &ast::VariantData,
936-
generics: &ast::Generics,
937-
span: Span) {
938-
let indent = self.block_indent;
939-
let result = self.format_struct("struct ",
940-
ident,
941-
vis,
942-
struct_def,
943-
Some(generics),
944-
span,
945-
indent);
946-
947-
if let Some(rewrite) = result {
948-
self.buffer.push_str(&rewrite);
949-
self.last_pos = span.hi;
950-
}
951-
}
952-
953932
fn format_header(&self, item_name: &str, ident: ast::Ident, vis: ast::Visibility) -> String {
954933
format!("{}{}{}", format_visibility(vis), item_name, ident)
955934
}
@@ -1138,6 +1117,26 @@ impl<'a> FmtVisitor<'a> {
11381117
}
11391118
}
11401119

1120+
pub fn rewrite_static(prefix: &str,
1121+
ident: ast::Ident,
1122+
ty: &ast::Ty,
1123+
mutability: ast::Mutability,
1124+
expr: &ast::Expr,
1125+
context: &RewriteContext)
1126+
-> Option<String> {
1127+
let prefix = format!("{} {}{}: ", prefix, format_mutability(mutability), ident);
1128+
// 2 = " =".len()
1129+
let ty_str = try_opt!(ty.rewrite(context,
1130+
context.config.max_width - context.block_indent.width() -
1131+
prefix.len() - 2,
1132+
context.block_indent));
1133+
let lhs = format!("{}{} =", prefix, ty_str);
1134+
1135+
// 1 = ;
1136+
let remaining_width = context.config.max_width - context.block_indent.width() - 1;
1137+
rewrite_assign_rhs(context, lhs, expr, remaining_width, context.block_indent).map(|s| s + ";")
1138+
}
1139+
11411140
impl Rewrite for ast::FunctionRetTy {
11421141
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
11431142
match *self {

src/visitor.rs

Lines changed: 60 additions & 45 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) {
@@ -58,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
5845
}
5946
}
6047
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
61-
self.format_missing_with_indent(stmt.span.lo);
6248
let suffix = if semicolon_for_stmt(stmt) {
6349
";"
6450
} else {
@@ -67,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
6753
let rewrite = ex.rewrite(&self.get_context(),
6854
self.config.max_width - self.block_indent.width() -
6955
suffix.len(),
70-
self.block_indent);
71-
72-
if let Some(new_str) = rewrite {
73-
self.buffer.push_str(&new_str);
74-
self.buffer.push_str(suffix);
75-
self.last_pos = stmt.span.hi;
76-
}
56+
self.block_indent)
57+
.map(|s| s + suffix);
58+
self.push_rewrite(stmt.span, rewrite);
7759
}
7860
ast::Stmt_::StmtMac(ref _mac, _macro_style) => {
7961
self.format_missing_with_indent(stmt.span.lo);
@@ -104,16 +86,19 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
10486
self.visit_stmt(&stmt)
10587
}
10688

107-
match b.expr {
108-
Some(ref e) => {
109-
self.format_missing_with_indent(e.span.lo);
110-
self.visit_expr(e);
89+
if let Some(ref e) = b.expr {
90+
self.format_missing_with_indent(e.span.lo);
91+
let rewrite = e.rewrite(&self.get_context(),
92+
self.config.max_width - self.block_indent.width(),
93+
self.block_indent)
94+
.unwrap_or_else(|| self.snippet(e.span));
11195

112-
if semicolon_for_expr(e) {
113-
self.buffer.push_str(";");
114-
}
96+
self.buffer.push_str(&rewrite);
97+
self.last_pos = e.span.hi;
98+
99+
if semicolon_for_expr(e) {
100+
self.buffer.push_str(";");
115101
}
116-
None => {}
117102
}
118103

119104
self.block_indent = self.block_indent.block_unindent(self.config);
@@ -131,7 +116,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
131116
b: &'v ast::Block,
132117
s: Span,
133118
_: ast::NodeId) {
134-
135119
let indent = self.block_indent;
136120
let rewrite = match fk {
137121
visit::FnKind::ItemFn(ident, ref generics, unsafety, constness, abi, vis) => {
@@ -190,6 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
190174
ast::Item_::ItemUse(ref vp) => {
191175
self.format_import(item.vis, vp, item.span);
192176
}
177+
// FIXME(#78): format traits and impl definitions.
193178
ast::Item_::ItemImpl(..) |
194179
ast::Item_::ItemTrait(..) => {
195180
self.block_indent = self.block_indent.block_indent(self.config);
@@ -203,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
203188
self.last_pos = item.span.hi;
204189
}
205190
ast::Item_::ItemStruct(ref def, ref generics) => {
206-
self.format_missing_with_indent(item.span.lo);
207-
self.visit_struct(item.ident, item.vis, def, generics, item.span);
191+
let indent = self.block_indent;
192+
let rewrite = self.format_struct("struct ",
193+
item.ident,
194+
item.vis,
195+
def,
196+
Some(generics),
197+
item.span,
198+
indent);
199+
self.push_rewrite(item.span, rewrite);
208200
}
209201
ast::Item_::ItemEnum(ref def, ref generics) => {
210202
self.format_missing_with_indent(item.span.lo);
@@ -225,7 +217,28 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
225217
self.format_missing_with_indent(item.span.lo);
226218
self.format_foreign_mod(foreign_mod, item.span);
227219
}
228-
_ => {
220+
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => {
221+
let rewrite = rewrite_static("static",
222+
item.ident,
223+
ty,
224+
mutability,
225+
expr,
226+
&self.get_context());
227+
self.push_rewrite(item.span, rewrite);
228+
}
229+
ast::Item_::ItemConst(ref ty, ref expr) => {
230+
let rewrite = rewrite_static("const",
231+
item.ident,
232+
ty,
233+
ast::Mutability::MutImmutable,
234+
expr,
235+
&self.get_context());
236+
self.push_rewrite(item.span, rewrite);
237+
}
238+
// FIXME(#486): format type aliases.
239+
ast::Item_::ItemDefaultImpl(..) |
240+
ast::Item_::ItemFn(..) |
241+
ast::Item_::ItemTy(..) => {
229242
visit::walk_item(self, item);
230243
}
231244
}
@@ -237,17 +250,10 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
237250
}
238251

239252
if let ast::TraitItem_::MethodTraitItem(ref sig, None) = ti.node {
240-
self.format_missing_with_indent(ti.span.lo);
241-
242253
let indent = self.block_indent;
243-
let new_fn = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
244-
245-
if let Some(fn_str) = new_fn {
246-
self.buffer.push_str(&fn_str);
247-
self.last_pos = ti.span.hi;
248-
}
254+
let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
255+
self.push_rewrite(ti.span, rewrite);
249256
}
250-
// TODO(#78): format trait types.
251257

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

274280
impl<'a> FmtVisitor<'a> {
281+
fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
282+
self.format_missing_with_indent(span.lo);
283+
284+
if let Some(res) = rewrite {
285+
self.buffer.push_str(&res);
286+
self.last_pos = span.hi;
287+
}
288+
}
289+
275290
pub fn from_codemap(codemap: &'a CodeMap, config: &'a Config) -> FmtVisitor<'a> {
276291
FmtVisitor {
277292
codemap: codemap,

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)