Skip to content

Commit

Permalink
Simplify more regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 2, 2024
1 parent fda4319 commit d5979fd
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
1 change: 0 additions & 1 deletion notes.org
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#+title: tex-fmt
* TODO Simplify other regexes
2 changes: 1 addition & 1 deletion src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
24 changes: 11 additions & 13 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
22 changes: 8 additions & 14 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> = VERBATIMS
pub static ref VERBATIMS_BEGIN: Vec<String> = VERBATIMS
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap())
.collect();
pub static ref RE_VERBATIMS_END: Vec<Regex> = VERBATIMS
.iter()
.map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LISTS_BEGIN: Vec<Regex> = LISTS
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LISTS_END: Vec<Regex> = LISTS
.iter()
.map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap())
.map(|l| format!("\\begin{{{l}}}"))
.collect();
pub static ref VERBATIMS_END: Vec<String> =
VERBATIMS.iter().map(|l| format!("\\end{{{l}}}")).collect();
pub static ref LISTS_BEGIN: Vec<String> =
LISTS.iter().map(|l| format!("\\begin{{{l}}}")).collect();
pub static ref LISTS_END: Vec<String> =
LISTS.iter().map(|l| format!("\\end{{{l}}}")).collect();
pub static ref RE_ENV_BEGIN_SHARED_LINE: Regex =
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\begin\{)").unwrap();
pub static ref RE_ENV_END_SHARED_LINE: Regex =
Expand Down
2 changes: 1 addition & 1 deletion src/subs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d5979fd

Please sign in to comment.