Skip to content

Commit 2e6907c

Browse files
authored
Merge pull request #2371 from retailcoder/next
fix for HasComment and non-standard REM comments
2 parents 058ef42 + 08ecc40 commit 2e6907c

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

Rubberduck.Parsing/VBA/StringExtensions.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text;
45
using System.Text.RegularExpressions;
@@ -8,7 +9,35 @@ namespace Rubberduck.Parsing.VBA
89
{
910
public static class StringExtensions
1011
{
11-
12+
// see issues #1057 and #2364.
13+
private static readonly IList<string> ValidRemCommentMarkers =
14+
new List<string>
15+
{
16+
Tokens.Rem + ' ',
17+
Tokens.Rem + '?',
18+
Tokens.Rem + '<',
19+
Tokens.Rem + '>',
20+
Tokens.Rem + '{',
21+
Tokens.Rem + '}',
22+
Tokens.Rem + '~',
23+
Tokens.Rem + '`',
24+
Tokens.Rem + '!',
25+
Tokens.Rem + '/',
26+
Tokens.Rem + '*',
27+
Tokens.Rem + '(',
28+
Tokens.Rem + ')',
29+
Tokens.Rem + '-',
30+
Tokens.Rem + '=',
31+
Tokens.Rem + '+',
32+
Tokens.Rem + '\\',
33+
Tokens.Rem + '|',
34+
Tokens.Rem + ';',
35+
Tokens.Rem + ':',
36+
Tokens.Rem + '\'',
37+
Tokens.Rem + '"',
38+
Tokens.Rem + ',',
39+
Tokens.Rem + '.',
40+
};
1241

1342
/// <summary>
1443
/// Returns a value indicating whether line of code is/contains a comment.
@@ -23,11 +52,22 @@ public static bool HasComment(this string line, out int index)
2352
index = instruction.IndexOf(Tokens.CommentMarker, StringComparison.InvariantCulture);
2453
if (index >= 0)
2554
{
55+
// line contains a single-quote comment marker
2656
return true;
2757
}
2858

29-
index = instruction.IndexOf(Tokens.Rem + " ", StringComparison.InvariantCulture);
30-
return index >= 0;
59+
// note: REM comment markers are NOT implemented as per language specifications.
60+
// ReSharper disable once ForCanBeConvertedToForeach
61+
for (var i = 0; i < ValidRemCommentMarkers.Count; i++)
62+
{
63+
index = instruction.IndexOf(ValidRemCommentMarkers[i], StringComparison.InvariantCulture);
64+
if (index >= 0)
65+
{
66+
return true;
67+
}
68+
}
69+
70+
return false;
3171
}
3272

3373
public static string StripStringLiterals(this string line)

RubberduckTests/StringExtensionsTests.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void StripsAllStringLiterals()
3333
}
3434

3535
[TestMethod]
36-
public void IsComment()
36+
public void IsComment_StartLineWithSingleQuoteMarker()
3737
{
3838
var instruction = "'Debug.Print mwahaha this is just a comment.";
3939

@@ -45,7 +45,7 @@ public void IsComment()
4545
}
4646

4747
[TestMethod]
48-
public void HasComment()
48+
public void HasComment_EndOfLineSingleQuoteMarkerWithStringLiteral()
4949
{
5050
var comment = "'but this is one.";
5151
var instruction = "Debug.Print \"'this isn't a comment\" " + comment;
@@ -57,6 +57,32 @@ public void HasComment()
5757
Assert.AreEqual(comment, instruction.Substring(index));
5858
}
5959

60+
[TestMethod]
61+
public void HasComment_RemMarkerWithWhitespace()
62+
{
63+
var comment = "Rem this is a comment.";
64+
var instruction = "Debug.Print \"'this isn't a comment\" : " + comment;
65+
66+
int index;
67+
var result = instruction.HasComment(out index);
68+
69+
Assert.IsTrue(result);
70+
Assert.AreEqual(comment, instruction.Substring(index));
71+
}
72+
73+
[TestMethod]
74+
public void HasComment_RemMarkerWithQuestionMark()
75+
{
76+
var comment = "Rem?this is a comment.";
77+
var instruction = comment;
78+
79+
int index;
80+
var result = instruction.HasComment(out index);
81+
82+
Assert.IsTrue(result);
83+
Assert.AreEqual(comment, instruction.Substring(index));
84+
}
85+
6086
[TestMethod]
6187
public void CaseInsensitiveContainsShouldReturnTrue()
6288
{

0 commit comments

Comments
 (0)