From 0f2e6593dd239a31be524d1c9b2587b5cc3ba8fe Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Fri, 13 Jan 2023 21:21:29 -0800 Subject: [PATCH 1/3] Add config option `generated_file_header_size` to specify how many lines to look at when checking for @generated --- Configurations.md | 12 ++++++++++-- src/config/mod.rs | 3 +++ src/formatting.rs | 2 +- src/formatting/generated.rs | 4 ++-- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Configurations.md b/Configurations.md index c1e967c85da..52b236e00fc 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1050,8 +1050,8 @@ Max width for code snippets included in doc comments. Only used if [`format_code ## `format_generated_files` -Format generated files. A file is considered generated -if any of the first five lines contain a `@generated` comment marker. +Format generated files. A file is considered generated if any of the first several lines contain a `@generated` comment marker. The number of lines to check is configured by `generated_file_header_size`. + By default, generated files are reformatted, i. e. `@generated` marker is ignored. This option is currently ignored for stdin (`@generated` in stdin is ignored.) @@ -1059,6 +1059,14 @@ This option is currently ignored for stdin (`@generated` in stdin is ignored.) - **Possible values**: `true`, `false` - **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080)) +## `generated_file_header_size` + +Number of lines to check for a `@generated` pragma header when `format_generated_files` is enabled. When `format_generated_files` is disabled, this option has no effect. + +- **Default value**: `5` +- **Possible values**: any positive integer +- **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080)) + ## `format_macro_matchers` Format the metavariable matching patterns in macros. diff --git a/src/config/mod.rs b/src/config/mod.rs index 2b38b616e29..00e3cbaa3b4 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -151,6 +151,8 @@ create_config! { "Write an item and its attribute on the same line \ if their combined width is below a threshold"; format_generated_files: bool, true, false, "Format generated files"; + generated_file_header_size: usize, 5, false, "Number of lines to check for a `@generated` \ + marker when `format_generated_files` is enabled"; // Options that can change the source code beyond whitespace/blocks (somewhat linty things) merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one"; @@ -680,6 +682,7 @@ edition = "2015" version = "One" inline_attribute_width = 0 format_generated_files = true +generated_file_header_size = 5 merge_derives = true use_try_shorthand = false use_field_init_shorthand = false diff --git a/src/formatting.rs b/src/formatting.rs index cd57a025b67..b29467d460b 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -82,7 +82,7 @@ fn should_skip_module( let source_file = context.parse_session.span_to_file_contents(module.span); let src = source_file.src.as_ref().expect("SourceFile without src"); - if is_generated_file(src) { + if is_generated_file(src, config.generated_file_header_size()) { return true; } } diff --git a/src/formatting/generated.rs b/src/formatting/generated.rs index 58f43f17ee1..7f2727dc92d 100644 --- a/src/formatting/generated.rs +++ b/src/formatting/generated.rs @@ -1,7 +1,7 @@ /// Returns `true` if the given span is a part of generated files. -pub(super) fn is_generated_file(original_snippet: &str) -> bool { +pub(super) fn is_generated_file(original_snippet: &str, header_size: usize) -> bool { original_snippet .lines() - .take(5) // looking for marker only in the beginning of the file + .take(header_size) // looking for marker only in the beginning of the file .any(|line| line.contains("@generated")) } From 3bc09b6e86eaba3ab311a2a5ed8b26d69dc6b4bd Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 18 Jan 2023 16:47:31 -0800 Subject: [PATCH 2/3] Add tests --- .../false_with_generated_file_header_size.rs | 10 ++++++++++ .../true_with_marker_not_in_header.rs | 9 +++++++++ .../false_with_generated_file_header_size.rs | 10 ++++++++++ .../true_with_marker_not_in_header.rs | 7 +++++++ 4 files changed, 36 insertions(+) create mode 100644 tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs create mode 100644 tests/source/configs/format_generated_files/true_with_marker_not_in_header.rs create mode 100644 tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs create mode 100644 tests/target/configs/format_generated_files/true_with_marker_not_in_header.rs diff --git a/tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs b/tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs new file mode 100644 index 00000000000..a121e0b40f0 --- /dev/null +++ b/tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs @@ -0,0 +1,10 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_file_header_size: 15 + +fn main() +{ + println!("hello, world") + ; +} + +// @generated \ No newline at end of file diff --git a/tests/source/configs/format_generated_files/true_with_marker_not_in_header.rs b/tests/source/configs/format_generated_files/true_with_marker_not_in_header.rs new file mode 100644 index 00000000000..8eff1a3b0ee --- /dev/null +++ b/tests/source/configs/format_generated_files/true_with_marker_not_in_header.rs @@ -0,0 +1,9 @@ +// rustfmt-format_generated_files: true + +fn main() +{ + println!("hello, world") + ; +} + +// @generated \ No newline at end of file diff --git a/tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs b/tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs new file mode 100644 index 00000000000..a121e0b40f0 --- /dev/null +++ b/tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs @@ -0,0 +1,10 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_file_header_size: 15 + +fn main() +{ + println!("hello, world") + ; +} + +// @generated \ No newline at end of file diff --git a/tests/target/configs/format_generated_files/true_with_marker_not_in_header.rs b/tests/target/configs/format_generated_files/true_with_marker_not_in_header.rs new file mode 100644 index 00000000000..3b6280b350e --- /dev/null +++ b/tests/target/configs/format_generated_files/true_with_marker_not_in_header.rs @@ -0,0 +1,7 @@ +// rustfmt-format_generated_files: true + +fn main() { + println!("hello, world"); +} + +// @generated From 1ca738e5aee805505c727a10c977ddbd31659ece Mon Sep 17 00:00:00 2001 From: IVIURARY Date: Tue, 16 Jan 2024 20:39:57 +0000 Subject: [PATCH 3/3] change config name to generated_marker_line_search_limit change config name to generated_marker_line_search_limit update docs fix formatting use config variable add 0 limit docs adding zero limit search --- Configurations.md | 8 +++++--- src/config/mod.rs | 6 +++--- src/formatting.rs | 2 +- src/formatting/generated.rs | 7 +++++-- .../false_with_generated_marker_line_search_limit.rs} | 2 +- ...der_size.rs => false_with_marker_out_scope_size.rs} | 2 +- .../false_with_zero_search_limit.rs | 9 +++++++++ .../true_with_marker_in_scope_size.rs | 10 ++++++++++ .../false_with_generated_marker_line_search_limit.rs | 10 ++++++++++ .../false_with_marker_out_scope_size.rs | 8 ++++++++ .../false_with_zero_search_limit.rs | 8 ++++++++ .../true_with_marker_in_scope_size.rs | 8 ++++++++ 12 files changed, 69 insertions(+), 11 deletions(-) rename tests/{target/configs/format_generated_files/false_with_generated_file_header_size.rs => source/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs} (67%) rename tests/source/configs/format_generated_files/{false_with_generated_file_header_size.rs => false_with_marker_out_scope_size.rs} (68%) create mode 100644 tests/source/configs/format_generated_files/false_with_zero_search_limit.rs create mode 100644 tests/source/configs/format_generated_files/true_with_marker_in_scope_size.rs create mode 100644 tests/target/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs create mode 100644 tests/target/configs/format_generated_files/false_with_marker_out_scope_size.rs create mode 100644 tests/target/configs/format_generated_files/false_with_zero_search_limit.rs create mode 100644 tests/target/configs/format_generated_files/true_with_marker_in_scope_size.rs diff --git a/Configurations.md b/Configurations.md index 52b236e00fc..af7fa012063 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1050,7 +1050,7 @@ Max width for code snippets included in doc comments. Only used if [`format_code ## `format_generated_files` -Format generated files. A file is considered generated if any of the first several lines contain a `@generated` comment marker. The number of lines to check is configured by `generated_file_header_size`. +Format generated files. A file is considered generated if any of the first several lines contain a `@generated` comment marker. The number of lines to check is configured by `generated_marker_line_search_limit`. By default, generated files are reformatted, i. e. `@generated` marker is ignored. This option is currently ignored for stdin (`@generated` in stdin is ignored.) @@ -1059,14 +1059,16 @@ This option is currently ignored for stdin (`@generated` in stdin is ignored.) - **Possible values**: `true`, `false` - **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080)) -## `generated_file_header_size` +## `generated_marker_line_search_limit` -Number of lines to check for a `@generated` pragma header when `format_generated_files` is enabled. When `format_generated_files` is disabled, this option has no effect. +Number of lines to check for a `@generated` pragma header, starting from the top of the file. Setting this value to `0` will treat all files as non-generated. When`format_generated_files` is `true`, this option has no effect. - **Default value**: `5` - **Possible values**: any positive integer - **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080)) +See also [format_generated_files](#format_generated_files) link here. + ## `format_macro_matchers` Format the metavariable matching patterns in macros. diff --git a/src/config/mod.rs b/src/config/mod.rs index 00e3cbaa3b4..c7c2d4e48a8 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -151,8 +151,8 @@ create_config! { "Write an item and its attribute on the same line \ if their combined width is below a threshold"; format_generated_files: bool, true, false, "Format generated files"; - generated_file_header_size: usize, 5, false, "Number of lines to check for a `@generated` \ - marker when `format_generated_files` is enabled"; + generated_marker_line_search_limit: usize, 5, false, "Number of lines to check for a \ + `@generated` marker when `format_generated_files` is enabled"; // Options that can change the source code beyond whitespace/blocks (somewhat linty things) merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one"; @@ -682,7 +682,7 @@ edition = "2015" version = "One" inline_attribute_width = 0 format_generated_files = true -generated_file_header_size = 5 +generated_marker_line_search_limit = 5 merge_derives = true use_try_shorthand = false use_field_init_shorthand = false diff --git a/src/formatting.rs b/src/formatting.rs index b29467d460b..60361505a3f 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -82,7 +82,7 @@ fn should_skip_module( let source_file = context.parse_session.span_to_file_contents(module.span); let src = source_file.src.as_ref().expect("SourceFile without src"); - if is_generated_file(src, config.generated_file_header_size()) { + if is_generated_file(src, config) { return true; } } diff --git a/src/formatting/generated.rs b/src/formatting/generated.rs index 7f2727dc92d..68ffee6e664 100644 --- a/src/formatting/generated.rs +++ b/src/formatting/generated.rs @@ -1,7 +1,10 @@ +use crate::Config; + /// Returns `true` if the given span is a part of generated files. -pub(super) fn is_generated_file(original_snippet: &str, header_size: usize) -> bool { +pub(super) fn is_generated_file(original_snippet: &str, config: &Config) -> bool { original_snippet .lines() - .take(header_size) // looking for marker only in the beginning of the file + // looking for marker only in the beginning of the file + .take(config.generated_marker_line_search_limit()) .any(|line| line.contains("@generated")) } diff --git a/tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs b/tests/source/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs similarity index 67% rename from tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs rename to tests/source/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs index a121e0b40f0..be03849fe10 100644 --- a/tests/target/configs/format_generated_files/false_with_generated_file_header_size.rs +++ b/tests/source/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs @@ -1,5 +1,5 @@ // rustfmt-format_generated_files: false -// rustfmt-generated_file_header_size: 15 +// rustfmt-generated_marker_line_search_limit: 15 fn main() { diff --git a/tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs b/tests/source/configs/format_generated_files/false_with_marker_out_scope_size.rs similarity index 68% rename from tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs rename to tests/source/configs/format_generated_files/false_with_marker_out_scope_size.rs index a121e0b40f0..abb97e6259f 100644 --- a/tests/source/configs/format_generated_files/false_with_generated_file_header_size.rs +++ b/tests/source/configs/format_generated_files/false_with_marker_out_scope_size.rs @@ -1,5 +1,5 @@ // rustfmt-format_generated_files: false -// rustfmt-generated_file_header_size: 15 +// rustfmt-generated_marker_line_search_limit: 1 fn main() { diff --git a/tests/source/configs/format_generated_files/false_with_zero_search_limit.rs b/tests/source/configs/format_generated_files/false_with_zero_search_limit.rs new file mode 100644 index 00000000000..f6978d0f355 --- /dev/null +++ b/tests/source/configs/format_generated_files/false_with_zero_search_limit.rs @@ -0,0 +1,9 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_marker_line_search_limit: 0 + +// @generated +fn main() { + println!("hello, world") + ; +} +// @generated diff --git a/tests/source/configs/format_generated_files/true_with_marker_in_scope_size.rs b/tests/source/configs/format_generated_files/true_with_marker_in_scope_size.rs new file mode 100644 index 00000000000..53cc116ec2b --- /dev/null +++ b/tests/source/configs/format_generated_files/true_with_marker_in_scope_size.rs @@ -0,0 +1,10 @@ +// rustfmt-format_generated_files: true +// rustfmt-generated_marker_line_search_limit: 20 + +fn main() +{ + println!("hello, world") + ; +} + +// @generated diff --git a/tests/target/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs b/tests/target/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs new file mode 100644 index 00000000000..be03849fe10 --- /dev/null +++ b/tests/target/configs/format_generated_files/false_with_generated_marker_line_search_limit.rs @@ -0,0 +1,10 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_marker_line_search_limit: 15 + +fn main() +{ + println!("hello, world") + ; +} + +// @generated \ No newline at end of file diff --git a/tests/target/configs/format_generated_files/false_with_marker_out_scope_size.rs b/tests/target/configs/format_generated_files/false_with_marker_out_scope_size.rs new file mode 100644 index 00000000000..93405896a1d --- /dev/null +++ b/tests/target/configs/format_generated_files/false_with_marker_out_scope_size.rs @@ -0,0 +1,8 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_marker_line_search_limit: 1 + +fn main() { + println!("hello, world"); +} + +// @generated diff --git a/tests/target/configs/format_generated_files/false_with_zero_search_limit.rs b/tests/target/configs/format_generated_files/false_with_zero_search_limit.rs new file mode 100644 index 00000000000..6acd46ed2eb --- /dev/null +++ b/tests/target/configs/format_generated_files/false_with_zero_search_limit.rs @@ -0,0 +1,8 @@ +// rustfmt-format_generated_files: false +// rustfmt-generated_marker_line_search_limit: 0 + +// @generated +fn main() { + println!("hello, world"); +} +// @generated diff --git a/tests/target/configs/format_generated_files/true_with_marker_in_scope_size.rs b/tests/target/configs/format_generated_files/true_with_marker_in_scope_size.rs new file mode 100644 index 00000000000..03bfdfd1c59 --- /dev/null +++ b/tests/target/configs/format_generated_files/true_with_marker_in_scope_size.rs @@ -0,0 +1,8 @@ +// rustfmt-format_generated_files: true +// rustfmt-generated_marker_line_search_limit: 20 + +fn main() { + println!("hello, world"); +} + +// @generated