Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
27 changes: 18 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
use clap::Parser;
use ignore::WalkBuilder;
use ignore::gitignore::GitignoreBuilder;
use regex::Regex;
use rubyfmt::init_logger;
use similar::TextDiff;
use std::ffi::OsStr;
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<Regex> =
LazyLock::new(|| Regex::new(r"(?m)^#\s*rubyfmt:\s*(?P<enabled>true|false)\s*$").unwrap());
use std::sync::{Arc, Mutex};

/// Simple Enum to exit on errors or not
#[derive(Debug, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down