Skip to content

Commit

Permalink
Working on logs
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 17, 2024
1 parent 110d2f4 commit adec5bb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 47 deletions.
26 changes: 6 additions & 20 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use crate::colors::*;
use crate::indent::*;
use crate::subs::*;
use crate::wrap::*;
use crate::Cli;

const MAX_TRIES: u8 = 10;

fn apply_passes(file: &str, args: &Cli) -> String {
let mut new_file = apply_indent(file, args);
fn apply_passes(file: &str, filename: &str, args: &Cli) -> String {
let mut new_file = apply_indent(file, filename, args);
let mut finished = false;
let mut tries = 0;

while needs_wrap(&new_file) && !finished && tries < MAX_TRIES {
let old_file = new_file.clone();
new_file = wrap(&new_file);
new_file = wrap(&new_file, filename);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, args);
new_file = apply_indent(&new_file, filename, args);
tries += 1;
if new_file == old_file {
finished = true;
Expand All @@ -27,27 +26,14 @@ fn apply_passes(file: &str, args: &Cli) -> String {
log::error!("Indent does not return to zero at end of file");
}

// TODO move this logging into wrap.rs
if needs_wrap(&new_file) {
for (i, line) in new_file.lines().enumerate() {
if line_needs_wrap(line) {
log::warn!(
" Line {}: cannot be wrapped: {}{:.50}...",
i,
WHITE,
line
);
}
}
}
new_file
}

pub fn format_file(file: &str, args: &Cli) -> String {
pub fn format_file(file: &str, filename: &str, args: &Cli) -> String {
let mut new_file = remove_extra_newlines(file);
new_file = begin_end_environments_new_line(&new_file);
new_file = remove_tabs(&new_file);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_passes(&new_file, args);
new_file = apply_passes(&new_file, filename, args);
new_file
}
23 changes: 15 additions & 8 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,38 @@ impl Ignore {
}
}

pub fn get_ignore(line: &str, i: usize, prev_ignore: Ignore) -> Ignore {
pub fn get_ignore(line: &str, i: usize, ignore: Ignore, filename: &str) -> Ignore {
let skip = contains_ignore_skip(line);
let start = contains_ignore_start(line);
let end = contains_ignore_end(line);
let mut block = prev_ignore.block;
let mut block = ignore.block;

if !prev_ignore.block {
if !ignore.block {
// not currently in ignore block
if start {
block = true
}
if end {
log::warn!(
" Line {}: no ignore block to end: {}{:.50}...",
i,
"{}tex-fmt {}{}: {}Line {}. \
{}No ignore block to end: \
{}{:.50}",
PINK,
PURPLE,
filename,
WHITE,
line
i,
YELLOW,
RESET,
line,
);
}
} else {
// currently in ignore block
if start {
log::warn!(
" Line {}: cannot start ignore block \
before ending previous block: {}{:.50}...",
"Line {}: cannot start ignore block \
before ending previous block: {}{:.50}",
i,
WHITE,
line
Expand Down
27 changes: 18 additions & 9 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,40 +102,49 @@ fn get_indent(line: &str, prev_indent: Indent) -> Indent {
Indent { actual, visual }
}

pub fn apply_indent(file: &str, args: &Cli) -> String {
pub fn apply_indent(file: &str, filename: &str, args: &Cli) -> String {
log::info!("Indenting file");

let mut indent = Indent::new();
let mut ignore = Ignore::new();
let mut new_file = String::with_capacity(file.len());
let mut verbatim_count = 0;

for (i, line) in file.lines().enumerate() {
for (linum, line) in file.lines().enumerate() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
ignore = get_ignore(line, i, ignore);
ignore = get_ignore(line, linum, ignore, filename);
if verbatim_count == 0 && !is_ignored(&ignore) {
// calculate indent
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
indent = get_indent(line_strip, indent);
log::info!(
"Indent line {}: actual = {}, visual = {}:{} {}",
i,
linum,
indent.actual,
indent.visual,
WHITE,
line,
);

if (indent.visual < 0) || (indent.actual < 0) {
log::error!(
"Line {}: indent is negative: {}{:.50}",
i,

log::warn!(
"{}tex-fmt {}{}: {}Line {}. \
{}Indent is negative: \
{}{:.50}",
PINK,
PURPLE,
filename,
WHITE,
line
);
linum,
YELLOW,
RESET,
line,
);

}

if !args.debug {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() {
};

let file = fs::read_to_string(filename).unwrap();
let new_file = format_file(&file, &args);
let new_file = format_file(&file, filename, &args);

if args.print {
print_file(&new_file);
Expand Down
4 changes: 2 additions & 2 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::colors::*;

pub fn print_script_name() {
println!("{}", String::new() + PINK + "tex-fmt" + RESET);
println!("{}{}{}", PINK, "tex-fmt", RESET);
}

pub fn print_filename(filename: &str) {
println!("{}", String::new() + PURPLE + filename + RESET);
println!("{}{}{}{}{}", PINK, "tex-fmt ", PURPLE, filename, RESET);
}

pub fn print_file(new_file: &str) {
Expand Down
37 changes: 30 additions & 7 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ fn wrap_line(line: &str) -> String {
new_line
}

pub fn wrap(file: &str) -> String {
pub fn wrap(file: &str, filename: &str) -> String {
log::info!("Wrapping file");
let mut new_file = "".to_string();
let mut new_line: String;
let mut verbatim_count = 0;
let mut ignore = Ignore::new();
for (i, line) in file.lines().enumerate() {
for (linum, line) in file.lines().enumerate() {
if RE_VERBATIM_BEGIN.is_match(line) {
verbatim_count += 1;
}
ignore = get_ignore(line, i, ignore);
ignore = get_ignore(line, linum, ignore, filename);
if line_needs_wrap(line) && verbatim_count == 0 && !is_ignored(&ignore)
{
new_line = wrap_line(line);
Expand All @@ -88,6 +88,29 @@ pub fn wrap(file: &str) -> String {
verbatim_count += 1;
}
}

if needs_wrap(&new_file) {
for (linum, line) in new_file.lines().enumerate() {
if line_needs_wrap(line) {
log::warn!(
"{}tex-fmt {}{}: {}Line {}. \
{}Line cannot be wrapped: \
{}{:.50}",
PINK,
PURPLE,
filename,
WHITE,
linum,
YELLOW,
RESET,
line,
);
}
}
}



new_file
}

Expand All @@ -99,22 +122,22 @@ fn test_wrap_line() {
Therefore it should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, 0), s_out);
// break before comment
let s_in = "This line is too long because it has more than eighty characters inside it. \
Therefore it % should be split.";
let s_out = "This line is too long because it has more than eighty characters inside it.\n \
Therefore it % should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, 0), s_out);
// break after comment
let s_in = "This line is too long because % it has more than eighty characters inside it. \
Therefore it should be split.";
let s_out = "This line is too long because % it has more than eighty characters inside it.\n\
% Therefore it should be split.";
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, 0), s_out);
// leading spaces
let s_in = " Thislineistoolongbecauseithasmorethaneightycharactersinsideiteventhoughitstartswithspaces. \
Thereforeitshouldbesplit.";
let s_out = s_in;
assert_eq!(wrap_line(s_in), s_out);
assert_eq!(wrap_line(s_in, 0), s_out);
}
2 changes: 2 additions & 0 deletions tests/wrap_out.tex
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
This line would usually be split at the special character part with a
slash\ but it's best to break the line earlier.

))

\end{document}

0 comments on commit adec5bb

Please sign in to comment.