Skip to content

Commit 3ce425c

Browse files
committed
Factor out common item indentation idiom
1 parent 1a7d390 commit 3ce425c

File tree

2 files changed

+36
-60
lines changed

2 files changed

+36
-60
lines changed

src/items.rs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -823,15 +823,15 @@ impl<'a> FmtVisitor<'a> {
823823
}
824824
}
825825

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

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

930-
pub fn visit_struct(&mut self,
931-
ident: ast::Ident,
932-
vis: ast::Visibility,
933-
struct_def: &ast::VariantData,
934-
generics: &ast::Generics,
935-
span: Span) {
936-
let indent = self.block_indent;
937-
let result = self.format_struct("struct ",
938-
ident,
939-
vis,
940-
struct_def,
941-
Some(generics),
942-
span,
943-
indent);
944-
945-
if let Some(rewrite) = result {
946-
self.buffer.push_str(&rewrite);
947-
self.last_pos = span.hi;
948-
}
949-
}
950-
951930
fn format_header(&self, item_name: &str, ident: ast::Ident, vis: ast::Visibility) -> String {
952931
format!("{}{}{}", format_visibility(vis), item_name, ident)
953932
}

src/visitor.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
4545
}
4646
}
4747
ast::Stmt_::StmtExpr(ref ex, _) | ast::Stmt_::StmtSemi(ref ex, _) => {
48-
self.format_missing_with_indent(stmt.span.lo);
4948
let suffix = if semicolon_for_stmt(stmt) {
5049
";"
5150
} else {
@@ -54,13 +53,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
5453
let rewrite = ex.rewrite(&self.get_context(),
5554
self.config.max_width - self.block_indent.width() -
5655
suffix.len(),
57-
self.block_indent);
58-
59-
if let Some(new_str) = rewrite {
60-
self.buffer.push_str(&new_str);
61-
self.buffer.push_str(suffix);
62-
self.last_pos = stmt.span.hi;
63-
}
56+
self.block_indent)
57+
.map(|s| s + suffix);
58+
self.push_rewrite(stmt.span, rewrite);
6459
}
6560
ast::Stmt_::StmtMac(ref _mac, _macro_style) => {
6661
self.format_missing_with_indent(stmt.span.lo);
@@ -179,7 +174,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
179174
ast::Item_::ItemUse(ref vp) => {
180175
self.format_import(item.vis, vp, item.span);
181176
}
182-
// TODO(#78): format traits and impl definitions.
177+
// FIXME(#78): format traits and impl definitions.
183178
ast::Item_::ItemImpl(..) |
184179
ast::Item_::ItemTrait(..) => {
185180
self.block_indent = self.block_indent.block_indent(self.config);
@@ -193,8 +188,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
193188
self.last_pos = item.span.hi;
194189
}
195190
ast::Item_::ItemStruct(ref def, ref generics) => {
196-
self.format_missing_with_indent(item.span.lo);
197-
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);
198200
}
199201
ast::Item_::ItemEnum(ref def, ref generics) => {
200202
self.format_missing_with_indent(item.span.lo);
@@ -216,32 +218,24 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
216218
self.format_foreign_mod(foreign_mod, item.span);
217219
}
218220
ast::Item_::ItemStatic(ref ty, mutability, ref expr) => {
219-
self.format_missing_with_indent(item.span.lo);
220221
let rewrite = rewrite_static("static",
221222
item.ident,
222223
ty,
223224
mutability,
224225
expr,
225226
&self.get_context());
226-
if let Some(s) = rewrite {
227-
self.buffer.push_str(&s);
228-
self.last_pos = item.span.hi;
229-
}
227+
self.push_rewrite(item.span, rewrite);
230228
}
231229
ast::Item_::ItemConst(ref ty, ref expr) => {
232-
self.format_missing_with_indent(item.span.lo);
233230
let rewrite = rewrite_static("const",
234231
item.ident,
235232
ty,
236233
ast::Mutability::MutImmutable,
237234
expr,
238235
&self.get_context());
239-
if let Some(s) = rewrite {
240-
self.buffer.push_str(&s);
241-
self.last_pos = item.span.hi;
242-
}
236+
self.push_rewrite(item.span, rewrite);
243237
}
244-
// TODO(#486): format type aliases.
238+
// FIXME(#486): format type aliases.
245239
ast::Item_::ItemDefaultImpl(..) |
246240
ast::Item_::ItemFn(..) |
247241
ast::Item_::ItemTy(..) => {
@@ -256,15 +250,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
256250
}
257251

258252
if let ast::TraitItem_::MethodTraitItem(ref sig, None) = ti.node {
259-
self.format_missing_with_indent(ti.span.lo);
260-
261253
let indent = self.block_indent;
262-
let new_fn = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
263-
264-
if let Some(fn_str) = new_fn {
265-
self.buffer.push_str(&fn_str);
266-
self.last_pos = ti.span.hi;
267-
}
254+
let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
255+
self.push_rewrite(ti.span, rewrite);
268256
}
269257

270258
visit::walk_trait_item(self, ti)
@@ -290,6 +278,15 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
290278
}
291279

292280
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+
293290
pub fn from_codemap(codemap: &'a CodeMap, config: &'a Config) -> FmtVisitor<'a> {
294291
FmtVisitor {
295292
codemap: codemap,

0 commit comments

Comments
 (0)