Permalink
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (40 sloc) 1.84 KB
// Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using System.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;
using Roslynator.CSharp.Refactorings;
namespace Roslynator.CSharp.CodeFixes
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(ReturnTaskInsteadOfNullCodeFixProvider2))]
[Shared]
public class ReturnTaskInsteadOfNullCodeFixProvider2 : BaseCodeFixProvider
{
public sealed override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(DiagnosticIdentifiers.ReturnTaskInsteadOfNull); }
}
public override FixAllProvider GetFixAllProvider()
{
return null;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);
if (!TryFindNode(root, context.Span, out ExpressionSyntax expression))
return;
if (!(expression.WalkUpParentheses() is ConditionalAccessExpressionSyntax conditionalAccess))
return;
Diagnostic diagnostic = context.Diagnostics[0];
CodeAction codeAction = CodeAction.Create(
"Use Task.FromResult",
cancellationToken => ReturnTaskInsteadOfReturningNullRefactoring.RefactorAsync(context.Document, conditionalAccess, cancellationToken),
GetEquivalenceKey(diagnostic));
context.RegisterCodeFix(codeAction, diagnostic);
}
}
}