diff --git a/src/format.rs b/src/format.rs index 4e2de93..6256914 100644 --- a/src/format.rs +++ b/src/format.rs @@ -38,48 +38,65 @@ pub fn format_file( // Read the patterns present on this line. let pattern = Pattern::new(&line); - let temp_state: State; - - // Apply indent based on the current state and the patterns in the line. - (line, temp_state) = apply_indent( - &line, - linum_old, - &state, - logs, - file, - args, - &pattern, - indent_char, - ); - - // Split the line based on: - // - environment commands, and - // - sectioning commands, - // and add the split to the queue. - - // Wrap the line after indenting, and add the wrap to the queue. - - // TODO: implement debug checks that the indent and the line length are correct. - - // Add line to new text - - if needs_env_new_line(&line, &temp_state, &pattern) { - let (this_line, next_line) = - put_env_new_line(&line, &temp_state, file, args, logs); - if let Some(next_line) = next_line { - queue.push((linum_old, next_line.to_string())); + // Temporary state for working on this line + let mut temp_state = state.clone(); + + temp_state.linum_old = linum_old; + temp_state.ignore = + get_ignore(&line, &temp_state, logs, file, true); + temp_state.verbatim = + get_verbatim(&line, &temp_state, logs, file, true, &pattern); + + // If the line should not be ignored ... + if !(temp_state.verbatim.visual || temp_state.ignore.visual) { + // ... format it. + + // Apply indent based on the current state and the patterns in the line. + line = apply_indent( + &line, + &mut temp_state, + logs, + file, + args, + &pattern, + indent_char, + ); + + // Split the line based on: + // - environment commands, and + // - sectioning commands, + // and add the split to the queue. + + // Wrap the line after indenting, and add the wrap to the queue. + + // TODO: implement debug checks that the indent and the line length are correct. + + // Add line to new text + + if needs_env_new_line(&line, &temp_state, &pattern) { + let (this_line, next_line) = + put_env_new_line(&line, &temp_state, file, args, logs); + if let Some(next_line) = next_line { + queue.push((linum_old, next_line.to_string())); + } + queue.push((linum_old, this_line.to_string())); + continue; } - queue.push((linum_old, this_line.to_string())); - continue; - } - 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)); - continue; + 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, + )); + continue; + } } } diff --git a/src/indent.rs b/src/indent.rs index 01294bb..5c0506d 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -2,11 +2,9 @@ use crate::comments::*; use crate::format::*; -use crate::ignore::*; use crate::logging::*; use crate::parse::*; use crate::regexes::*; -use crate::verbatim::*; use core::cmp::max; use log::Level::{Trace, Warn}; @@ -112,37 +110,28 @@ fn get_indent(line: &str, prev_indent: &Indent, pattern: &Pattern) -> Indent { /// Apply the correct indentation to a line pub fn apply_indent( line: &str, - linum_old: usize, - state: &State, + state: &mut State, logs: &mut Vec, file: &str, args: &Cli, pattern: &Pattern, indent_char: &str, -) -> (String, State) { +) -> String { #![allow(clippy::too_many_arguments)] - let mut new_state = state.clone(); - new_state.linum_old = linum_old; - new_state.ignore = get_ignore(line, &new_state, logs, file, true); - new_state.verbatim = - get_verbatim(line, &new_state, logs, file, true, pattern); - - let new_line = if new_state.verbatim.visual || new_state.ignore.visual { - line.to_string() - } else { + let new_line = { // calculate indent let comment_index = find_comment_index(line); - let line_strip = &remove_comment(line, comment_index); + let line_strip = remove_comment(line, comment_index); let mut indent = get_indent(line_strip, &state.indent, pattern); - new_state.indent = indent.clone(); + state.indent = indent.clone(); if args.trace { record_line_log( logs, Trace, file, state.linum_new, - new_state.linum_old, + state.linum_old, line, &format!( "Indent: actual = {}, visual = {}:", @@ -156,8 +145,8 @@ pub fn apply_indent( logs, Warn, file, - new_state.linum_new, - new_state.linum_old, + state.linum_new, + state.linum_old, line, "Indent is negative.", ); @@ -182,5 +171,5 @@ pub fn apply_indent( } }; - (new_line, new_state) + new_line }