Skip to content

Commit f81cd5b

Browse files
authored
Merge pull request #3536 from Hosch250/indenter
Make Smart Indenter ignore case. Makes the indenter more web-friendly, where arbitrary code isn't necessary pasted straight from the VBE.
2 parents 2dcf4c4 + 9d6b68d commit f81cd5b

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

Rubberduck.SmartIndenter/AbsoluteCodeLine.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ 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}))(?<separator>:)?\s+(?<code>.*)", RegexOptions.ExplicitCapture);
14-
private static readonly Regex EndOfLineCommentRegex = new Regex(@"^(?!(Rem\s)|('))(?<code>[^']*)(\s(?<comment>'.*))$", RegexOptions.ExplicitCapture);
15-
private static readonly Regex ProcedureStartRegex = new Regex(@"^(Public\s|Private\s|Friend\s)?(Static\s)?(Sub|Function|Property\s(Let|Get|Set))\s");
16-
private static readonly Regex ProcedureStartIgnoreRegex = new Regex(@"^[LR]?Set\s|^Let\s|^(Public|Private)\sDeclare\s(Function|Sub)");
17-
private static readonly Regex ProcedureEndRegex = new Regex(@"^End\s(Sub|Function|Property)");
18-
private static readonly Regex TypeEnumStartRegex = new Regex(@"^(Public\s|Private\s)?(Enum\s|Type\s)");
19-
private static readonly Regex TypeEnumEndRegex = new Regex(@"^End\s(Enum|Type)");
20-
private static readonly Regex InProcedureInRegex = new Regex(@"^(Else)?If\s.*\sThen$|^Else$|^Case\s|^With|^For\s|^Do$|^Do\s|^While$|^While\s|^Select Case");
21-
private static readonly Regex InProcedureOutRegex = new Regex(@"^Else(If)?|^Case\s|^End With|^Next\s|^Next$|^Loop$|^Loop\s|^Wend$|^End If$|^End Select");
22-
private static readonly Regex DeclarationRegex = new Regex(@"^(Dim|Const|Static|Public|Private)\s(.*(\sAs\s)?|_)");
23-
private static readonly Regex PrecompilerInRegex = new Regex(@"^#(Else)?If\s.+Then$|^#Else$");
24-
private static readonly Regex PrecompilerOutRegex = new Regex(@"^#ElseIf\s.+Then|^#Else$|#End\sIf$");
25-
private static readonly Regex SingleLineElseIfRegex = new Regex(@"^ElseIf\s.*\sThen\s.*");
13+
private static readonly Regex LineNumberRegex = new Regex(@"^(?<number>(-?\d+)|(&H[0-9A-F]{1,8}))(?<separator>:)?\s+(?<code>.*)", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
14+
private static readonly Regex EndOfLineCommentRegex = new Regex(@"^(?!(Rem\s)|('))(?<code>[^']*)(\s(?<comment>'.*))$", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
15+
private static readonly Regex ProcedureStartRegex = new Regex(@"^(Public\s|Private\s|Friend\s)?(Static\s)?(Sub|Function|Property\s(Let|Get|Set))\s", RegexOptions.IgnoreCase);
16+
private static readonly Regex ProcedureStartIgnoreRegex = new Regex(@"^[LR]?Set\s|^Let\s|^(Public|Private)\sDeclare\s(Function|Sub)", RegexOptions.IgnoreCase);
17+
private static readonly Regex ProcedureEndRegex = new Regex(@"^End\s(Sub|Function|Property)", RegexOptions.IgnoreCase);
18+
private static readonly Regex TypeEnumStartRegex = new Regex(@"^(Public\s|Private\s)?(Enum\s|Type\s)", RegexOptions.IgnoreCase);
19+
private static readonly Regex TypeEnumEndRegex = new Regex(@"^End\s(Enum|Type)", RegexOptions.IgnoreCase);
20+
private static readonly Regex InProcedureInRegex = new Regex(@"^(Else)?If\s.*\sThen$|^Else$|^Case\s|^With|^For\s|^Do$|^Do\s|^While$|^While\s|^Select Case", RegexOptions.IgnoreCase);
21+
private static readonly Regex InProcedureOutRegex = new Regex(@"^Else(If)?|^Case\s|^End With|^Next\s|^Next$|^Loop$|^Loop\s|^Wend$|^End If$|^End Select", RegexOptions.IgnoreCase);
22+
private static readonly Regex DeclarationRegex = new Regex(@"^(Dim|Const|Static|Public|Private)\s(.*(\sAs\s)?|_)", RegexOptions.IgnoreCase);
23+
private static readonly Regex PrecompilerInRegex = new Regex(@"^#(Else)?If\s.+Then$|^#Else$", RegexOptions.IgnoreCase);
24+
private static readonly Regex PrecompilerOutRegex = new Regex(@"^#ElseIf\s.+Then|^#Else$|#End\sIf$", RegexOptions.IgnoreCase);
25+
private static readonly Regex SingleLineElseIfRegex = new Regex(@"^ElseIf\s.*\sThen\s.*", RegexOptions.IgnoreCase);
2626

2727
private readonly IIndenterSettings _settings;
2828
private int _lineNumber;

Rubberduck.SmartIndenter/LogicalCodeLine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public bool IsCommentBlock
126126
get { return _lines.All(x => x.ContainsOnlyComment); }
127127
}
128128

129-
private static readonly Regex OperatorIgnoreRegex = new Regex(@"^(\d*\s)?\s*[+&]\s");
129+
private static readonly Regex OperatorIgnoreRegex = new Regex(@"^(\d*\s)?\s*[+&]\s", RegexOptions.IgnoreCase);
130130

131131
public string Indented()
132132
{
@@ -180,7 +180,7 @@ public override string ToString()
180180
return _lines.Aggregate(string.Empty, (x, y) => x + y.ToString());
181181
}
182182

183-
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+)");
183+
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+)", RegexOptions.IgnoreCase);
184184
private readonly Stack<AlignmentToken> _alignment = new Stack<AlignmentToken>();
185185

