Skip to content

Commit a1d28bf

Browse files
authored
Merge pull request #1872 from sinkuu/remove_newline
Remove blank lines at start or end of block
2 parents 16920fb + f9ee060 commit a1d28bf

23 files changed

+105
-27
lines changed

src/bin/cargo-fmt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result<Vec<Target>, std::
271271
std::io::ErrorKind::NotFound,
272272
str::from_utf8(&output.stderr).unwrap(),
273273
))
274-
275274
}
276275

277276
fn target_from_json(jtarget: &Value) -> Target {

src/bin/rustfmt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ fn match_cli_path_or_file(
102102
config_path: Option<PathBuf>,
103103
input_file: &Path,
104104
) -> FmtResult<(Config, Option<PathBuf>)> {
105-
106105
if let Some(config_file) = config_path {
107106
let toml = Config::from_toml_path(config_file.as_ref())?;
108107
return Ok((toml, Some(config_file)));

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ create_config! {
606606
tuple patterns";
607607
combine_control_expr: bool, true, "Combine control expressions with funciton calls.";
608608
struct_field_align_threshold: usize, 0, "Align struct fields if their diffs fits within \
609-
threshold."
609+
threshold.";
610+
remove_blank_lines_at_start_or_end_of_block: bool, true,
611+
"Remove blank lines at start or end of a block";
610612
}
611613

612614
#[cfg(test)]

src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2937,7 +2937,6 @@ fn choose_rhs(
29372937
}
29382938

29392939
fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool {
2940-
29412940
fn count_line_breaks(src: &str) -> usize {
29422941
src.chars().filter(|&x| x == '\n').count()
29432942
}

src/filemap.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ pub fn write_file<T>(
9090
where
9191
T: Write,
9292
{
93-
9493
fn source_and_formatted_text(
9594
text: &StringBuffer,
9695
filename: &str,

src/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ where
404404
} else {
405405
Some(format!("{}{}", args, output))
406406
}
407-
408407
}
409408

410409
fn type_bound_colon(context: &RewriteContext) -> &'static str {
@@ -601,7 +600,6 @@ impl Rewrite for ast::TyParam {
601600
result.push_str(&join_bounds(context, shape, &strs));
602601
}
603602
if let Some(ref def) = self.default {
604-
605603
let eq_str = match context.config.type_punctuation_density() {
606604
TypeDensity::Compressed => "=",
607605
TypeDensity::Wide => " = ",

src/visitor.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use std::cmp;
1212

1313
use strings::string_buffer::StringBuffer;
1414
use syntax::{ast, ptr, visit};
15-
use syntax::codemap::{self, BytePos, CodeMap, Span};
15+
use syntax::attr::HasAttrs;
16+
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
1617
use syntax::parse::ParseSess;
1718

1819
use {Indent, Shape, Spanned};
1920
use codemap::{LineRangeUtils, SpanUtils};
20-
use comment::{contains_comment, FindUncommented};
21+
use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
2122
use comment::rewrite_comment;
2223
use config::{BraceStyle, Config};
2324
use expr::{format_expr, ExprType};
@@ -131,6 +132,48 @@ impl<'a> FmtVisitor<'a> {
131132
self.block_indent = self.block_indent.block_indent(self.config);
132133
self.buffer.push_str("{");
133134

135+
if self.config.remove_blank_lines_at_start_or_end_of_block() {
136+
if let Some(first_stmt) = b.stmts.first() {
137+
let attr_lo = inner_attrs
138+
.and_then(|attrs| {
139+
utils::inner_attributes(attrs)
140+
.first()
141+
.map(|attr| attr.span.lo)
142+
})
143+
.or_else(|| {
144+
// Attributes for an item in a statement position
145+
// do not belong to the statement. (rust-lang/rust#34459)
146+
if let ast::StmtKind::Item(ref item) = first_stmt.node {
147+
item.attrs.first()
148+
} else {
149+
first_stmt.attrs().first()
150+
}.and_then(|attr| {
151+
// Some stmts can have embedded attributes.
152+
// e.g. `match { #![attr] ... }`
153+
let attr_lo = attr.span.lo;
154+
if attr_lo < first_stmt.span.lo {
155+
Some(attr_lo)
156+
} else {
157+
None
158+
}
159+
})
160+
});
161+
162+
let snippet =
163+
self.snippet(mk_sp(self.last_pos, attr_lo.unwrap_or(first_stmt.span.lo)));
164+
let len = CommentCodeSlices::new(&snippet).nth(0).and_then(
165+
|(kind, _, s)| if kind == CodeCharKind::Normal {
166+
s.rfind('\n')
167+
} else {
168+
None
169+
},
170+
);
171+
if let Some(len) = len {
172+
self.last_pos = self.last_pos + BytePos::from_usize(len);
173+
}
174+
}
175+
}
176+
134177
// Format inner attributes if available.
135178
if let Some(attrs) = inner_attrs {
136179
self.visit_attrs(attrs, ast::AttrStyle::Inner);
@@ -148,17 +191,39 @@ impl<'a> FmtVisitor<'a> {
148191
}
149192
}
150193

194+
let mut remove_len = BytePos(0);
195+
if self.config.remove_blank_lines_at_start_or_end_of_block() {
196+
if let Some(stmt) = b.stmts.last() {
197+
let snippet = self.snippet(mk_sp(
198+
stmt.span.hi,
199+
source!(self, b.span).hi - brace_compensation,
200+
));
201+
let len = CommentCodeSlices::new(&snippet)
202+
.last()
203+
.and_then(|(kind, _, s)| {
204+
if kind == CodeCharKind::Normal && s.trim().is_empty() {
205+
Some(s.len())
206+
} else {
207+
None
208+
}
209+
});
210+
if let Some(len) = len {
211+
remove_len = BytePos::from_usize(len);
212+
}
213+
}
214+
}
215+
151216
let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
152217
if unindent_comment {
153-
let end_pos = source!(self, b.span).hi - brace_compensation;
218+
let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
154219
let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
155220
unindent_comment = snippet.contains("//") || snippet.contains("/*");
156221
}
157222
// FIXME: we should compress any newlines here to just one
158223
if unindent_comment {
159224
self.block_indent = self.block_indent.block_unindent(self.config);
160225
}
161-
self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation);
226+
self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation - remove_len);
162227
if unindent_comment {
163228
self.block_indent = self.block_indent.block_indent(self.config);
164229
}

tests/source/fn-simple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ pub fn waltz(cwd: &Path) -> CliAssert {
5151
formatted_comment = rewrite_comment(comment, block_style, width, offset, formatting_fig);
5252
}
5353
}
54-
5554
}

tests/source/indent_match_arms.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ fn main() {
2323
},
2424
_ => "something else",
2525
}
26-
2726
}

tests/source/issue-510.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ fn solve_inline_size_constraints(&self,
33
block: &mut BlockFlow,
44
input: &ISizeConstraintInput)
55
-> ISizeConstraintSolution {
6-
76
let (inline_start,inline_size,margin_inline_start,margin_inline_end) =
87
match (inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx,inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx) {
98
(MaybeAuto::Auto, MaybeAuto::Auto, MaybeAuto::Auto) => {

tests/source/remove_blank_lines.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn main() {
2+
3+
4+
5+
6+
let x = 1;
7+
8+
9+
10+
11+
}
12+
13+
fn foo() {
14+
15+
#![attribute]
16+
17+
let x = 1;
18+
19+
// comment
20+
21+
22+
}

tests/source/spaces-within-angle-brackets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fn foo<T, E>(a: T, b: E) {
3131
}
3232

3333
fn foo<T: Send, E: Send>(a: T, b: E) {
34-
3534
foo::<u32, str>(10, "bar");
3635

3736
let opt: Option<u32>;

tests/source/spaces-within-parens.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ struct TupleStruct2(u32, u32);
1414
fn fooEmpty() {}
1515

1616
fn foo(e: E, _: u32) -> (u32, u32) {
17-
1817
// Tuples
1918
let t1 = ();
2019
let t2 = (1,);

tests/source/spaces-within-square-brackets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// rustfmt-spaces_within_square_brackets: true
22

33
fn main() {
4-
54
let arr: [i32; 5] = [1, 2, 3, 4, 5];
65
let arr: [i32; 500] = [0; 500];
76

tests/source/struct_tuple_visual.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// rustfmt-error_on_line_overflow: false
44
// rustfmt-struct_lit_style: Visual
55
fn foo() {
6-
76
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(f(), b());
87

98
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(// Comment
@@ -35,5 +34,4 @@ fn foo() {
3534
// /|\ \
3635
// o o o o
3736
G)
38-
3937
}

tests/target/fn-simple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,4 @@ pub fn waltz(cwd: &Path) -> CliAssert {
9696
rewrite_comment(comment, block_style, width, offset, formatting_fig);
9797
}
9898
}
99-
10099
}

tests/target/indent_match_arms.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@ fn main() {
2323
},
2424
_ => "something else",
2525
}
26-
2726
}

tests/target/issue-510.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ impl ISizeAndMarginsComputer for AbsoluteNonReplaced {
44
block: &mut BlockFlow,
55
input: &ISizeConstraintInput,
66
) -> ISizeConstraintSolution {
7-
87
let (inline_start, inline_size, margin_inline_start, margin_inline_end) = match (
98
inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx,
109
inline_startssssssxxxxxxsssssxxxxxxxxxssssssxxx,

tests/target/remove_blank_lines.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let x = 1;
3+
}
4+
5+
fn foo() {
6+
#![attribute]
7+
8+
let x = 1;
9+
10+
// comment
11+
}

tests/target/spaces-within-angle-brackets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ fn foo< T, E >(a: T, b: E) {
3131
}
3232

3333
fn foo< T: Send, E: Send >(a: T, b: E) {
34-
3534
foo::< u32, str >(10, "bar");
3635

3736
let opt: Option< u32 >;

tests/target/spaces-within-parens.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ struct TupleStruct2( u32, u32 );
1414
fn fooEmpty() {}
1515

1616
fn foo( e: E, _: u32 ) -> ( u32, u32 ) {
17-
1817
// Tuples
1918
let t1 = ();
2019
let t2 = ( 1, );

tests/target/spaces-within-square-brackets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// rustfmt-spaces_within_square_brackets: true
22

33
fn main() {
4-
54
let arr: [ i32; 5 ] = [ 1, 2, 3, 4, 5 ];
65
let arr: [ i32; 500 ] = [ 0; 500 ];
76

tests/target/struct_tuple_visual.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// rustfmt-error_on_line_overflow: false
44
// rustfmt-struct_lit_style: Visual
55
fn foo() {
6-
76
Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(f(), b());
87

98
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo(
@@ -45,5 +44,4 @@ fn foo() {
4544
// o o o o
4645
G,
4746
)
48-
4947
}

0 commit comments

Comments
 (0)