Skip to content

Commit 678a85e

Browse files
committed
Minor formatting cleanup, additional unit tests and implement EndLine() extensions
1 parent c1a8e9c commit 678a85e

File tree

2 files changed

+98
-25
lines changed

2 files changed

+98
-25
lines changed

Rubberduck.Parsing/SelectionExtensions.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ public static class SelectionExtensions
1414
public static bool Contains(this Selection selection, IToken token)
1515
{
1616
return
17-
(((selection.StartLine == token.Line) && (selection.StartColumn - 1) <= token.Column) || (selection.StartLine < token.Line))
18-
&& (((selection.EndLine == token.Line) && (selection.EndColumn - 1) >= (token.EndColumn())) || (selection.EndLine > token.Line));
17+
(((selection.StartLine == token.Line) && (selection.StartColumn - 1) <= token.Column)
18+
|| (selection.StartLine < token.Line))
19+
&& (((selection.EndLine == token.EndLine()) && (selection.EndColumn - 1) >= (token.EndColumn()))
20+
|| (selection.EndLine > token.EndLine()));
1921
}
2022

2123
public static bool Contains(this ParserRuleContext context, Selection selection)
2224
{
2325
return
24-
(((selection.StartLine == context.Start.Line) && (selection.StartColumn - 1) <= context.Start.Column) || (selection.StartLine < context.Start.Line))
25-
&& (((selection.EndLine == context.Stop.Line) && (selection.EndColumn - 1) >= (context.Stop.EndColumn())) || (selection.EndLine > context.Stop.Line));
26+
(((selection.StartLine == context.Start.Line) && (selection.StartColumn - 1) <= context.Start.Column)
27+
|| (selection.StartLine < context.Start.Line))
28+
&& (((selection.EndLine == context.Stop.EndLine()) && (selection.EndColumn - 1) >= (context.Stop.EndColumn()))
29+
|| (selection.EndLine > context.Stop.EndLine()));
2630
}
2731

2832
/// <summary>
@@ -37,15 +41,26 @@ public static int EndColumn(this IToken token)
3741
if (token.Text.Contains(Environment.NewLine))
3842
{
3943
var splitStrings = token.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
40-
var tokenOnLastLine = splitStrings[splitStrings.Length - 1];
44+
var lastOccupiedLine = splitStrings[splitStrings.Length - 1];
4145

42-
return tokenOnLastLine.Length;
46+
return lastOccupiedLine.Length;
4347
}
4448
else
4549
{
4650
return token.Column + token.Text.Length;
4751
}
52+
}
53+
54+
public static int EndLine(this IToken token)
55+
{
56+
if(token.Text.Contains(Environment.NewLine))
57+
{
58+
var splitStrings = token.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
59+
60+
return token.Line + (splitStrings.Length - 1);
61+
}
4862

63+
return token.Line;
4964
}
5065
}
5166
}

RubberduckTests/Grammar/SelectionExtensionsTests.cs

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,36 @@ namespace RubberduckTests.Grammar
1313
[TestClass]
1414
public class SelectionExtensionsTests
1515
{
16-
public class CollectorVBAParserBaseVisitor<Result> : VBAParserBaseVisitor<IList<Result>>
16+
public class CollectorVBAParserBaseVisitor<Result> : VBAParserBaseVisitor<IEnumerable<Result>>
1717
{
18-
private List<Result> defaultResult = new List<Result>();
18+
protected override IEnumerable<Result> DefaultResult => new List<Result>();
1919

20-
protected override IList<Result> DefaultResult => defaultResult;
21-
/*
22-
protected override IList<Result> AggregateResult(IList<Result> firstResult, IList<Result> secondResult)
20+
protected override IEnumerable<Result> AggregateResult(IEnumerable<Result> firstResult, IEnumerable<Result> secondResult)
2321
{
24-
if (firstResult != null && secondResult != null)
25-
return firstResult.Concat(secondResult) as IList<Result>;
26-
27-
if (secondResult == null)
28-
return firstResult;
29-
30-
return secondResult;
31-
}*/
22+
return firstResult.Concat(secondResult);
23+
}
3224
}
3325

3426
public class SubStmtContextElementCollectorVisitor : CollectorVBAParserBaseVisitor<SubStmtContext>
3527
{
36-
public override IList<SubStmtContext> VisitSubStmt([NotNull] SubStmtContext context)
28+
public override IEnumerable<SubStmtContext> VisitSubStmt([NotNull] SubStmtContext context)
3729
{
38-
DefaultResult.Add(context);
39-
return base.VisitSubStmt(context);
30+
return new List<SubStmtContext> { context };
4031
}
4132
}
4233

4334
public class IfStmtContextElementCollectorVisitor : CollectorVBAParserBaseVisitor<IfStmtContext>
4435
{
45-
public override IList<IfStmtContext> VisitIfStmt([NotNull] IfStmtContext context)
36+
public override IEnumerable<IfStmtContext> VisitIfStmt([NotNull] IfStmtContext context)
4637
{
47-
DefaultResult.Add(context);
48-
return base.VisitIfStmt(context);
38+
base.VisitIfStmt(context);
39+
return new List<IfStmtContext> { context };
4940
}
5041
}
5142

