33use clap:: Parser ;
44use ignore:: WalkBuilder ;
55use ignore:: gitignore:: GitignoreBuilder ;
6- use regex:: Regex ;
76use rubyfmt:: init_logger;
87use similar:: TextDiff ;
98use std:: ffi:: OsStr ;
109use std:: fs:: { File , OpenOptions , read} ;
1110use std:: io:: { self , BufRead , BufReader , IsTerminal , Read , Write } ;
1211use std:: path:: Path ;
1312use std:: process:: { Command , exit} ;
14- use std:: sync:: { Arc , LazyLock , Mutex } ;
15-
16- static MAGIC_COMMENT_REGEX : LazyLock < Regex > =
17- LazyLock :: new ( || Regex :: new ( r"(?m)^#\s*rubyfmt:\s*(?P<enabled>true|false)\s*$" ) . unwrap ( ) ) ;
13+ use std:: sync:: { Arc , Mutex } ;
1814
1915/// Simple Enum to exit on errors or not
2016#[ derive( Debug , PartialEq , Copy , Clone ) ]
@@ -164,10 +160,7 @@ fn rubyfmt_string(
164160 let slice_size = buffer. len ( ) . min ( 500 ) ;
165161 let slice = String :: from_utf8_lossy ( & buffer[ ..slice_size] ) ;
166162
167- let matched = MAGIC_COMMENT_REGEX
168- . captures ( & slice)
169- . and_then ( |c| c. name ( "enabled" ) )
170- . map ( |s| s. as_str ( ) ) ;
163+ let matched = parse_magic_comment ( & slice) ;
171164
172165 // If opted in to magic "# rubyfmt: true" header and true is not
173166 // in the file, return early
@@ -189,6 +182,22 @@ fn rubyfmt_string(
189182/* Helpers */
190183/******************************************************/
191184
185+ /// Scan for a `# rubyfmt: true` or `# rubyfmt: false` magic comment
186+ fn parse_magic_comment ( slice : & str ) -> Option < & str > {
187+ for line in slice. lines ( ) {
188+ if let Some ( rest) = line. strip_prefix ( '#' ) {
189+ let rest = rest. trim_start ( ) ;
190+ if let Some ( rest) = rest. strip_prefix ( "rubyfmt:" ) {
191+ let val = rest. trim ( ) ;
192+ if val == "true" || val == "false" {
193+ return Some ( val) ;
194+ }
195+ }
196+ }
197+ }
198+ None
199+ }
200+
192201/// Check if a path should be ignored based on .gitignore and .rubyfmtignore patterns.
193202/// The path should be relative to the current working directory.
194203fn is_path_ignored ( path : & Path , include_gitignored : bool ) -> bool {
0 commit comments