diff --git a/notes.org b/notes.org index a959e23..707d903 100644 --- a/notes.org +++ b/notes.org @@ -1,2 +1 @@ #+title: tex-fmt -* TODO Simplify other regexes diff --git a/src/format.rs b/src/format.rs index 281a130..1d6832e 100644 --- a/src/format.rs +++ b/src/format.rs @@ -2,10 +2,10 @@ use crate::ignore::*; use crate::indent::*; -use crate::verbatim::*; use crate::logging::*; use crate::parse::*; use crate::subs::*; +use crate::verbatim::*; use crate::wrap::*; use crate::LINE_END; use log::Level::{Info, Warn}; diff --git a/src/indent.rs b/src/indent.rs index 9a183b0..a0fcb93 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -1,10 +1,10 @@ use crate::comments::*; use crate::format::*; use crate::ignore::*; -use crate::verbatim::*; use crate::logging::*; use crate::parse::*; use crate::regexes::*; +use crate::verbatim::*; use crate::TAB; use core::cmp::max; use log::Level::{Trace, Warn}; @@ -39,22 +39,20 @@ fn get_diff(line: &str) -> i8 { return 0; }; diff += 1; - for re_list_begin in RE_LISTS_BEGIN.iter() { - if re_list_begin.is_match(line) { - diff += 1; - }; - } + diff += i8::try_from( + LISTS_BEGIN.iter().filter(|&r| line.contains(r)).count(), + ) + .unwrap(); } else if line.contains(ENV_END) { // documents get no global indentation if line.contains(DOC_END) { return 0; }; diff -= 1; - for re_list_end in RE_LISTS_END.iter() { - if re_list_end.is_match(line) { - diff -= 1; - }; - } + diff -= i8::try_from( + LISTS_END.iter().filter(|&r| line.contains(r)).count(), + ) + .unwrap(); }; // indent for delimiters @@ -85,8 +83,8 @@ fn get_back(line: &str) -> i8 { return 0; }; // list environments get double indents for indenting items - for re_list_end in RE_LISTS_END.iter() { - if re_list_end.is_match(line) { + for r in LISTS_END.iter() { + if line.contains(r) { return 2; }; } diff --git a/src/main.rs b/src/main.rs index d236a19..bf6ceec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,11 +20,11 @@ mod comments; mod format; mod ignore; mod indent; -mod verbatim; mod logging; mod parse; mod regexes; mod subs; +mod verbatim; mod wrap; mod write; use crate::format::*; diff --git a/src/regexes.rs b/src/regexes.rs index 5d0def6..e83e3f2 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -23,22 +23,16 @@ lazy_static! { Regex::new(&format!(r"{LINE_END}{LINE_END}({LINE_END})+")).unwrap(); pub static ref RE_TRAIL: Regex = Regex::new(&format!(r" +{LINE_END}")).unwrap(); - pub static ref RE_VERBATIMS_BEGIN: Vec = VERBATIMS + pub static ref VERBATIMS_BEGIN: Vec = VERBATIMS .iter() - .map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap()) - .collect(); - pub static ref RE_VERBATIMS_END: Vec = VERBATIMS - .iter() - .map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap()) - .collect(); - pub static ref RE_LISTS_BEGIN: Vec = LISTS - .iter() - .map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap()) - .collect(); - pub static ref RE_LISTS_END: Vec = LISTS - .iter() - .map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap()) + .map(|l| format!("\\begin{{{l}}}")) .collect(); + pub static ref VERBATIMS_END: Vec = + VERBATIMS.iter().map(|l| format!("\\end{{{l}}}")).collect(); + pub static ref LISTS_BEGIN: Vec = + LISTS.iter().map(|l| format!("\\begin{{{l}}}")).collect(); + pub static ref LISTS_END: Vec = + LISTS.iter().map(|l| format!("\\end{{{l}}}")).collect(); pub static ref RE_ENV_BEGIN_SHARED_LINE: Regex = Regex::new(r"(?P\S.*?)(?P\\begin\{)").unwrap(); pub static ref RE_ENV_END_SHARED_LINE: Regex = diff --git a/src/subs.rs b/src/subs.rs index 8a63487..9eaf155 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -1,9 +1,9 @@ use crate::comments::*; use crate::format::*; use crate::ignore::*; -use crate::verbatim::*; use crate::logging::*; use crate::regexes::*; +use crate::verbatim::*; use crate::Cli; use crate::{LINE_END, TAB}; use log::Level::Info; diff --git a/src/verbatim.rs b/src/verbatim.rs index d281eb7..e683f50 100644 --- a/src/verbatim.rs +++ b/src/verbatim.rs @@ -46,11 +46,11 @@ pub fn get_verbatim( fn get_verbatim_diff(line: &str) -> i8 { if line.contains(ENV_BEGIN) - && RE_VERBATIMS_BEGIN.iter().any(|r| r.is_match(line)) + && VERBATIMS_BEGIN.iter().any(|r| line.contains(r)) { 1 } else if line.contains(ENV_END) - && RE_VERBATIMS_END.iter().any(|r| r.is_match(line)) + && VERBATIMS_END.iter().any(|r| line.contains(r)) { -1 } else {