Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option generated_file_header_size #5659

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1050,15 +1050,23 @@ 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.)

- **Default value**: `true`
- **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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reword this to be clearer about when this option has an effect:

Number of lines to check for a @generated pragma header when format_generated_files is false. 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))
Copy link
Contributor Author

@captbaritone captbaritone Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these two config options be promoted to stable together? Or should I create a new issue to track promoting this option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's would be fine to stabilize these at the same time.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a See also [format_generated_files](#format_generated_files) link here.

## `format_macro_matchers`

Format the metavariable matching patterns in macros.
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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";
Expand Down Expand Up @@ -667,6 +669,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
Expand Down
2 changes: 1 addition & 1 deletion src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.generated_file_header_size()) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/formatting/generated.rs
Original file line number Diff line number Diff line change
@@ -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"))
}