diff --git a/Cargo.lock b/Cargo.lock index fafb8064..089cef76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,6 @@ dependencies = [ "ctrlc", "ignore", "libtest-mimic", - "regex", "rubyfmt", "similar", "tempfile", diff --git a/Cargo.toml b/Cargo.toml index 6d37e1bf..8ec2ba41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ edition = "2024" clap = { version = "4.5.32", features = ["derive"] } ctrlc = { version = "3.2", features = ["termination"] } ignore = "0.4.18" -regex = "1.11.1" rubyfmt = { path = "./librubyfmt" } similar = { version = "2.7.0", features = ["bytes"] } diff --git a/src/main.rs b/src/main.rs index 3f7fea12..1c6aeb06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ use clap::Parser; use ignore::WalkBuilder; use ignore::gitignore::GitignoreBuilder; -use regex::Regex; use rubyfmt::init_logger; use similar::TextDiff; use std::ffi::OsStr; @@ -11,10 +10,7 @@ use std::fs::{File, OpenOptions, read}; use std::io::{self, BufRead, BufReader, IsTerminal, Read, Write}; use std::path::Path; use std::process::{Command, exit}; -use std::sync::{Arc, LazyLock, Mutex}; - -static MAGIC_COMMENT_REGEX: LazyLock = - LazyLock::new(|| Regex::new(r"(?m)^#\s*rubyfmt:\s*(?Ptrue|false)\s*$").unwrap()); +use std::sync::{Arc, Mutex}; /// Simple Enum to exit on errors or not #[derive(Debug, PartialEq, Copy, Clone)] @@ -164,10 +160,7 @@ fn rubyfmt_string( let slice_size = buffer.len().min(500); let slice = String::from_utf8_lossy(&buffer[..slice_size]); - let matched = MAGIC_COMMENT_REGEX - .captures(&slice) - .and_then(|c| c.name("enabled")) - .map(|s| s.as_str()); + let matched = parse_magic_comment(&slice); // If opted in to magic "# rubyfmt: true" header and true is not // in the file, return early @@ -189,6 +182,22 @@ fn rubyfmt_string( /* Helpers */ /******************************************************/ +/// Scan for a `# rubyfmt: true` or `# rubyfmt: false` magic comment +fn parse_magic_comment(slice: &str) -> Option<&str> { + for line in slice.lines() { + if let Some(rest) = line.strip_prefix('#') { + let rest = rest.trim_start(); + if let Some(rest) = rest.strip_prefix("rubyfmt:") { + let val = rest.trim(); + if val == "true" || val == "false" { + return Some(val); + } + } + } + } + None +} + /// Check if a path should be ignored based on .gitignore and .rubyfmtignore patterns. /// The path should be relative to the current working directory. fn is_path_ignored(path: &Path, include_gitignored: bool) -> bool {