Skip to content

Commit

Permalink
Rename "leave" to "verbatim"
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 2, 2024
1 parent 2c569e0 commit fda4319
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions notes.org
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#+title: tex-fmt
* TODO Simplify other regexes
6 changes: 3 additions & 3 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::ignore::*;
use crate::indent::*;
use crate::leave::*;
use crate::verbatim::*;
use crate::logging::*;
use crate::parse::*;
use crate::subs::*;
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct State {
/// Indentation status of the current line
pub indent: Indent,
/// Verbatim status of the current line
pub leave: Leave,
pub verbatim: Verbatim,
}

impl State {
Expand All @@ -89,7 +89,7 @@ impl State {
linum_new: 0,
ignore: Ignore::new(),
indent: Indent::new(),
leave: Leave::new(),
verbatim: Verbatim::new(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/indent.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::comments::*;
use crate::format::*;
use crate::ignore::*;
use crate::leave::*;
use crate::verbatim::*;
use crate::logging::*;
use crate::parse::*;
use crate::regexes::*;
Expand Down Expand Up @@ -123,9 +123,9 @@ pub fn apply_indent(
new_state.linum_old = linum_old;

new_state.ignore = get_ignore(line, &new_state, logs, file, true);
new_state.leave = get_leave(line, &new_state, logs, file, true);
new_state.verbatim = get_verbatim(line, &new_state, logs, file, true);

if !new_state.leave.visual && !new_state.ignore.visual {
if !new_state.verbatim.visual && !new_state.ignore.visual {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = &remove_comment(line, comment_index);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod comments;
mod format;
mod ignore;
mod indent;
mod leave;
mod verbatim;
mod logging;
mod parse;
mod regexes;
Expand Down
6 changes: 3 additions & 3 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ const LISTS: [&str; 5] = [
"inventory",
];

const LEAVES: [&str; 4] = ["verbatim", "Verbatim", "lstlisting", "minted"];
const VERBATIMS: [&str; 4] = ["verbatim", "Verbatim", "lstlisting", "minted"];

lazy_static! {
pub static ref RE_NEWLINES: Regex =
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_LEAVES_BEGIN: Vec<Regex> = LEAVES
pub static ref RE_VERBATIMS_BEGIN: Vec<Regex> = VERBATIMS
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{l}}}")).unwrap())
.collect();
pub static ref RE_LEAVES_END: Vec<Regex> = LEAVES
pub static ref RE_VERBATIMS_END: Vec<Regex> = VERBATIMS
.iter()
.map(|l| Regex::new(&format!(r"\\end\{{{l}}}")).unwrap())
.collect();
Expand Down
6 changes: 3 additions & 3 deletions src/subs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::comments::*;
use crate::format::*;
use crate::ignore::*;
use crate::leave::*;
use crate::verbatim::*;
use crate::logging::*;
use crate::regexes::*;
use crate::Cli;
Expand Down Expand Up @@ -42,9 +42,9 @@ pub fn environments_new_line(

for line in text.lines() {
state.ignore = get_ignore(line, &state, logs, file, false);
state.leave = get_leave(line, &state, logs, file, true);
state.verbatim = get_verbatim(line, &state, logs, file, true);

if !state.leave.visual
if !state.verbatim.visual
&& !state.ignore.visual
&& (line.contains(ENV_BEGIN)
|| line.contains(ENV_END)
Expand Down
24 changes: 12 additions & 12 deletions src/leave.rs → src/verbatim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::regexes::*;
use log::Level::Warn;

#[derive(Clone, Debug)]
pub struct Leave {
pub struct Verbatim {
pub actual: i8,
pub visual: bool,
}

impl Leave {
impl Verbatim {
pub const fn new() -> Self {
Self {
actual: 0,
Expand All @@ -18,16 +18,16 @@ impl Leave {
}
}

pub fn get_leave(
pub fn get_verbatim(
line: &str,
state: &State,
logs: &mut Vec<Log>,
file: &str,
warn: bool,
) -> Leave {
let diff = get_leave_diff(line);
let actual = state.leave.actual + diff;
let visual = actual > 0 && state.leave.actual > 0;
) -> Verbatim {
let diff = get_verbatim_diff(line);
let actual = state.verbatim.actual + diff;
let visual = actual > 0 && state.verbatim.actual > 0;

if warn && (actual < 0) {
record_line_log(
Expand All @@ -37,20 +37,20 @@ pub fn get_leave(
state.linum_new,
state.linum_old,
line,
"Leave count is negative.",
"Verbatim count is negative.",
);
}

Leave { actual, visual }
Verbatim { actual, visual }
}

fn get_leave_diff(line: &str) -> i8 {
fn get_verbatim_diff(line: &str) -> i8 {
if line.contains(ENV_BEGIN)
&& RE_LEAVES_BEGIN.iter().any(|r| r.is_match(line))
&& RE_VERBATIMS_BEGIN.iter().any(|r| r.is_match(line))
{
1
} else if line.contains(ENV_END)
&& RE_LEAVES_END.iter().any(|r| r.is_match(line))
&& RE_VERBATIMS_END.iter().any(|r| r.is_match(line))
{
-1
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const WRAP_MAX: usize = 80;

pub fn needs_wrap(line: &str, state: &State, args: &Cli) -> bool {
!args.keep
&& !state.leave.visual
&& !state.verbatim.visual
&& !state.ignore.visual
&& (line.chars().count() > WRAP_MAX)
}
Expand Down

0 comments on commit fda4319

Please sign in to comment.