Skip to content

Commit

Permalink
Add config option generated_file_header_size to specify how many li…
Browse files Browse the repository at this point in the history
…nes to look at when checking for @generated
  • Loading branch information
captbaritone committed Jan 14, 2023
1 parent ee2bed9 commit 673535f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
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` \
pragma header when `format_generated_files` is true";

// 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"))
}

0 comments on commit 673535f

Please sign in to comment.