Skip to content

Commit

Permalink
More logging
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed May 18, 2024
1 parent a0ca55c commit ebe5cc6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn apply_passes(

while needs_wrap(&new_file) && !finished && pass < MAX_PASS + 2 {
let old_file = new_file.clone();
new_file = wrap(&new_file, filename, logs, Some(pass));
new_file = wrap(&new_file, filename, logs, Some(pass), args);
new_file = remove_trailing_spaces(&new_file);
new_file = apply_indent(&new_file, filename, args, logs, Some(pass));
pass += 1;
Expand All @@ -38,7 +38,7 @@ fn apply_passes(
filename.to_string(),
None,
None,
"Indent does not return to zero at end of file.".to_string(),
"Indent does not return to zero.".to_string(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn apply_indent(
Some(linum),
Some(line.to_string()),
format!(
"Indent: actual = {}, visual = {}",
"Indent: actual = {}, visual = {}.",
indent.actual, indent.visual
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn init_logger(args: &Cli) {
pub fn print_logs(args: &Cli, mut logs: Vec<Log>) {
if get_log_level(args) == LevelFilter::Warn && !logs.is_empty() {
let max_pass = &logs.iter().map(|l| l.pass).max().unwrap();
logs.retain(|l| l.pass == *max_pass || l.pass == None);
logs.retain(|l| l.pass == *max_pass || l.pass.is_none());
}

logs.sort_by_key(|l| l.time);
Expand Down
90 changes: 45 additions & 45 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
use crate::comments::*;
use crate::ignore::*;
use crate::logging::*;
use crate::parse::*;
use crate::regexes::*;
use log::Level::{Error, Info};

const WRAP: usize = 80;

Expand Down Expand Up @@ -31,8 +33,25 @@ fn find_wrap_point(line: &str) -> Option<usize> {
wrap_point
}

fn wrap_line(line: &str) -> String {
//log::info!("Wrap long line: {}{}", WHITE, line);
fn wrap_line(
line: &str,
linum: usize,
args: &Cli,
logs: &mut Vec<Log>,
pass: Option<usize>,
filename: &str,
) -> String {
if args.verbose {
record_log(
logs,
Info,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
"Wrapping long line.".to_string(),
);
}
let mut remaining_line = line.to_string();
let mut new_line = "".to_string();
let mut can_wrap = true;
Expand Down Expand Up @@ -71,8 +90,20 @@ pub fn wrap(
filename: &str,
logs: &mut Vec<Log>,
pass: Option<usize>,
args: &Cli,
) -> String {
//log::info!("Wrapping file");
if args.verbose {
// TODO this check should be in record_log fn
record_log(
logs,
Info,
pass,
filename.to_string(),
None,
None,
format!("Wrap pass {}.", pass.unwrap()),
);
}
let mut new_file = "".to_string();
let mut new_line: String;
let mut verbatim_count = 0;
Expand All @@ -84,7 +115,7 @@ pub fn wrap(
ignore = get_ignore(line, linum, ignore, filename, logs, pass, false);
if line_needs_wrap(line) && verbatim_count == 0 && !is_ignored(&ignore)
{
new_line = wrap_line(line);
new_line = wrap_line(line, linum, args, logs, pass, filename);
new_file.push_str(&new_line);
} else {
new_file.push_str(line);
Expand All @@ -98,50 +129,19 @@ pub fn wrap(
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,
//);
// TODO check how this works with verbatim and ignore
record_log(
logs,
Error,
pass,
filename.to_string(),
Some(linum),
Some(line.to_string()),
"Line cannot be wrapped.".to_string(),
);
}
}
}

new_file
}

#[cfg(test)]
#[test]
fn test_wrap_line() {
// no 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);
// 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);
// 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);
// leading spaces
let s_in = " Thislineistoolongbecauseithasmorethaneightycharactersinsideiteventhoughitstartswithspaces. \
Thereforeitshouldbesplit.";
let s_out = s_in;
assert_eq!(wrap_line(s_in), s_out);
}

0 comments on commit ebe5cc6

Please sign in to comment.