@@ -36,7 +36,7 @@ struct Config {
3636}
3737
3838struct JobItem {
39- language : LANG ,
39+ language : Option < LANG > ,
4040 path : PathBuf ,
4141 cfg : Config ,
4242}
@@ -60,19 +60,24 @@ fn mk_globset(elems: clap::Values) -> GlobSet {
6060 }
6161}
6262
63- fn act_on_file ( language : LANG , path : PathBuf , cfg : Config ) -> std:: io:: Result < ( ) > {
63+ fn act_on_file ( language : Option < LANG > , path : PathBuf , cfg : Config ) -> std:: io:: Result < ( ) > {
64+ let source = read_file_with_eol ( & path) ?;
65+ let language = if let Some ( language) = language {
66+ language
67+ } else if let Some ( language) = guess_language ( & source, & path) . 0 {
68+ language
69+ } else {
70+ return Ok ( ( ) ) ;
71+ } ;
72+
6473 let pr = cfg. preproc ;
6574 if cfg. dump {
66- let source = read_file_with_eol ( & path) ?;
67- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
6875 let cfg = DumpCfg {
6976 line_start : cfg. line_start ,
7077 line_end : cfg. line_end ,
7178 } ;
7279 action :: < Dump > ( & language, source, & path, pr, cfg)
7380 } else if cfg. metrics {
74- let source = read_file_with_eol ( & path) ?;
75- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
7681 let cfg = MetricsCfg {
7782 path,
7883 output_path : if cfg. output . is_empty ( ) {
@@ -83,30 +88,19 @@ fn act_on_file(language: LANG, path: PathBuf, cfg: Config) -> std::io::Result<()
8388 } ;
8489 action :: < Metrics > ( & language, source, & cfg. path . clone ( ) , pr, cfg)
8590 } else if cfg. comments {
86- let source = read_file_with_eol ( & path) ?;
87- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
88- let lang = get_language_for_file ( & path) ;
8991 let cfg = CommentRmCfg {
9092 in_place : cfg. in_place ,
9193 path,
9294 } ;
93- if let Some ( lang) = lang {
94- if lang == LANG :: Cpp {
95- action :: < CommentRm > ( & LANG :: Ccomment , source, & cfg. path . clone ( ) , pr, cfg)
96- } else {
97- action :: < CommentRm > ( & language, source, & cfg. path . clone ( ) , pr, cfg)
98- }
95+ if language == LANG :: Cpp {
96+ action :: < CommentRm > ( & LANG :: Ccomment , source, & cfg. path . clone ( ) , pr, cfg)
9997 } else {
10098 action :: < CommentRm > ( & language, source, & cfg. path . clone ( ) , pr, cfg)
10199 }
102100 } else if cfg. function {
103- let source = read_file_with_eol ( & path) ?;
104- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
105101 let cfg = FunctionCfg { path : path. clone ( ) } ;
106102 action :: < Function > ( & language, source, & path, pr, cfg)
107103 } else if !cfg. find_filter . is_empty ( ) {
108- let source = read_file_with_eol ( & path) ?;
109- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
110104 let cfg = FindCfg {
111105 path : Some ( path. clone ( ) ) ,
112106 filters : cfg. find_filter ,
@@ -115,24 +109,20 @@ fn act_on_file(language: LANG, path: PathBuf, cfg: Config) -> std::io::Result<()
115109 } ;
116110 action :: < Find > ( & language, source, & path, pr, cfg)
117111 } else if cfg. count_lock . is_some ( ) {
118- let source = read_file_with_eol ( & path) ?;
119- let language = guess_language ( & source) . 0 . unwrap_or ( language) ;
120112 let cfg = CountCfg {
121113 path : Some ( path. clone ( ) ) ,
122114 filters : cfg. count_filter ,
123115 stats : cfg. count_lock . unwrap ( ) . clone ( ) ,
124116 } ;
125117 action :: < Count > ( & language, source, & path, pr, cfg)
126118 } else if cfg. preproc_lock . is_some ( ) {
127- if let Some ( lang) = get_language_for_file ( & path) {
128- if lang == LANG :: Cpp {
129- let source = read_file_with_eol ( & path) ?;
130- preprocess (
131- & PreprocParser :: new ( source, & path, None ) ,
132- & path,
133- cfg. preproc_lock . unwrap ( ) . clone ( ) ,
134- ) ;
135- }
119+ if language == LANG :: Cpp {
120+ let source = read_file_with_eol ( & path) ?;
121+ preprocess (
122+ & PreprocParser :: new ( source, & path, None ) ,
123+ & path,
124+ cfg. preproc_lock . unwrap ( ) . clone ( ) ,
125+ ) ;
136126 }
137127 Ok ( ( ) )
138128 } else {
@@ -154,21 +144,13 @@ fn consumer(receiver: JobReceiver) {
154144}
155145
156146fn send_file ( path : PathBuf , cfg : & Config , language : & Option < LANG > , sender : & JobSender ) {
157- let language = if language. is_none ( ) {
158- get_language_for_file ( & path)
159- } else {
160- language. clone ( )
161- } ;
162-
163- if let Some ( language) = language {
164- sender
165- . send ( Some ( JobItem {
166- language,
167- path,
168- cfg : cfg. clone ( ) ,
169- } ) )
170- . unwrap ( ) ;
171- }
147+ sender
148+ . send ( Some ( JobItem {
149+ language : language. clone ( ) ,
150+ path,
151+ cfg : cfg. clone ( ) ,
152+ } ) )
153+ . unwrap ( ) ;
172154}
173155
174156fn is_hidden ( entry : & DirEntry ) -> bool {
0 commit comments