// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license 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 ().
///
internal static void RegisterRefactorings(
this CodeRefactoringContext context, ImmutableArray actions)
where TCodeAction : CodeAction
{
if (!actions.IsDefault)
{
foreach (var action in actions)
{
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 async Task> GetRelevantNodesAsync(
this Document document,
TextSpan span,
CancellationToken cancellationToken) where TSyntaxNode : SyntaxNode
{
var helpers = document.GetLanguageService();
var potentialNodes = await helpers.GetRelevantNodesAsync(document, span, cancellationToken).ConfigureAwait(false);
return potentialNodes;
}
}
}