Skip to content

Commit

Permalink
Working on faster implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 25, 2024
1 parent 99f626e commit 95c82fd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
18 changes: 16 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub fn format_file(
) -> String {
record_file_log(logs, Info, file, "Formatting started.");
let mut old_text = remove_extra_newlines(text);
old_text = environments_new_line(&old_text, file, args, logs);
old_text = remove_tabs(&old_text, args);
old_text = remove_trailing_spaces(&old_text);

Expand All @@ -35,13 +34,26 @@ pub fn format_file(
let temp_state: State;
(line, temp_state) =
apply_indent(&line, linum_old, &state, logs, file, args);
if needs_wrap(&line, &temp_state, args) {
if needs_env_new_line(&line, &temp_state, args) {
let env_lines =
put_env_new_line(&line, &temp_state, file, args, logs);
if env_lines.is_some() {
queue.push((linum_old, env_lines.clone().unwrap().1));
queue.push((linum_old, env_lines.clone().unwrap().0));
} else {
state = temp_state;
new_text.push_str(&line);
new_text.push_str(LINE_END);
state.linum_new += 1;
};
} else if needs_wrap(&line, &temp_state, args) {
let wrapped_lines =
apply_wrap(&line, &temp_state, file, args, logs);
if wrapped_lines.is_some() {
queue.push((linum_old, wrapped_lines.clone().unwrap().1));
queue.push((linum_old, wrapped_lines.clone().unwrap().0));
} else {
state = temp_state;
new_text.push_str(&line);
new_text.push_str(LINE_END);
state.linum_new += 1;
Expand All @@ -65,9 +77,11 @@ pub fn format_file(

record_file_log(logs, Info, file, "Formatting complete.");

new_text = remove_trailing_spaces(&new_text);
new_text
}


/// Information on the current state during formatting
#[derive(Clone, Debug)]
pub struct State {
Expand Down
76 changes: 33 additions & 43 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

use crate::comments::*;
use crate::format::*;
use crate::ignore::*;
use crate::logging::*;
use crate::regexes::*;
use crate::verbatim::*;
use crate::Cli;
use crate::LINE_END;
use log::Level::Info;

/// Remove multiple line breaks
pub fn remove_extra_newlines(text: &str) -> String {
Expand All @@ -27,50 +24,43 @@ pub fn remove_trailing_spaces(text: &str) -> String {
RE_TRAIL.replace_all(text, LINE_END).to_string()
}

pub fn needs_env_new_line(line: &str, state: &State, args: &Cli) -> bool {
!state.verbatim.visual
&& !state.ignore.visual
&& (line.contains(ENV_BEGIN)
|| line.contains(ENV_END)
|| line.contains(ITEM))
&& (RE_ENV_BEGIN_SHARED_LINE.is_match(line)
|| RE_ENV_END_SHARED_LINE.is_match(line)
|| RE_ITEM_SHARED_LINE.is_match(line))
}

/// Ensure LaTeX environments begin on new lines
pub fn environments_new_line(
text: &str,
pub fn put_env_new_line(
line: &str,
state: &State,
file: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> String {
if args.verbose {
record_file_log(
logs,
Info,
file,
"Ensuring environments on new lines.",
);
) -> Option<(String, String)> {
let comment_index = find_comment_index(line);
let comment = &get_comment(line, comment_index);
let mut text = &remove_comment(line, comment_index);
let mut temp =
RE_ENV_BEGIN_SHARED_LINE.replace(text, format!("$prev{LINE_END}$env")).to_string();
text = &temp;
if !text.contains(LINE_END) {
temp = RE_ENV_END_SHARED_LINE.replace(text, format!("$prev{LINE_END}$env")).to_string();
text = &temp;
}

let mut state = State::new();
let mut new_text = String::with_capacity(text.len());

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

if !state.verbatim.visual
&& !state.ignore.visual
&& (line.contains(ENV_BEGIN)
|| line.contains(ENV_END)
|| line.contains(ITEM))
{
let comment_index = find_comment_index(line);
let comment = &get_comment(line, comment_index);
let text = &remove_comment(line, comment_index);
let text = &RE_ENV_BEGIN_SHARED_LINE
.replace_all(text, format!("$prev{LINE_END}$env"));
let text = &RE_ENV_END_SHARED_LINE
.replace_all(text, format!("$prev{LINE_END}$env"));
let text = &RE_ITEM_SHARED_LINE
.replace_all(text, format!("$prev{LINE_END}$env"));
new_text.push_str(text);
new_text.push_str(comment);
} else {
new_text.push_str(line);
}
new_text.push_str(LINE_END);
if !text.contains(LINE_END) {
temp = RE_ITEM_SHARED_LINE.replace(text, format!("$prev{LINE_END}$env")).to_string();
text = &temp;
}
if text.contains(LINE_END) {
let split = text.split_once(LINE_END).unwrap();
dbg!(&split);
return Some((split.0.to_string(), split.1.to_string()))
}
new_text
None
}
4 changes: 2 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn test_short() {
//"comments.tex",
//"cv.tex",
//"document.tex",
//"environment_lines.tex",
"environment_lines.tex",
//"heavy_wrap.tex",
//"higher_categories_thesis.bib",
//"higher_categories_thesis.tex",
Expand All @@ -100,7 +100,7 @@ fn test_short() {
//"puthesis.cls",
//"quiver.sty",
//"readme.tex",
"short_document.tex",
//"short_document.tex",
//"tikz_network.sty",
//"unicode.tex",
//"verbatim.tex",
Expand Down

0 comments on commit 95c82fd

Please sign in to comment.