@@ -104,32 +104,39 @@ def format_code(
104104 formatter_cmds : list [str ],
105105 path : Union [str , Path ],
106106 optimized_function : str = "" ,
107+ check_diff : bool = False , # noqa
107108 print_status : bool = True , # noqa
108109) -> str :
109110 with tempfile .TemporaryDirectory () as test_dir_str :
110- max_diff_lines = 100
111-
112111 if isinstance (path , str ):
113112 path = Path (path )
114113
115114 original_code = path .read_text (encoding = "utf8" )
116- # we dont' count the formatting diff for the optimized function as it should be well-formatted
117- original_code_without_opfunc = original_code .replace (optimized_function , "" )
118-
119- original_temp = Path (test_dir_str ) / "original_temp.py"
120- original_temp .write_text (original_code_without_opfunc , encoding = "utf8" )
121-
122- formatted_temp , formatted_code = apply_formatter_cmds (
123- formatter_cmds , original_temp , test_dir_str , print_status = False
124- )
125-
126- diff_output = generate_unified_diff (
127- original_code_without_opfunc , formatted_code , from_file = str (original_temp ), to_file = str (formatted_temp )
128- )
129- diff_lines_count = get_diff_lines_count (diff_output )
130- if diff_lines_count > max_diff_lines :
131- logger .debug (f"Skipping formatting { path } : { diff_lines_count } lines would change (max: { max_diff_lines } )" )
132- return original_code
115+ original_code_lines = len (original_code .split ("\n " ))
116+
117+ if check_diff and original_code_lines > 50 :
118+ # we dont' count the formatting diff for the optimized function as it should be well-formatted
119+ original_code_without_opfunc = original_code .replace (optimized_function , "" )
120+
121+ original_temp = Path (test_dir_str ) / "original_temp.py"
122+ original_temp .write_text (original_code_without_opfunc , encoding = "utf8" )
123+
124+ formatted_temp , formatted_code = apply_formatter_cmds (
125+ formatter_cmds , original_temp , test_dir_str , print_status = False
126+ )
127+
128+ diff_output = generate_unified_diff (
129+ original_code_without_opfunc , formatted_code , from_file = str (original_temp ), to_file = str (formatted_temp )
130+ )
131+ diff_lines_count = get_diff_lines_count (diff_output )
132+
133+ max_diff_lines = min (int (original_code_lines * 0.3 ), 50 )
134+
135+ if diff_lines_count > max_diff_lines and max_diff_lines != - 1 :
136+ logger .debug (
137+ f"Skipping formatting { path } : { diff_lines_count } lines would change (max: { max_diff_lines } )"
138+ )
139+ return original_code
133140
134141 _ , formatted_code = apply_formatter_cmds (formatter_cmds , path , test_dir_str = None , print_status = print_status )
135142 logger .debug (f"Formatted { path } with commands: { formatter_cmds } " )
0 commit comments