5243
[TestMethod]
5344
[TestCategory("Grammar")]
45+
[TestCategory("Selection")]
5446
public void Context_Not_In_Selection_ZeroBased_EvilCode()
5547
{
5648
const string inputCode = @"
@@ -78,6 +70,7 @@ End _
7870

7971
[TestMethod]
8072
[TestCategory("Grammar")]
73+
[TestCategory("Selection")]
8174
public void Context_In_Selection_OneBased_EvilCode()
8275
{
8376
const string inputCode = @"
@@ -105,6 +98,63 @@ End _
10598

10699
[TestMethod]
107100
[TestCategory("Grammar")]
101+
[TestCategory("Selection")]
102+
public void Context_Not_In_Selection_Start_OneBased_EvilCode()
103+
{
104+
const string inputCode = @"
105+
Option Explicit
106+
107+
Public _
108+
Sub _
109+
foo()
110+
111+
Debug.Print ""foo""
112+
113+
End _
114+
Sub : 'Lame comment!
115+
";
116+
117+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
118+
var state = MockParser.CreateAndParse(vbe.Object);
119+
var tree = state.GetParseTree(new QualifiedModuleName(component));
120+
var visitor = new SubStmtContextElementCollectorVisitor();
121+
var context = visitor.Visit(tree).First();
122+
var selection = new Selection(5, 1, 11, 8);
123+
124+
Assert.IsFalse(context.Contains(selection));
125+
}
126+
127+
[TestMethod]
128+
[TestCategory("Grammar")]
129+
[TestCategory("Selection")]
130+
public void Context_Not_In_Selection_End_OneBased_EvilCode()
131+
{
132+
const string inputCode = @"
133+
Option Explicit
134+
135+
Public _
136+
Sub _
137+
foo()
138+
139+
Debug.Print ""foo""
140+
141+
End _
142+
Sub : 'Lame comment!
143+
";
144+
145+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
146+
var state = MockParser.CreateAndParse(vbe.Object);
147+
var tree = state.GetParseTree(new QualifiedModuleName(component));
148+
var visitor = new SubStmtContextElementCollectorVisitor();
149+
var context = visitor.Visit(tree).First();
150+
var selection = new Selection(4, 1, 10, 8);
151+
152+
Assert.IsFalse(context.Contains(selection));
153+
}
154+
155+
[TestMethod]
156+
[TestCategory("Grammar")]
157+
[TestCategory("Selection")]
108158
public void Context_In_GetSelection_OneBased_EvilCode()
109159
{
110160
const string inputCode = @"
@@ -133,6 +183,7 @@ End _
133183

134184
[TestMethod]
135185
[TestCategory("Grammar")]
186+
[TestCategory("Selection")]
136187
public void Context_Not_In_GetSelection_ZeroBased()
137188
{
138189
const string inputCode = @"
@@ -158,6 +209,7 @@ Public Sub foo()
158209

159210
[TestMethod]
160211
[TestCategory("Grammar")]
212+
[TestCategory("Selection")]
161213
public void Context_In_GetSelection_OneBased()
162214
{
163215
const string inputCode = @"
@@ -183,6 +235,7 @@ Public Sub foo()
183235

184236
[TestMethod]
185237
[TestCategory("Grammar")]
238+
[TestCategory("Selection")]
186239
public void Context_In_Selection_OneBased()
187240
{
188241
const string inputCode = @"
@@ -208,6 +261,7 @@ Public Sub foo()
208261

209262
[TestMethod]
210263
[TestCategory("Grammar")]
264+
[TestCategory("Selection")]
211265
public void Context_NotIn_Selection_StartTooSoon_OneBased()
212266
{
213267
const string inputCode = @"
@@ -233,6 +287,7 @@ Public Sub foo()
233287

234288
[TestMethod]
235289
[TestCategory("Grammar")]
290+
[TestCategory("Selection")]
236291
public void Context_NotIn_Selection_EndsTooSoon_OneBased()
237292
{
238293
const string inputCode = @"
@@ -293,6 +348,7 @@ End If
293348

294349
[TestMethod]
295350
[TestCategory("Grammar")]
351+
[TestCategory("Selection")]
296352
public void Context_Not_In_Selection_SecondBlock_OneBased()
297353
{
298354
const string inputCode = @"
@@ -328,6 +384,7 @@ End If
328384

329385
[TestMethod]
330386
[TestCategory("Grammar")]
387+
[TestCategory("Selection")]
331388
public void Context_In_Selection_SecondBlock_OneBased()
332389
{
333390
const string inputCode = @"
@@ -363,6 +420,7 @@ End If
363420

364421
[TestMethod]
365422
[TestCategory("Grammar")]
423+
[TestCategory("Selection")]
366424
public void Selection_Contains_LastToken()
367425
{
368426
const string inputCode = @"

0 commit comments

Comments
 (0)