@@ -5,6 +5,7 @@ use std::io::{self, Write};
55use std:: time:: { Duration , Instant } ;
66
77use rustc_ast:: ast;
8+ use rustc_ast:: AstLike ;
89use rustc_span:: Span ;
910
1011use self :: newline_style:: apply_newline_style;
@@ -13,9 +14,9 @@ use crate::config::{Config, FileName, Verbosity};
1314use crate :: formatting:: generated:: is_generated_file;
1415use crate :: issues:: BadIssueSeeker ;
1516use crate :: modules:: Module ;
16- use crate :: syntux :: parser:: { DirectoryOwnership , Parser , ParserError } ;
17- use crate :: syntux :: session:: ParseSess ;
18- use crate :: utils:: count_newlines;
17+ use crate :: parse :: parser:: { DirectoryOwnership , Parser , ParserError } ;
18+ use crate :: parse :: session:: ParseSess ;
19+ use crate :: utils:: { contains_skip , count_newlines} ;
1920use crate :: visitor:: FmtVisitor ;
2021use crate :: { modules, source_file, ErrorKind , FormatReport , Input , Session } ;
2122
@@ -58,6 +59,39 @@ impl<'b, T: Write + 'b> Session<'b, T> {
5859 }
5960}
6061
62+ /// Determine if a module should be skipped. True if the module should be skipped, false otherwise.
63+ fn should_skip_module < T : FormatHandler > (
64+ config : & Config ,
65+ context : & FormatContext < ' _ , T > ,
66+ input_is_stdin : bool ,
67+ main_file : & FileName ,
68+ path : & FileName ,
69+ module : & Module < ' _ > ,
70+ ) -> bool {
71+ if contains_skip ( module. attrs ( ) ) {
72+ return true ;
73+ }
74+
75+ if config. skip_children ( ) && path != main_file {
76+ return true ;
77+ }
78+
79+ if !input_is_stdin && context. ignore_file ( path) {
80+ return true ;
81+ }
82+
83+ if !config. format_generated_files ( ) {
84+ let source_file = context. parse_session . span_to_file_contents ( module. span ) ;
85+ let src = source_file. src . as_ref ( ) . expect ( "SourceFile without src" ) ;
86+
87+ if is_generated_file ( src) {
88+ return true ;
89+ }
90+ }
91+
92+ false
93+ }
94+
6195// Format an entire crate (or subset of the module tree).
6296fn format_project < T : FormatHandler > (
6397 input : Input ,
@@ -97,23 +131,19 @@ fn format_project<T: FormatHandler>(
97131 directory_ownership. unwrap_or ( DirectoryOwnership :: UnownedViaBlock ) ,
98132 !input_is_stdin && !config. skip_children ( ) ,
99133 )
100- . visit_crate ( & krate) ?;
134+ . visit_crate ( & krate) ?
135+ . into_iter ( )
136+ . filter ( |( path, module) | {
137+ !should_skip_module ( config, & context, input_is_stdin, & main_file, path, module)
138+ } )
139+ . collect :: < Vec < _ > > ( ) ;
101140
102141 timer = timer. done_parsing ( ) ;
103142
104143 // Suppress error output if we have to do any further parsing.
105144 context. parse_session . set_silent_emitter ( ) ;
106145
107146 for ( path, module) in files {
108- let source_file = context. parse_session . span_to_file_contents ( module. span ) ;
109- let src = source_file. src . as_ref ( ) . expect ( "SourceFile without src" ) ;
110-
111- let should_ignore = ( !input_is_stdin && context. ignore_file ( & path) )
112- || ( !config. format_generated_files ( ) && is_generated_file ( src) ) ;
113-
114- if ( config. skip_children ( ) && path != main_file) || should_ignore {
115- continue ;
116- }
117147 should_emit_verbose ( input_is_stdin, config, || println ! ( "Formatting {}" , path) ) ;
118148 context. format_file ( path, & module, is_macro_def) ?;
119149 }
0 commit comments