Skip to content

Commit da52c6b

Browse files
committed
Create extensions to selection/contexts/tokens for ease of validating the question of whether a selection contains a given context or token, plus associated tests.
1 parent 2bdd5b3 commit da52c6b

File tree

4 files changed

+489
-0
lines changed

4 files changed

+489
-0
lines changed

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<Compile Include="ComReflection\ComProject.cs" />
200200
<Compile Include="Rewriter\IModuleRewriter.cs" />
201201
<Compile Include="PreProcessing\VBAPreprocessorVisitor.cs" />
202+
<Compile Include="SelectionExtensions.cs" />
202203
<Compile Include="SymbolList.cs" />
203204
<Compile Include="Symbols\AliasDeclaration.cs" />
204205
<Compile Include="Symbols\DeclarationFinderFactory.cs" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Antlr4.Runtime;
2+
using Rubberduck.VBEditor;
3+
using System;
4+
5+
namespace Rubberduck.Parsing
6+
{
7+
/// <summary>
8+
/// Provide extensions on selections & contexts/tokens
9+
/// to assist in validating whether a selection contains
10+
/// a given context or a token.
11+
/// </summary>
12+
public static class SelectionExtensions
13+
{
14+
public static bool Contains(this Selection selection, IToken token)
15+
{
16+
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));
19+
}
20+
21+
public static bool Contains(this ParserRuleContext context, Selection selection)
22+
{
23+
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+
}
27+
28+
/// <summary>
29+
/// Because a token can be spread across multiple lines with line continuations
30+
/// it is necessary to do some work to determine the token's actual ending column.
31+
/// Whitespace and newline should be preserved within the token.
32+
/// </summary>
33+
/// <param name="token">The last token within a given context to test</param>
34+
/// <returns></returns>
35+
public static int EndColumn(this IToken token)
36+
{
37+
if (token.Text.Contains(Environment.NewLine))
38+
{
39+
var splitStrings = token.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
40+
var tokenOnLastLine = splitStrings[splitStrings.Length - 1];
41+
42+
return tokenOnLastLine.Length;
43+
}
44+
else
45+
{
46+
return token.Column + token.Text.Length;
47+
}
48+
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)