Skip to content

Commit 961286c

Browse files
committed
Implement SyntaxTriviaList.RemoveRange extension method
1 parent a1189aa commit 961286c

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/TriviaHelper.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,53 @@ internal static int IndexOfTrailingWhitespace(IReadOnlyList<SyntaxTrivia> trivia
112112
return (whiteSpaceStartIndex < triviaList.Count) ? whiteSpaceStartIndex : -1;
113113
}
114114

115+
/// <summary>
116+
/// Removes a range of elements from the <see cref="SyntaxTriviaList"/>.
117+
/// </summary>
118+
/// <param name="list">The list to remove elements from.</param>
119+
/// <param name="index">The zero-based starting index of the range of elements to remove.</param>
120+
/// <param name="count">The number of elements to remove.</param>
121+
/// <returns>A copy of <paramref name="list"/> with the specified range of elements removed.</returns>
122+
/// <exception cref="ArgumentOutOfRangeException">
123+
/// <para>If <paramref name="index"/> is less than 0.</para>
124+
/// <para>-or-</para>
125+
/// <para>If <paramref name="count"/> is less than 0.</para>
126+
/// </exception>
127+
/// <exception cref="ArgumentException">
128+
/// <para>If <paramref name="index"/> and <paramref name="count"/> do not denote a valid range of elements in
129+
/// the <see cref="SyntaxTriviaList"/>.</para>
130+
/// </exception>
131+
internal static SyntaxTriviaList RemoveRange(this SyntaxTriviaList list, int index, int count)
132+
{
133+
if (index < 0)
134+
{
135+
throw new ArgumentOutOfRangeException(nameof(index));
136+
}
137+
138+
if (count < 0)
139+
{
140+
throw new ArgumentOutOfRangeException(nameof(count));
141+
}
142+
143+
if (index > list.Count - count)
144+
{
145+
throw new ArgumentException("The specified range of elements does not exist in the list.");
146+
}
147+
148+
SyntaxTrivia[] trivia = new SyntaxTrivia[list.Count - count];
149+
for (int i = 0; i < index; i++)
150+
{
151+
trivia[i] = list[i];
152+
}
153+
154+
for (int i = index; i + count < list.Count; i++)
155+
{
156+
trivia[i] = list[i + count];
157+
}
158+
159+
return SyntaxFactory.TriviaList(trivia);
160+
}
161+
115162
/// <summary>
116163
/// Returns the index of the last trivia of a specified kind in the trivia list.
117164
/// </summary>

StyleCop.Analyzers/StyleCop.Analyzers/LayoutRules/SA1506CodeFixProvider.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
6262
case SyntaxKind.EndOfLineTrivia:
6363
if (onBlankLine)
6464
{
65-
for (int i = 0; i < currentIndex - currentLineStart + 1; i++)
66-
{
67-
triviaList = triviaList.RemoveAt(currentLineStart);
68-
currentIndex = currentLineStart - 1;
69-
}
70-
65+
triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
66+
currentIndex = currentLineStart - 1;
7167
continue;
7268
}
7369
else

0 commit comments

Comments
 (0)