Skip to content

Commit

Permalink
Only dedent lines with indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Oct 24, 2024
1 parent 9f2e143 commit f7a0315
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ fn get_diff(line: &str, pattern: &Pattern) -> i8 {
}

/// Calculate dedentation for the current line
fn get_back(line: &str, pattern: &Pattern) -> i8 {
fn get_back(line: &str, pattern: &Pattern, state: &State) -> i8 {
// Only need to dedent if indentation is present
if state.indent.actual == 0 {
return 0;
}
let mut back: i8 = 0;

if pattern.contains_env_end && line.contains(ENV_END) {
Expand All @@ -80,10 +84,8 @@ fn get_back(line: &str, pattern: &Pattern) -> i8 {
}
// other environments get single indents
back = 1;
};

// deindent items to make the rest of item environment appear indented
if pattern.contains_item && line.contains(ITEM) {
} else if pattern.contains_item && line.contains(ITEM) {
// deindent items to make the rest of item environment appear indented
back += 1;
};

Expand All @@ -99,9 +101,9 @@ fn get_back(line: &str, pattern: &Pattern) -> i8 {
}

/// Calculate indentation properties of the current line
fn get_indent(line: &str, prev_indent: &Indent, pattern: &Pattern) -> Indent {
fn get_indent(line: &str, prev_indent: &Indent, pattern: &Pattern, state: &State) -> Indent {
let diff = get_diff(line, pattern);
let back = get_back(line, pattern);
let back = get_back(line, pattern, state);
let actual = prev_indent.actual + diff;
let visual = prev_indent.actual - back;
Indent { actual, visual }
Expand All @@ -123,7 +125,7 @@ pub fn calculate_indent(
// (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);
let mut indent = get_indent(line_strip, &state.indent, pattern, state);

// Record the indent to the logs.
if args.trace {
Expand Down

0 comments on commit f7a0315

Please sign in to comment.