Skip to content

Commit

Permalink
Merge 22b5e41 into 749024a
Browse files Browse the repository at this point in the history
  • Loading branch information
awb95 committed Aug 20, 2022
2 parents 749024a + 22b5e41 commit cca9925
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Rubberduck.SmartIndenter/AbsoluteCodeLine.cs
Expand Up @@ -26,6 +26,7 @@ public class AbsoluteCodeLine
private static readonly Regex PrecompilerInRegex = new Regex(@"^#(Else)?If\s.+Then$|^#Else$", RegexOptions.IgnoreCase);
private static readonly Regex PrecompilerOutRegex = new Regex(@"^#ElseIf\s.+Then|^#Else$|#End\sIf$", RegexOptions.IgnoreCase);
private static readonly Regex SingleLineElseIfRegex = new Regex(@"^ElseIf\s.*\sThen\s.*", RegexOptions.IgnoreCase);
private static readonly Regex IfThenWithColonNoElseRegex = new Regex(@"If\s.*\sThen:\s(?!.*:\sElse(If)?)", RegexOptions.IgnoreCase);

private readonly IIndenterSettings _settings;
private int _lineNumber;
Expand Down Expand Up @@ -192,6 +193,8 @@ public bool IsProcedureEnd

public bool IsEmpty => Original.Trim().Length == 0;

public bool ContainsIfThenWithColonNoElse => IfThenWithColonNoElseRegex.IsMatch(_code);

public int NextLineIndents
{
get
Expand Down
9 changes: 8 additions & 1 deletion Rubberduck.SmartIndenter/LogicalCodeLine.cs
Expand Up @@ -51,9 +51,16 @@ public int NextLineIndents
{
return 0;
}
return _rebuilt.Segments.Count() < 2
var indents = _rebuilt.Segments.Count() < 2
? _rebuilt.NextLineIndents
: _rebuilt.Segments.Select(s => new AbsoluteCodeLine(s, _settings)).Select(a => a.NextLineIndents).Sum();

if (_rebuilt.ContainsIfThenWithColonNoElse)
{
indents--;
}
return indents;

}
}

Expand Down
9 changes: 4 additions & 5 deletions RubberduckTests/SmartIndenter/MultiSegmentLineTests.cs
Expand Up @@ -83,25 +83,24 @@ public void UnmatchedEnumsNotIndent()
Assert.IsTrue(code.SequenceEqual(actual));
}

// https://github.com/rubberduck-vba/Rubberduck/issues/5929
[Test] // Broken in VB6 SmartIndenter.
[Category("Indenter")]
public void IfThenElseOnSameLineWorks()
{
var code = new[]
{
"Public Sub Test()",
"If Foo = 42 Then: Bar = Foo: Else",
"If Foo = 42 Then: Bar = Foo: Else Baz = Bar",
"Baz = Foo",
"End If",
"End Sub"
};

var expected = new[]
{
"Public Sub Test()",
" If Foo = 42 Then: Bar = Foo: Else",
" Baz = Foo",
" End If",
" If Foo = 42 Then: Bar = Foo: Else Baz = Bar",
" Baz = Foo",
"End Sub"
};

Expand Down

0 comments on commit cca9925

Please sign in to comment.