Skip to content

Commit 5ff67f4

Browse files
authored
Merge pull request #6012 from awb95/dev-5929-Indent_Single_Line_If_then_Colon
Fix Indent if-then-colon #5929
2 parents 749024a + 22b5e41 commit 5ff67f4

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

Rubberduck.SmartIndenter/AbsoluteCodeLine.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class AbsoluteCodeLine
2626
private static readonly Regex PrecompilerInRegex = new Regex(@"^#(Else)?If\s.+Then$|^#Else$", RegexOptions.IgnoreCase);
2727
private static readonly Regex PrecompilerOutRegex = new Regex(@"^#ElseIf\s.+Then|^#Else$|#End\sIf$", RegexOptions.IgnoreCase);
2828
private static readonly Regex SingleLineElseIfRegex = new Regex(@"^ElseIf\s.*\sThen\s.*", RegexOptions.IgnoreCase);
29+
private static readonly Regex IfThenWithColonNoElseRegex = new Regex(@"If\s.*\sThen:\s(?!.*:\sElse(If)?)", RegexOptions.IgnoreCase);
2930

3031
private readonly IIndenterSettings _settings;
3132
private int _lineNumber;
@@ -192,6 +193,8 @@ public bool IsProcedureEnd
192193

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

196+
public bool ContainsIfThenWithColonNoElse => IfThenWithColonNoElseRegex.IsMatch(_code);
197+
195198
public int NextLineIndents
196199
{
197200
get

Rubberduck.SmartIndenter/LogicalCodeLine.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,16 @@ public int NextLineIndents
5151
{
5252
return 0;
5353
}
54-
return _rebuilt.Segments.Count() < 2
54+
var indents = _rebuilt.Segments.Count() < 2
5555
? _rebuilt.NextLineIndents
5656
: _rebuilt.Segments.Select(s => new AbsoluteCodeLine(s, _settings)).Select(a => a.NextLineIndents).Sum();
57+
58+
if (_rebuilt.ContainsIfThenWithColonNoElse)
59+
{
60+
indents--;
61+
}
62+
return indents;
63+
5764
}
5865
}
5966

RubberduckTests/SmartIndenter/MultiSegmentLineTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,24 @@ public void UnmatchedEnumsNotIndent()
8383
Assert.IsTrue(code.SequenceEqual(actual));
8484
}
8585

86+
// https://github.com/rubberduck-vba/Rubberduck/issues/5929
8687
[Test] // Broken in VB6 SmartIndenter.
8788
[Category("Indenter")]
8889
public void IfThenElseOnSameLineWorks()
8990
{
9091
var code = new[]
9192
{
9293
"Public Sub Test()",
93-
"If Foo = 42 Then: Bar = Foo: Else",
94+
"If Foo = 42 Then: Bar = Foo: Else Baz = Bar",
9495
"Baz = Foo",
95-
"End If",
9696
"End Sub"
9797
};
9898

9999
var expected = new[]
100100
{
101101
"Public Sub Test()",
102-
" If Foo = 42 Then: Bar = Foo: Else",
103-
" Baz = Foo",
104-
" End If",
102+
" If Foo = 42 Then: Bar = Foo: Else Baz = Bar",
103+
" Baz = Foo",
105104
"End Sub"
106105
};
107106

0 commit comments

Comments
 (0)