Skip to content

Commit

Permalink
Merge pull request #68 from magic-akari/master
Browse files Browse the repository at this point in the history
  • Loading branch information
shssoichiro authored Dec 11, 2024
2 parents 244ecb6 + cfafdb8 commit b5b483f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ categories = ["development-tools"]
[dependencies]
nom = "7.0.0"
unicode_categories = "0.1.1"
once_cell = "1"
regex = "1.6"

[dev-dependencies]
criterion = "0.4"
Expand Down
48 changes: 40 additions & 8 deletions src/formatter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use regex::Regex;
use std::borrow::Cow;

use crate::indentation::Indentation;
Expand All @@ -7,14 +6,47 @@ use crate::params::Params;
use crate::tokenizer::{Token, TokenKind};
use crate::{FormatOptions, QueryParams};

use once_cell::sync::Lazy;

static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)^(--|/\*)\s*fmt\s*:\s*(off|on)").unwrap());

// -- fmt: off
// -- fmt: on
pub(crate) fn check_fmt_off(s: &str) -> Option<bool> {
RE.captures(s)?
.get(2)
.map(|matched| matched.as_str().eq_ignore_ascii_case("off"))
let mut state = 0;

const ON: bool = false;
const OFF: bool = true;

const NEXT: u8 = 1;
const STAY: u8 = 0;

// SPACE SPACE SPACE n
// ┌┐ ┌┐ ┌┐ ┌───────────► ON
// - - ▼ f m t ▼ : ▼ o │ N
// 0 ───► 1 ───► 2 ───► 3 ───► 4 ───► 5 ───► 6 ───► 7
// F M T O │ f f
// └────► 8 ───► OFF
// F F
for c in s.bytes() {
state += match (state, c) {
(0 | 1, b'-') => NEXT,
(2, b' ') => STAY,
(2, b'f' | b'F') => NEXT,
(3, b'm' | b'M') => NEXT,
(4, b't' | b'T') => NEXT,
(5, b' ') => STAY,
(5, b':') => NEXT,
(6, b' ') => STAY,
(6, b'o' | b'O') => NEXT,
(7, b'n' | b'N') => {
return Some(ON);
}
(7, b'f' | b'F') => NEXT,
(8, b'f' | b'F') => {
return Some(OFF);
}
_ => return None,
};
}

None
}

pub(crate) fn format(
Expand Down

0 comments on commit b5b483f

Please sign in to comment.