Skip to content

Commit c3c7ea0

Browse files
committed
Modified LineNumberRegex to recognize line numbers followed by colon. Line numbers followed by colon were not recognized as line numbers, but were instead treated as code segments, which caused the issue with indentation.
1 parent 72a53d3 commit c3c7ea0

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Rubberduck.SmartIndenter/AbsoluteCodeLine.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Rubberduck.SmartIndenter
1010
internal class AbsoluteCodeLine
1111
{
1212
private const string StupidLineEnding = ": _";
13-
private static readonly Regex LineNumberRegex = new Regex(@"^(?<number>(-?\d+)|(&H[0-9A-F]{1,8}))\s+(?<code>.*)", RegexOptions.ExplicitCapture);
13+
private static readonly Regex LineNumberRegex = new Regex(@"^(?<number>(-?\d+)|(&H[0-9A-F]{1,8}))(?<separator>:)?\s+(?<code>.*)", RegexOptions.ExplicitCapture);
1414
private static readonly Regex EndOfLineCommentRegex = new Regex(@"^(?!(Rem\s)|('))(?<code>[^']*)(\s(?<comment>'.*))$", RegexOptions.ExplicitCapture);
1515
private static readonly Regex ProcedureStartRegex = new Regex(@"^(Public\s|Private\s|Friend\s)?(Static\s)?(Sub|Function|Property\s(Let|Get|Set))\s");
1616
private static readonly Regex ProcedureStartIgnoreRegex = new Regex(@"^[LR]?Set\s|^Let\s|^(Public|Private)\sDeclare\s(Function|Sub)");
@@ -26,6 +26,7 @@ internal class AbsoluteCodeLine
2626

2727
private readonly IIndenterSettings _settings;
2828
private int _lineNumber;
29+
private bool _lineNumberSeparator;
2930
private bool _numbered;
3031
private string _code;
3132
private readonly bool _stupidLineEnding;
@@ -69,6 +70,7 @@ private void ExtractLineNumber()
6970
{
7071
_code = match.Groups["code"].Value;
7172
_numbered = true;
73+
_lineNumberSeparator = match.Groups["separator"].Value != string.Empty;
7274
var number = match.Groups["number"].Value;
7375
if (!int.TryParse(number, out _lineNumber))
7476
{
@@ -254,14 +256,15 @@ public string Indent(int indents, bool atProcStart, bool absolute = false)
254256
}
255257

256258
var number = _numbered ? _lineNumber.ToString(CultureInfo.InvariantCulture) : string.Empty;
257-
var gap = Math.Max((absolute ? indents : _settings.IndentSpaces * indents) - number.Length, number.Length > 0 ? 1 : 0);
259+
var separator = _lineNumberSeparator ? ":" : string.Empty;
260+
var gap = Math.Max((absolute ? indents : _settings.IndentSpaces * indents) - number.Length - separator.Length, number.Length + separator.Length > 0 ? 1 : 0);
258261
if (_settings.AlignDims && (IsDeclaration || IsDeclarationContinuation))
259262
{
260263
AlignDims(gap);
261264
}
262265

263266
var code = string.Join(": ", _segments);
264-
code = string.Join(string.Empty, number, new string(' ', gap), code);
267+
code = string.Join(string.Empty, number, separator, new string(' ', gap), code);
265268
if (string.IsNullOrEmpty(EndOfLineComment))
266269
{
267270
return _escaper.UnescapeIndented(code + (_stupidLineEnding ? StupidLineEnding : string.Empty));

RubberduckTests/SmartIndenter/MiscAndCornerCaseTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,33 @@ public void HexLineNumbersWork()
298298
Assert.IsTrue(expected.SequenceEqual(actual));
299299
}
300300

301+
[TestMethod]
302+
[TestCategory("Indenter")]
303+
public void LineNumbersWithColonWork()
304+
{
305+
var code = new[]
306+
{
307+
"Private Sub Test()",
308+
"5: If Foo Then",
309+
"10: Debug.Print",
310+
"15: End If",
311+
"End Sub"
312+
};
313+
314+
var expected = new[]
315+
{
316+
"Private Sub Test()",
317+
"5: If Foo Then",
318+
"10: Debug.Print",
319+
"15: End If",
320+
"End Sub"
321+
};
322+
323+
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
324+
var actual = indenter.Indent(code);
325+
Assert.IsTrue(expected.SequenceEqual(actual));
326+
}
327+
301328
[TestMethod]
302329
[TestCategory("Indenter")]
303330
public void LineNumberLongerThanIndentFallsBackToOneSpace()

0 commit comments

Comments
 (0)