From f7a031555d0025aec06d49427638fdd4b5f5c9d1 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Thu, 24 Oct 2024 16:41:52 +0100 Subject: [PATCH] Only dedent lines with indentation --- src/indent.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/indent.rs b/src/indent.rs index c52b39d..eacc895 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -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) { @@ -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; }; @@ -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 } @@ -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 {