// 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.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis.CodeRefactorings { internal static class CodeRefactoringContextExtensions { /// /// Use this helper to register multiple refactorings (). /// private static void RegisterRefactorings( CodeRefactoringContext context, ImmutableArray actions, TextSpan? applicableToSpan = null) where TCodeAction : CodeAction { if (!actions.IsDefault) { foreach (var action in actions) { if (applicableToSpan != null) { context.RegisterRefactoring(action, applicableToSpan.Value); } else { context.RegisterRefactoring(action); } } } } internal static Task TryGetRelevantNodeAsync(this CodeRefactoringContext context) where TSyntaxNode : SyntaxNode => TryGetRelevantNodeAsync(context.Document, context.Span, context.CancellationToken); internal static Task> GetRelevantNodesAsync(this CodeRefactoringContext context) where TSyntaxNode : SyntaxNode => GetRelevantNodesAsync(context.Document, context.Span, context.CancellationToken); internal static async Task TryGetRelevantNodeAsync( this Document document, TextSpan span, CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode { var potentialNodes = await GetRelevantNodesAsync(document, span, cancellationToken).ConfigureAwait(false); return potentialNodes.FirstOrDefault(); } internal static Task> GetRelevantNodesAsync( this Document document, TextSpan span, CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode { var helpers = document.GetRequiredLanguageService(); return helpers.GetRelevantNodesAsync(document, span, cancellationToken); } } }