186186
//The splitNamed parameter is a straight up hack for fixing https://github.com/rubberduck-vba/Rubberduck/issues/2402

Rubberduck.SmartIndenter/StringLiteralAndBracketEscaper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ internal class StringLiteralAndBracketEscaper
99
public const char StringPlaceholder = '\a';
1010
public const char BracketPlaceholder = '\x02';
1111

12-
private static readonly Regex StringReplaceRegex = new Regex("\a+");
13-
private static readonly Regex BracketReplaceRegex = new Regex("\x02+");
12+
private static readonly Regex StringReplaceRegex = new Regex("\a+", RegexOptions.IgnoreCase);
13+
private static readonly Regex BracketReplaceRegex = new Regex("\x02+", RegexOptions.IgnoreCase);
1414

1515
private readonly List<string> _strings = new List<string>();
1616
private readonly List<string> _brackets = new List<string>();

RubberduckTests/SmartIndenter/MiscAndCornerCaseTests.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,47 @@ namespace RubberduckTests.SmartIndenter
88
[TestClass]
99
public class MiscAndCornerCaseTests
1010
{
11+
[TestMethod]
12+
[TestCategory("Indenter")]
13+
public void LowerCaseKeywordNext()
14+
{
15+
var code = new[]
16+
{
17+
"Dim i As Long",
18+
"Dim maxRow As Long",
19+
"",
20+
"maxRow = 120",
21+
"For i = 20 To maxRow Step 19",
22+
"",
23+
"Sheet5.Range(Rows(i), Rows(i + 4)).Cut Sheet61.Range(Rows(i))",
24+
"next i"
25+
};
26+
27+
var output = new[]
28+
{
29+
"Dim i As Long",
30+
"Dim maxRow As Long",
31+
"",
32+
"maxRow = 120",
33+
"For i = 20 To maxRow Step 19",
34+
"",
35+
" Sheet5.Range(Rows(i), Rows(i + 4)).Cut Sheet61.Range(Rows(i))",
36+
"next i"
37+
};
38+
39+
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
40+
var actual = indenter.Indent(code);
41+
Assert.IsTrue(output.SequenceEqual(actual));
42+
}
43+
1144
[TestMethod]
1245
[TestCategory("Indenter")]
1346
public void DeclareFunctionsDoNotIndentNextLine()
1447
{
1548
var code = new[]
1649
{
17-
@"Public Declare Function Foo Lib ""bar.dll"" (X As Any) As Variant",
18-
@"Public Declare Sub Bar Lib ""bar.dll"" (Y As Integer)"
50+
@"Public Declare Function Foo Lib ""bar.dll"" (X As Any) As Variant",
51+
@"Public Declare Sub Bar Lib ""bar.dll"" (Y As Integer)"
1952
};
2053

2154
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());

0 commit comments

Comments
 (0)