diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/ICollectionExtensions.cs b/src/Dependencies/Collections/Extensions/ICollectionExtensions.cs similarity index 73% rename from src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/ICollectionExtensions.cs rename to src/Dependencies/Collections/Extensions/ICollectionExtensions.cs index f864393c6a3cb..0f88c8c6faeba 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/ICollectionExtensions.cs +++ b/src/Dependencies/Collections/Extensions/ICollectionExtensions.cs @@ -7,17 +7,35 @@ using System.Collections.Immutable; using Microsoft.CodeAnalysis.PooledObjects; -namespace Roslyn.Utilities; +namespace Microsoft.CodeAnalysis; internal static class ICollectionExtensions { - public static void AddRange(this ICollection collection, IEnumerable? values) + public static void RemoveRange(this ICollection collection, IEnumerable? items) { - if (collection == null) + if (items != null) { - throw new ArgumentNullException(nameof(collection)); + foreach (var item in items) + { + collection.Remove(item); + } } + } + + public static void AddIfNotNull(this ICollection collection, T? value) where T : struct + { + if (value != null) + collection.Add(value.Value); + } + + public static void AddIfNotNull(this ICollection collection, T? value) where T : class + { + if (value != null) + collection.Add(value); + } + public static void AddRange(this ICollection collection, IEnumerable? values) + { if (values != null) { foreach (var item in values) @@ -29,9 +47,6 @@ public static void AddRange(this ICollection collection, IEnumerable? v public static void AddRange(this ICollection collection, ArrayBuilder? values) { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); - if (values != null) { foreach (var item in values) @@ -41,9 +56,6 @@ public static void AddRange(this ICollection collection, ArrayBuilder? public static void AddRange(this ICollection collection, HashSet? values) { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); - if (values != null) { foreach (var item in values) @@ -53,9 +65,6 @@ public static void AddRange(this ICollection collection, HashSet? value public static void AddRange(this ICollection collection, Dictionary.KeyCollection? keyCollection) where TKey : notnull { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); - if (keyCollection != null) { foreach (var key in keyCollection) @@ -65,11 +74,6 @@ public static void AddRange(this ICollection collection, Dic public static void AddRange(this ICollection collection, ImmutableArray values) { - if (collection == null) - { - throw new ArgumentNullException(nameof(collection)); - } - if (!values.IsDefault) { foreach (var item in values) diff --git a/src/Dependencies/Collections/Extensions/EnumerableExtensions.cs b/src/Dependencies/Collections/Extensions/IEnumerableExtensions.cs similarity index 98% rename from src/Dependencies/Collections/Extensions/EnumerableExtensions.cs rename to src/Dependencies/Collections/Extensions/IEnumerableExtensions.cs index 9d9617032ba09..058e1e54e38e7 100644 --- a/src/Dependencies/Collections/Extensions/EnumerableExtensions.cs +++ b/src/Dependencies/Collections/Extensions/IEnumerableExtensions.cs @@ -347,6 +347,19 @@ public static IEnumerable WhereNotNull(this IEnumerable source) return source.Where((Func)s_notNullTest)!; } + public static ImmutableArray WhereAsArray(this IEnumerable values, Func predicate, TArg arg) + { + var result = ArrayBuilder.GetInstance(); + + foreach (var value in values) + { + if (predicate(value, arg)) + result.Add(value); + } + + return result.ToImmutableAndFree(); + } + public static T[] AsArray(this IEnumerable source) => source as T[] ?? source.ToArray(); diff --git a/src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs b/src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs index 3cab1a5c85c9f..a57b085a28477 100644 --- a/src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs +++ b/src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; diff --git a/src/Dependencies/Collections/Microsoft.CodeAnalysis.Collections.projitems b/src/Dependencies/Collections/Microsoft.CodeAnalysis.Collections.projitems index e4388508a4636..3155af37e00c9 100644 --- a/src/Dependencies/Collections/Microsoft.CodeAnalysis.Collections.projitems +++ b/src/Dependencies/Collections/Microsoft.CodeAnalysis.Collections.projitems @@ -66,8 +66,9 @@ - + + diff --git a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionBatchFixAllProvider.cs b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionBatchFixAllProvider.cs index e36fe24b17ec0..330bd176ee8f1 100644 --- a/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionBatchFixAllProvider.cs +++ b/src/Features/Core/Portable/CodeFixes/Suppression/AbstractSuppressionBatchFixAllProvider.cs @@ -89,7 +89,7 @@ internal abstract class AbstractSuppressionBatchFixAllProvider : FixAllProvider // Determine the set of documents to actually fix. We can also use this to update the progress bar with // the amount of remaining work to perform. We'll update the progress bar as we compute each fix in // AddDocumentFixesAsync. - var source = documentsAndDiagnosticsToFixMap.WhereAsArray(static (kvp, _) => !kvp.Value.IsDefaultOrEmpty, state: false); + var source = documentsAndDiagnosticsToFixMap.WhereAsArray(static (kvp, _) => !kvp.Value.IsDefaultOrEmpty, arg: false); progressTracker.AddItems(source.Length); return await ProducerConsumer<(Diagnostic diagnostic, CodeAction action)>.RunParallelAsync( diff --git a/src/Features/ExternalAccess/Copilot/Internal/Analyzer/AbstractCopilotCodeAnalysisService.cs b/src/Features/ExternalAccess/Copilot/Internal/Analyzer/AbstractCopilotCodeAnalysisService.cs index 48af70a1e8696..ae44dadd08e08 100644 --- a/src/Features/ExternalAccess/Copilot/Internal/Analyzer/AbstractCopilotCodeAnalysisService.cs +++ b/src/Features/ExternalAccess/Copilot/Internal/Analyzer/AbstractCopilotCodeAnalysisService.cs @@ -16,6 +16,7 @@ using Microsoft.CodeAnalysis.QuickInfo; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.ExternalAccess.Copilot.Internal.Analyzer; @@ -167,7 +168,7 @@ public async Task> GetCachedDocumentDiagnosticsAsync( protected virtual Task> GetDiagnosticsIntersectWithSpanAsync(Document document, IReadOnlyList diagnostics, TextSpan span, CancellationToken cancellationToken) { - return Task.FromResult(diagnostics.WhereAsArray((diagnostic, _) => diagnostic.Location.SourceSpan.IntersectsWith(span), state: (object?)null)); + return Task.FromResult(diagnostics.WhereAsArray(static (diagnostic, span) => diagnostic.Location.SourceSpan.IntersectsWith(span), span)); } public async Task StartRefinementSessionAsync(Document oldDocument, Document newDocument, Diagnostic? primaryDiagnostic, CancellationToken cancellationToken) diff --git a/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Testing/TestRunner.TestRunHandler.cs b/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Testing/TestRunner.TestRunHandler.cs index ff23961901e3b..2cc948741a56a 100644 --- a/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Testing/TestRunner.TestRunHandler.cs +++ b/src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer/Testing/TestRunner.TestRunHandler.cs @@ -12,6 +12,7 @@ using Microsoft.VisualStudio.TestPlatform.ObjectModel; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.LanguageServer.Testing; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorSymbolReferenceFinder.cs index 01ea7e24cdb94..46be6a928a41d 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ConstructorSymbolReferenceFinder.cs @@ -10,6 +10,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.Shared.Extensions; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols.Finders; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitConversionSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitConversionSymbolReferenceFinder.cs index 379f2f543d2bd..647a4cb6565e2 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitConversionSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/ExplicitConversionSymbolReferenceFinder.cs @@ -10,6 +10,7 @@ using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Shared.Extensions; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols.Finders; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs index 7e50775f7fc1a..e94f628cce45d 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamedTypeSymbolReferenceFinder.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.PooledObjects; using Microsoft.CodeAnalysis.Shared.Extensions; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols.Finders; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs index a37a5aa784526..e64b29bd4b776 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/NamespaceSymbolReferenceFinder.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.PooledObjects; -using Microsoft.CodeAnalysis.Shared.Extensions; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols.Finders; diff --git a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs index 3284a0a82ca4d..869ff256c1691 100644 --- a/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs +++ b/src/Workspaces/Core/Portable/FindSymbols/FindReferences/Finders/OperatorSymbolReferenceFinder.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using Microsoft.CodeAnalysis.LanguageService; using Microsoft.CodeAnalysis.Shared.Extensions; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.FindSymbols.Finders; diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CompilerExtensions.projitems b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CompilerExtensions.projitems index 513c2c6c07907..f8c115b73eff7 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CompilerExtensions.projitems +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/CompilerExtensions.projitems @@ -482,7 +482,6 @@ - @@ -544,7 +543,6 @@ - diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ICollectionExtensions.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ICollectionExtensions.cs deleted file mode 100644 index b0d57c834df5b..0000000000000 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ICollectionExtensions.cs +++ /dev/null @@ -1,54 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using Microsoft.CodeAnalysis.PooledObjects; - -namespace Microsoft.CodeAnalysis.Shared.Extensions; - -internal static class ICollectionExtensions -{ - public static ImmutableArray WhereAsArray(this IEnumerable values, Func predicate, TState state) - { - using var _ = ArrayBuilder.GetInstance(out var result); - - foreach (var value in values) - { - if (predicate(value, state)) - result.Add(value); - } - - return result.ToImmutableAndClear(); - } - - public static void RemoveRange(this ICollection collection, IEnumerable? items) - { - if (collection == null) - { - throw new ArgumentNullException(nameof(collection)); - } - - if (items != null) - { - foreach (var item in items) - { - collection.Remove(item); - } - } - } - - public static void AddIfNotNull(this ICollection collection, T? value) where T : struct - { - if (value != null) - collection.Add(value.Value); - } - - public static void AddIfNotNull(this ICollection collection, T? value) where T : class - { - if (value != null) - collection.Add(value); - } -}