|
| 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