Skip to content

Commit 5e88444

Browse files
committed
visitor: Handle specified line ranges in visit_stmt
This commit adds a very rough implementation of handling the specified line ranges in `config.file_lines_map` for statements. It reformats a statement if its span is fully contained in the set of lines specified for the file. The implementation here is intended as a proof of concept, and demonstration that the machinery added in the preceding commits is functional. A final implementation would likely hook in via the `Rewrite` trait. Refs #434
1 parent 4d2695d commit 5e88444

9 files changed

+242
-1
lines changed

src/visitor.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use strings::string_buffer::StringBuffer;
1616

1717
use Indent;
1818
use utils;
19-
use codemap::SpanUtils;
19+
use codemap::{LineRangeUtils, SpanUtils};
2020
use config::Config;
2121
use rewrite::{Rewrite, RewriteContext};
2222
use comment::rewrite_comment;
@@ -47,6 +47,11 @@ impl<'a> FmtVisitor<'a> {
4747
self.codemap.lookup_char_pos(stmt.span.lo),
4848
self.codemap.lookup_char_pos(stmt.span.hi));
4949

50+
// FIXME(#434): Move this check to somewhere more central, eg Rewrite.
51+
if !self.config.file_lines.contains(&self.codemap.lookup_line_range(stmt.span)) {
52+
return;
53+
}
54+
5055
match stmt.node {
5156
ast::StmtKind::Decl(ref decl, _) => {
5257
if let ast::DeclKind::Item(ref item) = decl.node {

tests/source/file-lines-1.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-1.rs","range":[4,8]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call().method_call();
9+
10+
let y = if cond {
11+
val1
12+
} else {
13+
val2
14+
}
15+
.method_call();
16+
17+
{
18+
match x {
19+
PushParam => {
20+
// params are 1-indexed
21+
stack.push(mparams[match cur.to_digit(10) {
22+
Some(d) => d as usize - 1,
23+
None => return Err("bad param number".to_owned()),
24+
}]
25+
.clone());
26+
}
27+
}
28+
}
29+
}

tests/source/file-lines-2.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-2.rs","range":[10,15]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call().method_call();
9+
10+
let y = if cond {
11+
val1
12+
} else {
13+
val2
14+
}
15+
.method_call();
16+
17+
{
18+
match x {
19+
PushParam => {
20+
// params are 1-indexed
21+
stack.push(mparams[match cur.to_digit(10) {
22+
Some(d) => d as usize - 1,
23+
None => return Err("bad param number".to_owned()),
24+
}]
25+
.clone());
26+
}
27+
}
28+
}
29+
}

tests/source/file-lines-3.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-3.rs","range":[4,8]},{"file":"tests/source/file-lines-3.rs","range":[10,15]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call().method_call();
9+
10+
let y = if cond {
11+
val1
12+
} else {
13+
val2
14+
}
15+
.method_call();
16+
17+
{
18+
match x {
19+
PushParam => {
20+
// params are 1-indexed
21+
stack.push(mparams[match cur.to_digit(10) {
22+
Some(d) => d as usize - 1,
23+
None => return Err("bad param number".to_owned()),
24+
}]
25+
.clone());
26+
}
27+
}
28+
}
29+
}

tests/source/file-lines-4.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-file_lines: []
2+
// (Test that nothing is formatted if an empty array is specified.)
3+
4+
fn floaters() {
5+
let x = Foo {
6+
field1: val1,
7+
field2: val2,
8+
}
9+
.method_call().method_call();
10+
11+
let y = if cond {
12+
val1
13+
} else {
14+
val2
15+
}
16+
.method_call();
17+
18+
{
19+
match x {
20+
PushParam => {
21+
// params are 1-indexed
22+
stack.push(mparams[match cur.to_digit(10) {
23+
Some(d) => d as usize - 1,
24+
None => return Err("bad param number".to_owned()),
25+
}]
26+
.clone());
27+
}
28+
}
29+
}
30+
}

tests/target/file-lines-1.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-1.rs","range":[4,8]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call()
9+
.method_call();
10+
11+
let y = if cond {
12+
val1
13+
} else {
14+
val2
15+
}
16+
.method_call();
17+
18+
{
19+
match x {
20+
PushParam => {
21+
// params are 1-indexed
22+
stack.push(mparams[match cur.to_digit(10) {
23+
Some(d) => d as usize - 1,
24+
None => return Err("bad param number".to_owned()),
25+
}]
26+
.clone());
27+
}
28+
}
29+
}
30+
}

tests/target/file-lines-2.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-2.rs","range":[10,15]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call().method_call();
9+
10+
let y = if cond {
11+
val1
12+
} else {
13+
val2
14+
}
15+
.method_call();
16+
17+
{
18+
match x {
19+
PushParam => {
20+
// params are 1-indexed
21+
stack.push(mparams[match cur.to_digit(10) {
22+
Some(d) => d as usize - 1,
23+
None => return Err("bad param number".to_owned()),
24+
}]
25+
.clone());
26+
}
27+
}
28+
}
29+
}

tests/target/file-lines-3.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-file_lines: [{"file":"tests/source/file-lines-3.rs","range":[4,8]},{"file":"tests/source/file-lines-3.rs","range":[10,15]}]
2+
3+
fn floaters() {
4+
let x = Foo {
5+
field1: val1,
6+
field2: val2,
7+
}
8+
.method_call()
9+
.method_call();
10+
11+
let y = if cond {
12+
val1
13+
} else {
14+
val2
15+
}
16+
.method_call();
17+
18+
{
19+
match x {
20+
PushParam => {
21+
// params are 1-indexed
22+
stack.push(mparams[match cur.to_digit(10) {
23+
Some(d) => d as usize - 1,
24+
None => return Err("bad param number".to_owned()),
25+
}]
26+
.clone());
27+
}
28+
}
29+
}
30+
}

tests/target/file-lines-4.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// rustfmt-file_lines: []
2+
// (Test that nothing is formatted if an empty array is specified.)
3+
4+
fn floaters() {
5+
let x = Foo {
6+
field1: val1,
7+
field2: val2,
8+
}
9+
.method_call().method_call();
10+
11+
let y = if cond {
12+
val1
13+
} else {
14+
val2
15+
}
16+
.method_call();
17+
18+
{
19+
match x {
20+
PushParam => {
21+
// params are 1-indexed
22+
stack.push(mparams[match cur.to_digit(10) {
23+
Some(d) => d as usize - 1,
24+
None => return Err("bad param number".to_owned()),
25+
}]
26+
.clone());
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)