Skip to content

Commit 2c39e80

Browse files
authored
Skip formatting generated files (#4296)
1 parent 99efd73 commit 2c39e80

File tree

9 files changed

+87
-2
lines changed

9 files changed

+87
-2
lines changed

Configurations.md

+8
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,14 @@ fn add_one(x: i32) -> i32 {
961961
}
962962
```
963963

964+
## `format_generated_files`
965+
966+
Format generated files. A file is considered as generated if the file starts with `// @generated`.
967+
968+
- **Default value**: `false`
969+
- **Possible values**: `true`, `false`
970+
- **Stable**: No
971+
964972
## `format_macro_matchers`
965973

966974
Format the metavariable matching patterns in macros.

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ create_config! {
129129
inline_attribute_width: usize, 0, false,
130130
"Write an item and its attribute on the same line \
131131
if their combined width is below a threshold";
132+
format_generated_files: bool, false, false, "Format generated files";
132133

133134
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
134135
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
@@ -615,6 +616,7 @@ blank_lines_upper_bound = 1
615616
blank_lines_lower_bound = 0
616617
edition = "2018"
617618
inline_attribute_width = 0
619+
format_generated_files = false
618620
merge_derives = true
619621
use_try_shorthand = false
620622
use_field_init_shorthand = false

src/formatting.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) use syntux::session::ParseSess;
1010
use crate::config::{Config, FileName};
1111
use crate::formatting::{
1212
comment::{CharClasses, FullCodeCharKind},
13+
generated::is_generated_file,
1314
modules::{FileModMap, Module},
1415
newline_style::apply_newline_style,
1516
report::NonFormattedRange,
@@ -30,6 +31,7 @@ mod chains;
3031
mod closures;
3132
mod comment;
3233
mod expr;
34+
mod generated;
3335
mod imports;
3436
mod items;
3537
mod lists;
@@ -125,7 +127,10 @@ fn format_project(
125127
parse_session.set_silent_emitter();
126128

127129
for (path, module) in &files {
128-
let should_ignore = !input_is_stdin && parse_session.ignore_file(&path);
130+
let should_ignore = (!input_is_stdin && parse_session.ignore_file(&path))
131+
|| (!config.format_generated_files()
132+
&& is_generated_file(&path, original_snippet.as_ref()));
133+
129134
if (!operation_setting.recursive && path != &main_file) || should_ignore {
130135
continue;
131136
}

src/formatting/generated.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::{
2+
fs,
3+
io::{self, BufRead},
4+
};
5+
6+
use crate::config::file_lines::FileName;
7+
use crate::formatting::comment::contains_comment;
8+
9+
/// Returns `true` if the given span is a part of generated files.
10+
pub(super) fn is_generated_file(file_name: &FileName, original_snippet: Option<&String>) -> bool {
11+
let first_line = match file_name {
12+
FileName::Stdin => original_snippet
13+
.and_then(|s| s.lines().next())
14+
.map(str::to_owned)
15+
.unwrap_or("".to_owned()),
16+
FileName::Real(ref path) => fs::File::open(path)
17+
.ok()
18+
.and_then(|f| io::BufReader::new(f).lines().next()?.ok())
19+
.unwrap_or("".to_owned()),
20+
};
21+
22+
is_comment_with_generated_notation(&first_line)
23+
}
24+
25+
fn is_comment_with_generated_notation(s: &str) -> bool {
26+
contains_comment(&s) && s.contains("@generated")
27+
}
28+
29+
#[cfg(test)]
30+
mod test {
31+
#[test]
32+
fn is_comment_with_generated_notation() {
33+
use super::is_comment_with_generated_notation;
34+
35+
assert!(is_comment_with_generated_notation("// @generated"));
36+
assert!(is_comment_with_generated_notation("//@generated\n\n"));
37+
assert!(is_comment_with_generated_notation("\n// @generated"));
38+
assert!(is_comment_with_generated_notation("/* @generated"));
39+
}
40+
}

src/test/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ fn read_significant_comments(file_name: &Path) -> HashMap<String, String> {
744744
reader
745745
.lines()
746746
.map(|line| line.expect("failed getting line"))
747-
.take_while(|line| line_regex.is_match(line))
747+
.filter(|line| line_regex.is_match(line))
748748
.filter_map(|line| {
749749
regex.captures_iter(&line).next().map(|capture| {
750750
(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @generated
2+
// rustfmt-format_generated_files: false
3+
4+
fn main()
5+
{
6+
println!("hello, world")
7+
;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @generated
2+
// rustfmt-format_generated_files: true
3+
4+
fn main()
5+
{
6+
println!("hello, world")
7+
;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @generated
2+
// rustfmt-format_generated_files: false
3+
4+
fn main()
5+
{
6+
println!("hello, world")
7+
;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @generated
2+
// rustfmt-format_generated_files: true
3+
4+
fn main() {
5+
println!("hello, world");
6+
}

0 commit comments

Comments
 (0)