Skip to content

Commit 9c8daf0

Browse files
committed
implemented ParseComments
1 parent cc4e64b commit 9c8daf0

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

RetailCoder.VBE/VBA/StringExtensions.cs

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Text;
4+
using System.Text.RegularExpressions;
45

56
namespace Rubberduck.VBA.Grammar
67
{
@@ -36,9 +37,9 @@ public static string StripTrailingComment(this string line)
3637
/// <summary>
3738
/// Returns a value indicating whether line of code is/contains a comment.
3839
/// </summary>
39-
/// <param name="line"></param>
40-
/// <param name="index">Returns the start index of the comment string, including the comment marker.</param>
41-
/// <returns></returns>
40+
/// <param name="line">The extended string.</param>
41+
/// <param name="index">The start index of the comment string.</param>
42+
/// <returns>Returns <c>true</c> if specified string contains a VBA comment marker.</returns>
4243
public static bool HasComment(this string line, out int index)
4344
{
4445
var instruction = line.StripStringLiterals();
@@ -53,41 +54,46 @@ public static bool HasComment(this string line, out int index)
5354
return index >= 0;
5455
}
5556

57+
public static string StripStringLiterals(this string line)
58+
{
59+
return Regex.Replace(line, "\"[^\"]*\"", match => new string(' ', match.Length));
60+
}
61+
5662
/// <summary>
5763
/// Strips all string literals from a line of code or instruction.
5864
/// Replaces string literals with whitespace characters, to maintain original length.
5965
/// </summary>
6066
/// <param name="line"></param>
6167
/// <returns>Returns a new string, stripped of all string literals and string delimiters.</returns>
62-
public static string StripStringLiterals(this string line)
63-
{
64-
var builder = new StringBuilder(line.Length);
65-
var isInsideString = false;
66-
for (var cursor = 0; cursor < line.Length; cursor++)
67-
{
68-
if (line[cursor] == StringDelimiter)
69-
{
70-
if (isInsideString)
71-
{
72-
isInsideString = cursor + 1 == line.Length || line[cursor + 1] == StringDelimiter || cursor > 0 && (line[cursor - 1] == StringDelimiter);
73-
}
74-
else
75-
{
76-
isInsideString = true;
77-
}
78-
}
68+
//public static string StripStringLiterals(this string line)
69+
//{
70+
// var builder = new StringBuilder(line.Length);
71+
// var isInsideString = false;
72+
// for (var cursor = 0; cursor < line.Length; cursor++)
73+
// {
74+
// if (line[cursor] == StringDelimiter)
75+
// {
76+
// if (isInsideString)
77+
// {
78+
// isInsideString = cursor + 1 == line.Length || line[cursor + 1] == StringDelimiter || cursor > 0 && (line[cursor - 1] == StringDelimiter);
79+
// }
80+
// else
81+
// {
82+
// isInsideString = true;
83+
// }
84+
// }
7985

80-
if (!isInsideString && line[cursor] != StringDelimiter)
81-
{
82-
builder.Append(line[cursor]);
83-
}
84-
else
85-
{
86-
builder.Append(' ');
87-
}
88-
}
86+
// if (!isInsideString && line[cursor] != StringDelimiter)
87+
// {
88+
// builder.Append(line[cursor]);
89+
// }
90+
// else
91+
// {
92+
// builder.Append(' ');
93+
// }
94+
// }
8995

90-
return builder.ToString();
91-
}
96+
// return builder.ToString();
97+
//}
9298
}
9399
}

RetailCoder.VBE/VBA/VBParser.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,23 @@ public IEnumerable<CommentNode> ParseComments(VBComponent component)
9090
if (continuing || line.HasComment(out index))
9191
{
9292
startLine = continuing ? startLine : i;
93-
startColumn = index + 1; // VBE positions are 1-based
93+
startColumn = index;
94+
95+
var commentLength = line.Length - index;
9496

9597
continuing = line.EndsWith("_");
9698
if (!continuing)
9799
{
98-
commentBuilder.Append(line);
99-
var selection = new Selection(startLine, startColumn, i, line.Length);
100+
commentBuilder.Append(line.Substring(startColumn, commentLength).Trim());
101+
var selection = new Selection(startLine + 1, startColumn + 1, i, line.Length);
100102
yield return new CommentNode(commentBuilder.ToString(), new QualifiedSelection(qualifiedName, selection));
101103
commentBuilder.Clear();
102104
}
103105
else
104106
{
105107
// ignore line continuations in comment text:
106-
commentBuilder.Append(line.Remove(line.Length - 1));
108+
109+
commentBuilder.Append(line.Remove(line.Length - 1).TrimStart());
107110
}
108111
}
109112
}

0 commit comments

Comments
 (0)