Add config option generated_marker_line_search_limit (#5993)

`generated_marker_line_search_limit` allows users to configure how many
lines rustfmt should search for an `@generated` marker comment when
`format_generated_files=false`

---------

Co-authored-by: Jordan Eldredge <jordan@jordaneldredge.com>
This commit is contained in:
IVIURRAY 2024-01-20 16:14:50 +00:00 committed by GitHub
parent 6356fca675
commit bf967319e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 110 additions and 5 deletions

View File

@ -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_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,6 +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_marker_line_search_limit`
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.

View File

@ -151,6 +151,8 @@
"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_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";
@ -680,6 +682,7 @@ fn test_dump_default_config() {
version = "One"
inline_attribute_width = 0
format_generated_files = true
generated_marker_line_search_limit = 5
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false

View File

@ -82,7 +82,7 @@ fn should_skip_module<T: FormatHandler>(
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) {
return true;
}
}

View File

@ -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) -> bool {
pub(super) fn is_generated_file(original_snippet: &str, config: &Config) -> bool {
original_snippet
.lines()
.take(5) // 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"))
}

View File

@ -0,0 +1,10 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 15
fn main()
{
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,10 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 1
fn main()
{
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,9 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 0
// @generated
fn main() {
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,10 @@
// rustfmt-format_generated_files: true
// rustfmt-generated_marker_line_search_limit: 20
fn main()
{
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,9 @@
// rustfmt-format_generated_files: true
fn main()
{
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,10 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 15
fn main()
{
println!("hello, world")
;
}
// @generated

View File

@ -0,0 +1,8 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 1
fn main() {
println!("hello, world");
}
// @generated

View File

@ -0,0 +1,8 @@
// rustfmt-format_generated_files: false
// rustfmt-generated_marker_line_search_limit: 0
// @generated
fn main() {
println!("hello, world");
}
// @generated

View File

@ -0,0 +1,8 @@
// rustfmt-format_generated_files: true
// rustfmt-generated_marker_line_search_limit: 20
fn main() {
println!("hello, world");
}
// @generated

View File

@ -0,0 +1,7 @@
// rustfmt-format_generated_files: true
fn main() {
println!("hello, world");
}
// @generated