Skip to content

Commit fe2746e

Browse files
committed
Add indenter support, tests for negative and hex line numbers.
1 parent e8b987f commit fe2746e

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

Rubberduck.SmartIndenter/AbsoluteCodeLine.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class AbsoluteCodeLine
1414
private const char BracketPlaceholder = '\x2';
1515
private static readonly Regex StringReplaceRegex = new Regex(StringPlaceholder.ToString(CultureInfo.InvariantCulture));
1616
private static readonly Regex BracketReplaceRegex = new Regex(BracketPlaceholder.ToString(CultureInfo.InvariantCulture));
17-
private static readonly Regex LineNumberRegex = new Regex(@"^(?<number>\d+)\s+(?<code>.*)", RegexOptions.ExplicitCapture);
17+
private static readonly Regex LineNumberRegex = new Regex(@"^(?<number>(-?\d+)|(&H[0-9A-F]{1,8}))\s+(?<code>.*)", RegexOptions.ExplicitCapture);
1818
private static readonly Regex EndOfLineCommentRegex = new Regex(@"^(?!(Rem\s)|('))(?<code>[^']*)(\s(?<comment>'.*))$", RegexOptions.ExplicitCapture);
1919
private static readonly Regex ProcedureStartRegex = new Regex(@"^(Public\s|Private\s|Friend\s)?(Static\s)?(Sub|Function|Property\s(Let|Get|Set))\s");
2020
private static readonly Regex ProcedureStartIgnoreRegex = new Regex(@"^[LR]?Set\s|^Let\s|^(Public|Private)\sDeclare\s(Function|Sub)");
@@ -29,7 +29,7 @@ internal class AbsoluteCodeLine
2929
private static readonly Regex SingleLineElseIfRegex = new Regex(@"^ElseIf\s.*\sThen\s.*");
3030

3131
private readonly IIndenterSettings _settings;
32-
private uint _lineNumber;
32+
private int _lineNumber;
3333
private bool _numbered;
3434
private string _code;
3535
private readonly bool _stupidLineEnding;
@@ -129,9 +129,14 @@ private void ExtractLineNumber()
129129
var match = LineNumberRegex.Match(_code);
130130
if (match.Success)
131131
{
132-
_numbered = true;
133-
_lineNumber = Convert.ToUInt32(match.Groups["number"].Value);
134132
_code = match.Groups["code"].Value;
133+
_numbered = true;
134+
var number = match.Groups["number"].Value;
135+
if (!int.TryParse(number, out _lineNumber))
136+
{
137+
int.TryParse(number.Replace("&H", string.Empty), NumberStyles.HexNumber,
138+
CultureInfo.InvariantCulture, out _lineNumber);
139+
}
135140
}
136141
}
137142
_code = _code.Trim();

RubberduckTests/SmartIndenter/MiscAndCornerCaseTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,60 @@ public void LineNumbersAreNotIncludedInIndentAmount()
244244
Assert.IsTrue(expected.SequenceEqual(actual));
245245
}
246246

247+
[TestMethod]
248+
[TestCategory("Indenter")]
249+
public void NegativeLineNumbersWork()
250+
{
251+
var code = new[]
252+
{
253+
"Private Sub Test()",
254+
"-5 If Foo Then",
255+
"-10 Debug.Print",
256+
"-15 End If",
257+
"End Sub"
258+
};
259+
260+
var expected = new[]
261+
{
262+
"Private Sub Test()",
263+
"-5 If Foo Then",
264+
"-10 Debug.Print",
265+
"-15 End If",
266+
"End Sub"
267+
};
268+
269+
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
270+
var actual = indenter.Indent(code, string.Empty);
271+
Assert.IsTrue(expected.SequenceEqual(actual));
272+
}
273+
274+
[TestMethod]
275+
[TestCategory("Indenter")]
276+
public void HexLineNumbersWork()
277+
{
278+
var code = new[]
279+
{
280+
"Private Sub Test()",
281+
"&HAAA If Foo Then",
282+
"&HABE Debug.Print",
283+
"&HAD2 End If",
284+
"End Sub"
285+
};
286+
287+
var expected = new[]
288+
{
289+
"Private Sub Test()",
290+
"2730 If Foo Then",
291+
"2750 Debug.Print",
292+
"2770 End If",
293+
"End Sub"
294+
};
295+
296+
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
297+
var actual = indenter.Indent(code, string.Empty);
298+
Assert.IsTrue(expected.SequenceEqual(actual));
299+
}
300+
247301
[TestMethod]
248302
[TestCategory("Indenter")]
249303
public void LineNumberLongerThanIndentFallsBackToOneSpace()

0 commit comments

Comments
 (0)