Skip to content

Commit bf66e6a

Browse files
committed
Enhance SingleLineComment
Doesn't copy the code of SingleLineComment any more
1 parent 87529ea commit bf66e6a

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

FluentMigrator.BatchParser/RangeSearchers/PoundSignSingleLineComment.cs

+2-37
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,19 @@
1414
// limitations under the License.
1515
#endregion
1616

17-
using System.Text.RegularExpressions;
18-
1917
namespace FluentMigrator.BatchParser.RangeSearchers
2018
{
2119
/// <summary>
2220
/// A single line comment starting with a pound sign (<c># comment</c>)
2321
/// </summary>
24-
public sealed class PoundSignSingleLineComment : IRangeSearcher
22+
public sealed class PoundSignSingleLineComment : SingleLineComment
2523
{
26-
private readonly Regex _startCodeRegex;
27-
2824
/// <summary>
2925
/// Initializes a new instance of the <see cref="PoundSignSingleLineComment"/> class.
3026
/// </summary>
3127
public PoundSignSingleLineComment()
28+
: base("#", true)
3229
{
33-
var startCode = "#";
34-
_startCodeRegex = new Regex(Regex.Escape(startCode), RegexOptions.CultureInvariant | RegexOptions.Compiled);
35-
StartCodeLength = startCode.Length;
36-
}
37-
38-
/// <inheritdoc />
39-
public int StartCodeLength { get; }
40-
41-
/// <inheritdoc />
42-
public int EndCodeLength => 0;
43-
44-
/// <inheritdoc />
45-
public bool IsComment => true;
46-
47-
/// <inheritdoc />
48-
public int FindStartCode(ILineReader reader)
49-
{
50-
if (reader.Index != 0)
51-
return -1;
52-
var match = _startCodeRegex.Match(reader.Line, reader.Index);
53-
if (!match.Success)
54-
return -1;
55-
var skippedText = reader.ReadString(match.Index - reader.Index);
56-
if (!string.IsNullOrWhiteSpace(skippedText))
57-
return -1;
58-
return match.Index;
59-
}
60-
61-
/// <inheritdoc />
62-
public EndCodeSearchResult FindEndCode(ILineReader reader)
63-
{
64-
return reader.Line.Length;
6530
}
6631
}
6732
}

FluentMigrator.BatchParser/RangeSearchers/SingleLineComment.cs

+20
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,26 @@ namespace FluentMigrator.BatchParser.RangeSearchers
2424
/// </summary>
2525
public class SingleLineComment : IRangeSearcher
2626
{
27+
private readonly bool _onlyAtBeginningOfLine;
2728
private readonly Regex _startCodeRegex;
2829

2930
/// <summary>
3031
/// Initializes a new instance of the <see cref="SingleLineComment"/> class.
3132
/// </summary>
3233
/// <param name="startCode">The start code for the single line comment</param>
3334
public SingleLineComment(string startCode)
35+
: this(startCode, false)
3436
{
37+
}
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="SingleLineComment"/> class.
41+
/// </summary>
42+
/// <param name="startCode">The start code for the single line comment</param>
43+
/// <param name="onlyAtBeginningOfLine">Find the start code only at the beginning of the line</param>
44+
public SingleLineComment(string startCode, bool onlyAtBeginningOfLine)
45+
{
46+
_onlyAtBeginningOfLine = onlyAtBeginningOfLine;
3547
_startCodeRegex = new Regex(Regex.Escape(startCode), RegexOptions.CultureInvariant | RegexOptions.Compiled);
3648
StartCodeLength = startCode.Length;
3749
}
@@ -51,6 +63,14 @@ public int FindStartCode(ILineReader reader)
5163
var match = _startCodeRegex.Match(reader.Line, reader.Index);
5264
if (!match.Success)
5365
return -1;
66+
67+
if (_onlyAtBeginningOfLine)
68+
{
69+
var skippedText = reader.Line.Substring(0, match.Index);
70+
if (!string.IsNullOrWhiteSpace(skippedText))
71+
return -1;
72+
}
73+
5474
return match.Index;
5575
}
5676

0 commit comments

Comments
 (0)