From 673535f53df4926689d856822ba5bb87f1c4d9ab Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Fri, 13 Jan 2023 21:21:29 -0800 Subject: [PATCH] Add config option `generated_file_header_size` to specify how many lines to look at when checking for @generated --- src/config/mod.rs | 3 +++ src/formatting.rs | 2 +- src/formatting/generated.rs | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 14f27f3f8b6..481d99a57f1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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"; @@ -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 diff --git a/src/formatting.rs b/src/formatting.rs index 1dfd8a514f0..6eee40885f9 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")) }