Skip to content

Commit

Permalink
Move check for ignore outside of apply_indent()
Browse files Browse the repository at this point in the history
  • Loading branch information
cdesaintguilhem committed Oct 13, 2024
1 parent 70ac614 commit 0424d0a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 60 deletions.
97 changes: 57 additions & 40 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
29 changes: 9 additions & 20 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Log>,
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 = {}:",
Expand All @@ -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.",
);
Expand All @@ -182,5 +171,5 @@ pub fn apply_indent(
}
};

(new_line, new_state)
new_line
}

0 comments on commit 0424d0a

Please sign in to comment.