Skip to content

Commit e31a48b

Browse files
committed
Refactoring: remove duplicates
1 parent e780532 commit e31a48b

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a> FmtVisitor<'a> {
260260
) {
261261
let vis = utils::format_visibility(vis);
262262
// 4 = `use `, 1 = `;`
263-
let rw = Shape::indented(self.block_indent, self.config)
263+
let rw = self.shape()
264264
.offset_left(vis.len() + 4)
265265
.and_then(|shape| shape.sub_width(1))
266266
.and_then(|shape| match vp.node {

src/items.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ impl<'a> FmtVisitor<'a> {
187187

188188

189189
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
190-
let shape = Shape::indented(self.block_indent, self.config);
191-
let rewrite = item.rewrite(&self.get_context(), shape);
190+
let rewrite = item.rewrite(&self.get_context(), self.shape());
192191
self.push_rewrite(item.span(), rewrite);
193192
self.last_pos = item.span.hi;
194193
}
@@ -312,18 +311,11 @@ impl<'a> FmtVisitor<'a> {
312311
""
313312
};
314313

315-
format_expr(
316-
&e,
317-
ExprType::Statement,
318-
&self.get_context(),
319-
Shape::indented(self.block_indent, self.config),
320-
).map(|s| s + suffix)
314+
format_expr(&e, ExprType::Statement, &self.get_context(), self.shape())
315+
.map(|s| s + suffix)
321316
.or_else(|| Some(self.snippet(e.span)))
322317
}
323-
None => stmt.rewrite(
324-
&self.get_context(),
325-
Shape::indented(self.block_indent, self.config),
326-
),
318+
None => stmt.rewrite(&self.get_context(), self.shape()),
327319
}
328320
} else {
329321
None
@@ -421,9 +413,7 @@ impl<'a> FmtVisitor<'a> {
421413
false,
422414
);
423415

424-
let shape = Shape::indented(self.block_indent, self.config)
425-
.sub_width(2)
426-
.unwrap();
416+
let shape = self.shape().sub_width(2).unwrap();
427417
let fmt = ListFormatting {
428418
tactic: DefinitiveListTactic::Vertical,
429419
separator: ",",
@@ -451,7 +441,7 @@ impl<'a> FmtVisitor<'a> {
451441

452442
let context = self.get_context();
453443
let indent = self.block_indent;
454-
let shape = Shape::indented(indent, self.config);
444+
let shape = self.shape();
455445
let attrs_str = try_opt!(field.node.attrs.rewrite(&context, shape));
456446
let lo = field
457447
.node

src/visitor.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ pub struct FmtVisitor<'a> {
5858
}
5959

6060
impl<'a> FmtVisitor<'a> {
61+
pub fn shape(&self) -> Shape {
62+
Shape::indented(self.block_indent, self.config)
63+
}
64+
6165
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
6266
debug!(
6367
"visit_stmt: {:?} {:?}",
@@ -138,9 +142,7 @@ impl<'a> FmtVisitor<'a> {
138142
if let Some(first_stmt) = b.stmts.first() {
139143
let attr_lo = inner_attrs
140144
.and_then(|attrs| {
141-
utils::inner_attributes(attrs)
142-
.first()
143-
.map(|attr| attr.span.lo)
145+
inner_attributes(attrs).first().map(|attr| attr.span.lo)
144146
})
145147
.or_else(|| {
146148
// Attributes for an item in a statement position
@@ -218,7 +220,7 @@ impl<'a> FmtVisitor<'a> {
218220
let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
219221
if unindent_comment {
220222
let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
221-
let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
223+
let snippet = self.snippet(mk_sp(self.last_pos, end_pos));
222224
unindent_comment = snippet.contains("//") || snippet.contains("/*");
223225
}
224226
// FIXME: we should compress any newlines here to just one
@@ -336,7 +338,7 @@ impl<'a> FmtVisitor<'a> {
336338
self.push_rewrite(item.span, None);
337339
return;
338340
}
339-
} else if utils::contains_skip(&item.attrs) {
341+
} else if contains_skip(&item.attrs) {
340342
// Module is not inline, but should be skipped.
341343
return;
342344
} else {
@@ -371,7 +373,7 @@ impl<'a> FmtVisitor<'a> {
371373
}
372374
ast::ItemKind::Impl(..) => {
373375
self.format_missing_with_indent(source!(self, item.span).lo);
374-
let snippet = self.get_context().snippet(item.span);
376+
let snippet = self.snippet(item.span);
375377
let where_span_end = snippet
376378
.find_uncommented("{")
377379
.map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
@@ -635,9 +637,7 @@ impl<'a> FmtVisitor<'a> {
635637
skip_out_of_file_lines_range_visitor!(self, mac.span);
636638

637639
// 1 = ;
638-
let shape = Shape::indented(self.block_indent, self.config)
639-
.sub_width(1)
640-
.unwrap();
640+
let shape = self.shape().sub_width(1).unwrap();
641641
let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
642642
self.push_rewrite(mac.span, rewrite);
643643
}
@@ -677,7 +677,7 @@ impl<'a> FmtVisitor<'a> {
677677

678678
// Returns true if we should skip the following item.
679679
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
680-
if utils::contains_skip(attrs) {
680+
if contains_skip(attrs) {
681681
return true;
682682
}
683683

@@ -686,10 +686,7 @@ impl<'a> FmtVisitor<'a> {
686686
return false;
687687
}
688688

689-
let rewrite = attrs.rewrite(
690-
&self.get_context(),
691-
Shape::indented(self.block_indent, self.config),
692-
);
689+
let rewrite = attrs.rewrite(&self.get_context(), self.shape());
693690
let span = mk_sp(attrs[0].span.lo, attrs[attrs.len() - 1].span.hi);
694691
self.push_rewrite(span, rewrite);
695692

0 commit comments

Comments
 (0)