Skip to content

Commit 2c6a498

Browse files
authored
Merge pull request #1864 from comintern/next
Fixes the last of the (known) SmartIndenter issues inherited from VB6.
2 parents 788dc2d + f6373af commit 2c6a498

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

Rubberduck.SmartIndenter/AbsoluteCodeLine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private int MultipleCaseAdjustment()
199199

200200
public string Indent(int indents, bool atProcStart, bool absolute = false)
201201
{
202-
if (IsEmpty || (ContainsOnlyComment && !_settings.AlignCommentsWithCode))
202+
if (IsEmpty || (ContainsOnlyComment && !_settings.AlignCommentsWithCode && !absolute))
203203
{
204204
return Original;
205205
}

Rubberduck.SmartIndenter/LogicalCodeLine.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,24 @@ public string Indented()
112112
var current = _lines.First().Indent(IndentationLevel, AtProcedureStart);
113113
var commentPos = string.IsNullOrEmpty(_lines.First().EndOfLineComment) ? 0 : current.Length - _lines.First().EndOfLineComment.Length;
114114
output.Add(current);
115-
var alignment = FunctionAlign(current, true);
115+
var alignment = FunctionAlign(current);
116116

117117
foreach (var line in _lines.Skip(1))
118118
{
119+
if (line.ContainsOnlyComment)
120+
{
121+
commentPos = alignment;
122+
}
119123
if (commentPos > 0)
120124
{
121125
output.Add(line.Indent(commentPos, AtProcedureStart, true));
122126
continue;
123127
}
124128

125-
var operatorAdjust = _settings.IgnoreOperatorsInContinuations && OperatorIgnoreRegex.IsMatch(line.Original) ? 2 : 0;
129+
var operatorAdjust = _settings.IgnoreOperatorsInContinuations && OperatorIgnoreRegex.IsMatch(line.Original) ? 2 : 0;
126130
current = line.Indent(Math.Max(alignment - operatorAdjust, 0), AtProcedureStart, true);
127131
output.Add(current);
128-
alignment = FunctionAlign(current, false);
132+
alignment = FunctionAlign(current);
129133
commentPos = string.IsNullOrEmpty(line.EndOfLineComment) ? 0 : current.Length - line.EndOfLineComment.Length;
130134
}
131135

@@ -135,7 +139,7 @@ public string Indented()
135139
private static readonly Regex StartIgnoreRegex = new Regex(@"^(\d*\s)?\s*[LR]?Set\s|^(\d*\s)?\s*Let\s|^(\d*\s)?\s*(Public|Private)\sDeclare\s(Function|Sub)|^(\d*\s+)");
136140
private readonly Stack<AlignmentToken> _alignment = new Stack<AlignmentToken>();
137141

138-
private int FunctionAlign(string line, bool firstLine)
142+
private int FunctionAlign(string line)
139143
{
140144
var stackPos = _alignment.Count;
141145

@@ -246,7 +250,7 @@ private int FunctionAlign(string line, bool firstLine)
246250

247251
if (fallback == 0 || fallback >= line.Length - 1)
248252
{
249-
fallback = !_alignment.Any() && firstLine ? (_settings.IndentSpaces * 2) : 0;
253+
fallback = !_alignment.Any() ? (_settings.IndentSpaces * 2) : 0;
250254
}
251255
else
252256
{

RubberduckTests/SmartIndenter/LineContinuationTests.cs

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public void ContinuationsInTypeDeclarationsWorks()
484484
}
485485

486486
//https://github.com/rubberduck-vba/Rubberduck/issues/1287
487-
[TestMethod, Ignore] // Broken in VB6 SmartIndenter.
487+
[TestMethod] // Broken in VB6 SmartIndenter.
488488
[TestCategory("Indenter")]
489489
public void ContinuationsInProcedureDeclarationsWorks()
490490
{
@@ -510,26 +510,25 @@ public void ContinuationsInProcedureDeclarationsWorks()
510510
"End Sub"
511511
};
512512

513-
//TODO: Not sure if this is what should be expected...
514513
var expected = new[]
515514
{
516515
"Sub MySub()",
517516
" Dim x1 As Integer",
518517
" Dim _",
519-
" x2 _",
520-
" As Integer",
518+
" x2 _",
519+
" As Integer",
521520
" Dim x3 As _",
522-
" Integer",
521+
" Integer",
523522
" Dim x4 _",
524-
" As _",
525-
" Integer",
523+
" As _",
524+
" Integer",
526525
" Dim x5 As Integer: _",
527-
" Dim x6 As _",
528-
" Integer",
526+
" Dim x6 As _",
527+
" Integer",
529528
" Dim x7 As Integer _",
530-
" 'Comment _",
531-
" as _",
532-
" integer",
529+
" 'Comment _",
530+
" as _",
531+
" integer",
533532
"End Sub"
534533
};
535534

@@ -545,7 +544,7 @@ public void ContinuationsInProcedureDeclarationsWorks()
545544
}
546545

547546
//https://github.com/rubberduck-vba/Rubberduck/issues/1287
548-
[TestMethod, Ignore] // Broken in VB6 SmartIndenter.
547+
[TestMethod] // Broken in VB6 SmartIndenter.
549548
[TestCategory("Indenter")]
550549
public void ContinuationsInProcedureDeclarationsNoCommentAlignWorks()
551550
{
@@ -571,26 +570,55 @@ public void ContinuationsInProcedureDeclarationsNoCommentAlignWorks()
571570
"End Sub"
572571
};
573572

574-
//TODO: Not sure if this is what should be expected...
575573
var expected = new[]
576574
{
577575
"Sub MySub()",
578576
" Dim x1 As Integer",
579577
" Dim _",
580-
" x2 _",
581-
" As Integer",
578+
" x2 _",
579+
" As Integer",
582580
" Dim x3 As _",
583-
" Integer",
581+
" Integer",
584582
" Dim x4 _",
585-
" As _",
586-
" Integer",
583+
" As _",
584+
" Integer",
587585
" Dim x5 As Integer: _",
588-
" Dim x6 As _",
589-
" Integer",
586+
" Dim x6 As _",
587+
" Integer",
590588
" Dim x7 As Integer _",
591-
"'Comment _",
592-
"as _",
593-
"integer",
589+
" 'Comment _",
590+
" as _",
591+
" integer",
592+
"End Sub"
593+
};
594+
595+
var indenter = new Indenter(null, () =>
596+
{
597+
var s = IndenterSettingsTests.GetMockIndenterSettings();
598+
s.AlignCommentsWithCode = false;
599+
s.IndentFirstDeclarationBlock = true;
600+
return s;
601+
});
602+
var actual = indenter.Indent(code, string.Empty);
603+
Assert.IsTrue(expected.SequenceEqual(actual));
604+
}
605+
606+
[TestMethod]
607+
public void ContinuationWithOnlyCommentWorks()
608+
{
609+
var code = new[]
610+
{
611+
"Sub MySub()",
612+
"Debug.Print Foo _",
613+
"'Is this and end of line comment or not...?",
614+
"End Sub"
615+
};
616+
617+
var expected = new[]
618+
{
619+
"Sub MySub()",
620+
" Debug.Print Foo _",
621+
" 'Is this and end of line comment or not...?",
594622
"End Sub"
595623
};
596624

0 commit comments

Comments
 (0)