Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit fa03bcd

Browse files
fix bug in TagComment parsing logic: Mono's C# parser creates comment tokens with wrong line endings in the comment text
1 parent e537efb commit fa03bcd

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/Parser.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,29 @@ void AddCommentTags(SyntaxTree cu, IList<TagComment> tagComments, ITextSource fi
109109
foreach (var comment in cu.Descendants.OfType<Comment>()) {
110110
if (comment.CommentType == CommentType.InactiveCode)
111111
continue;
112-
int matchLength;
113-
int index = comment.Content.IndexOfAny(TaskListTokens, 0, out matchLength);
114-
if (index > -1) {
112+
string match;
113+
if (comment.Content.ContainsAny(TaskListTokens, 0, out match)) {
115114
if (document == null)
116115
document = new ReadOnlyDocument(fileContent, fileName);
117116
int commentSignLength = comment.CommentType == CommentType.Documentation || comment.CommentType == CommentType.MultiLineDocumentation ? 3 : 2;
118117
int commentEndSignLength = comment.CommentType == CommentType.MultiLine || comment.CommentType == CommentType.MultiLineDocumentation ? 2 : 0;
119118
int commentStartOffset = document.GetOffset(comment.StartLocation) + commentSignLength;
120119
int commentEndOffset = document.GetOffset(comment.EndLocation) - commentEndSignLength;
120+
int endOffset;
121+
int searchOffset = 0;
121122
do {
122-
int absoluteOffset = commentStartOffset + index;
123+
int start = commentStartOffset + searchOffset;
124+
int absoluteOffset = document.IndexOf(match, start, document.TextLength - start, StringComparison.Ordinal);
123125
var startLocation = document.GetLocation(absoluteOffset);
124-
int endOffset = Math.Min(document.GetLineByNumber(startLocation.Line).EndOffset, commentEndOffset);
126+
endOffset = Math.Min(document.GetLineByNumber(startLocation.Line).EndOffset, commentEndOffset);
125127
string content = document.GetText(absoluteOffset, endOffset - absoluteOffset);
126-
if (content.Length < matchLength) {
128+
if (content.Length < match.Length) {
127129
// HACK: workaround parser bug with multi-line documentation comments
128130
break;
129131
}
130-
tagComments.Add(new TagComment(content.Substring(0, matchLength), new DomRegion(cu.FileName, startLocation.Line, startLocation.Column), content.Substring(matchLength)));
131-
index = comment.Content.IndexOfAny(TaskListTokens, endOffset - commentStartOffset, out matchLength);
132-
} while (index > -1);
132+
tagComments.Add(new TagComment(content.Substring(0, match.Length), new DomRegion(cu.FileName, startLocation.Line, startLocation.Column), content.Substring(match.Length)));
133+
searchOffset = endOffset - commentStartOffset;
134+
} while (comment.Content.ContainsAny(TaskListTokens, searchOffset, out match));
133135
}
134136
}
135137
}

src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Utils/CharRope.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static string ToString(this Rope<char> rope, int startIndex, int length)
3535
{
3636
if (rope == null)
3737
throw new ArgumentNullException("rope");
38+
#if DEBUG
39+
if (length < 0)
40+
throw new ArgumentOutOfRangeException("length", length, "Value must be >= 0");
41+
#endif
3842
if (length == 0)
3943
return string.Empty;
4044
char[] buffer = new char[length];

src/Main/Base/Project/Util/SharpDevelopExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,24 @@ public static int IndexOfAny(this string haystack, IEnumerable<string> needles,
766766
return index;
767767
}
768768

769+
public static bool ContainsAny(this string haystack, IEnumerable<string> needles, int startIndex, out string match)
770+
{
771+
if (haystack == null)
772+
throw new ArgumentNullException("haystack");
773+
if (needles == null)
774+
throw new ArgumentNullException("needles");
775+
int index = -1;
776+
match = null;
777+
foreach (var needle in needles) {
778+
int i = haystack.IndexOf(needle, startIndex, StringComparison.Ordinal);
779+
if (i != -1 && (index == -1 || index > i)) {
780+
index = i;
781+
match = needle;
782+
}
783+
}
784+
return index > -1;
785+
}
786+
769787
/// <summary>
770788
/// Retrieves a hash code for the specified string that is stable across
771789
/// multiple runs of SharpDevelop and .NET upgrades.

0 commit comments

Comments
 (0)