Skip to content

Commit

Permalink
Extract indent calculation into function
Browse files Browse the repository at this point in the history
  • Loading branch information
cdesaintguilhem committed Oct 13, 2024
1 parent e41a66d commit eb7971e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,23 @@ fn get_indent(line: &str, prev_indent: &Indent, pattern: &Pattern) -> Indent {
Indent { actual, visual }
}

/// Apply the correct indentation to a line
pub fn apply_indent(
/// Calculates the indent for `line` based on its contents. This functions saves the calculated [Indent], which might be
/// negative, to the given [State], and then ensures that the returned [Indent] is non-negative.
fn calculate_indent(
line: &str,
state: &mut State,
logs: &mut Vec<Log>,
file: &str,
args: &Cli,
pattern: &Pattern,
indent_char: &str,
) -> String {
// calculate indent
) -> Indent {
// Calculate the new indent by first removing the comment from the line (if there is one) to ignore diffs from
// characters in there.
let comment_index = find_comment_index(line);
let line_strip = remove_comment(line, comment_index);
let mut indent = get_indent(line_strip, &state.indent, pattern);
state.indent = indent.clone();

// Record the indent to the logs.
if args.trace {
record_line_log(
logs,
Expand All @@ -137,6 +139,11 @@ pub fn apply_indent(
);
}

// Save the indent to the state. Note, this indent might be negative; it is saved without correction so that this is
// not forgotten for the next iterations.
state.indent = indent.clone();

// However, we can't negatively indent a line. So we log the negative indent and reset the values to 0.
if (indent.visual < 0) || (indent.actual < 0) {
record_line_log(
logs,
Expand All @@ -151,6 +158,22 @@ pub fn apply_indent(
indent.visual = indent.visual.max(0);
}

indent
}

/// Apply the correct indentation to a line
pub fn apply_indent(
line: &str,
state: &mut State,
logs: &mut Vec<Log>,
file: &str,
args: &Cli,
pattern: &Pattern,
indent_char: &str,
) -> String {
// calculate indent
let indent = calculate_indent(line, state, logs, file, args, pattern);

// apply indent
let trimmed_line = line.trim_start();
if trimmed_line.is_empty() {
Expand Down

0 comments on commit eb7971e

Please sign in to comment.