Skip to content

Commit cfc1d15

Browse files
authored
Move ICollectionExtensions to MS.CA.Collections (#78384)
1 parent 15ec676 commit cfc1d15

File tree

14 files changed

+46
-79
lines changed

14 files changed

+46
-79
lines changed
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,35 @@
77
using System.Collections.Immutable;
88
using Microsoft.CodeAnalysis.PooledObjects;
99

10-
namespace Roslyn.Utilities;
10+
namespace Microsoft.CodeAnalysis;
1111

1212
internal static class ICollectionExtensions
1313
{
14-
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T>? values)
14+
public static void RemoveRange<T>(this ICollection<T> collection, IEnumerable<T>? items)
1515
{
16-
if (collection == null)
16+
if (items != null)
1717
{
18-
throw new ArgumentNullException(nameof(collection));
18+
foreach (var item in items)
19+
{
20+
collection.Remove(item);
21+
}
1922
}
23+
}
24+
25+
public static void AddIfNotNull<T>(this ICollection<T> collection, T? value) where T : struct
26+
{
27+
if (value != null)
28+
collection.Add(value.Value);
29+
}
30+
31+
public static void AddIfNotNull<T>(this ICollection<T> collection, T? value) where T : class
32+
{
33+
if (value != null)
34+
collection.Add(value);
35+
}
2036

37+
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T>? values)
38+
{
2139
if (values != null)
2240
{
2341
foreach (var item in values)
@@ -29,9 +47,6 @@ public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T>? v
2947

3048
public static void AddRange<T>(this ICollection<T> collection, ArrayBuilder<T>? values)
3149
{
32-
if (collection == null)
33-
throw new ArgumentNullException(nameof(collection));
34-
3550
if (values != null)
3651
{
3752
foreach (var item in values)
@@ -41,9 +56,6 @@ public static void AddRange<T>(this ICollection<T> collection, ArrayBuilder<T>?
4156

4257
public static void AddRange<T>(this ICollection<T> collection, HashSet<T>? values)
4358
{
44-
if (collection == null)
45-
throw new ArgumentNullException(nameof(collection));
46-
4759
if (values != null)
4860
{
4961
foreach (var item in values)
@@ -53,9 +65,6 @@ public static void AddRange<T>(this ICollection<T> collection, HashSet<T>? value
5365

5466
public static void AddRange<TKey, TValue>(this ICollection<TKey> collection, Dictionary<TKey, TValue>.KeyCollection? keyCollection) where TKey : notnull
5567
{
56-
if (collection == null)
57-
throw new ArgumentNullException(nameof(collection));
58-
5968
if (keyCollection != null)
6069
{
6170
foreach (var key in keyCollection)
@@ -65,11 +74,6 @@ public static void AddRange<TKey, TValue>(this ICollection<TKey> collection, Dic
6574

6675
public static void AddRange<T>(this ICollection<T> collection, ImmutableArray<T> values)
6776
{
68-
if (collection == null)
69-
{
70-
throw new ArgumentNullException(nameof(collection));
71-
}
72-
7377
if (!values.IsDefault)
7478
{
7579
foreach (var item in values)

src/Dependencies/Collections/Extensions/EnumerableExtensions.cs renamed to src/Dependencies/Collections/Extensions/IEnumerableExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,19 @@ public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
347347
return source.Where((Func<T?, bool>)s_notNullTest)!;
348348
}
349349

350+
public static ImmutableArray<T> WhereAsArray<T, TArg>(this IEnumerable<T> values, Func<T, TArg, bool> predicate, TArg arg)
351+
{
352+
var result = ArrayBuilder<T>.GetInstance();
353+
354+
foreach (var value in values)
355+
{
356+
if (predicate(value, arg))
357+
result.Add(value);
358+
}
359+
360+
return result.ToImmutableAndFree();
361+
}
362+
350363
public static T[] AsArray<T>(this IEnumerable<T> source)
351364
=> source as T[] ?? source.ToArray();
352365

src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Collections.Immutable;
88
using System.Diagnostics;
9-
using System.Diagnostics.CodeAnalysis;
109
using System.IO;
1110
using System.Runtime.CompilerServices;
1211
using System.Runtime.InteropServices;

src/Dependencies/Collections/Microsoft.CodeAnalysis.Collections.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@
6666
<Compile Include="$(MSBuildThisFileDirectory)OneOrMany.cs" />
6767
<Compile Include="$(MSBuildThisFileDirectory)TemporaryArray`1.cs" />
6868
<Compile Include="$(MSBuildThisFileDirectory)TemporaryArrayExtensions.cs" />
69-
<Compile Include="$(MSBuildThisFileDirectory)Extensions\EnumerableExtensions.cs" />
69+
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IEnumerableExtensions.cs" />
7070
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ImmutableArrayExtensions.cs" />
71+
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ICollectionExtensions.cs" />
7172
<Compile Include="$(MSBuildThisFileDirectory)Specialized\SpecializedCollections.cs" />
7273
<Compile Include="$(MSBuildThisFileDirectory)Specialized\SpecializedCollections.Empty.Collection.cs" />
7374
<Compile Include="$(MSBuildThisFileDirectory)Specialized\SpecializedCollections.Empty.cs" />

src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionBatchFixAllProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ internal abstract class AbstractSuppressionBatchFixAllProvider : FixAllProvider
8989
// Determine the set of documents to actually fix. We can also use this to update the progress bar with
9090
// the amount of remaining work to perform. We'll update the progress bar as we compute each fix in
9191
// AddDocumentFixesAsync.
92-
var source = documentsAndDiagnosticsToFixMap.WhereAsArray(static (kvp, _) => !kvp.Value.IsDefaultOrEmpty, state: false);
92+
var source = documentsAndDiagnosticsToFixMap.WhereAsArray(static (kvp, _) => !kvp.Value.IsDefaultOrEmpty, arg: false);
9393
progressTracker.AddItems(source.Length);
9494

9595
return await ProducerConsumer<(Diagnostic diagnostic, CodeAction action)>.RunParallelAsync(

src/Features/ExternalAccess/Copilot/Internal/Analyzer/AbstractCopilotCodeAnalysisService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.CodeAnalysis.QuickInfo;
1717
using Microsoft.CodeAnalysis.Shared.Extensions;
1818
using Microsoft.CodeAnalysis.Text;
19+
using Roslyn.Utilities;
1920

2021
namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Internal.Analyzer;
2122

@@ -167,7 +168,7 @@ public async Task<ImmutableArray<Diagnostic>> GetCachedDocumentDiagnosticsAsync(
167168

168169
protected virtual Task<ImmutableArray<Diagnostic>> GetDiagnosticsIntersectWithSpanAsync(Document document, IReadOnlyList<Diagnostic> diagnostics, TextSpan span, CancellationToken cancellationToken)
169170
{
170-
return Task.FromResult(diagnostics.WhereAsArray((diagnostic, _) => diagnostic.Location.SourceSpan.IntersectsWith(span), state: (object?)null));
171+
return Task.FromResult(diagnostics.WhereAsArray(static (diagnostic, span) => diagnostic.Location.SourceSpan.IntersectsWith(span), span));
171172
}
172173

173174
public async Task StartRefinementSessionAsync(Document oldDocument, Document newDocument, Diagnostic? primaryDiagnostic, CancellationToken cancellationToken)

src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Testing/TestRunner.TestRunHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1313
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
1414
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
15+
using Roslyn.Utilities;
1516

1617
namespace Microsoft.CodeAnalysis.LanguageServer.Testing;
1718

src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorSymbolReferenceFinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Threading.Tasks;
1111
using Microsoft.CodeAnalysis.LanguageService;
1212
using Microsoft.CodeAnalysis.Shared.Extensions;
13+
using Roslyn.Utilities;
1314

1415
namespace Microsoft.CodeAnalysis.FindSymbols.Finders;
1516

src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitConversionSymbolReferenceFinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.CodeAnalysis.LanguageService;
1111
using Microsoft.CodeAnalysis.PooledObjects;
1212
using Microsoft.CodeAnalysis.Shared.Extensions;
13+
using Roslyn.Utilities;
1314

1415
namespace Microsoft.CodeAnalysis.FindSymbols.Finders;
1516

src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.CodeAnalysis.LanguageService;
1212
using Microsoft.CodeAnalysis.PooledObjects;
1313
using Microsoft.CodeAnalysis.Shared.Extensions;
14+
using Roslyn.Utilities;
1415

1516
namespace Microsoft.CodeAnalysis.FindSymbols.Finders;
1617

0 commit comments

Comments
 (0)