提交 5e7a2894 编写于 作者: C CyrusNajmabadi

Simplifying code now that it doesn't need to support multiple purposes.

上级 90759e0c
......@@ -38,19 +38,17 @@ internal class CSharpAddAwaitCodeFixProvider : AbstractAddAwaitCodeFixProvider
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CS0029, CS4014, CS4016);
protected override async Task<IList<DescriptionAndNode>> GetDescriptionsAndNodesAsync(
protected override async Task<DescriptionAndNode> GetDescriptionAndNodeAsync(
SyntaxNode root, SyntaxNode oldNode, SemanticModel semanticModel, Diagnostic diagnostic, Document document, CancellationToken cancellationToken)
{
var newRoot = await GetNewRootAsync(
root, oldNode, semanticModel, diagnostic, document, cancellationToken).ConfigureAwait(false);
if (newRoot == null)
{
return null;
return default(DescriptionAndNode);
}
return SpecializedCollections.SingletonList(new DescriptionAndNode(
CSharpFeaturesResources.Insert_await,
newRoot));
return new DescriptionAndNode(CSharpFeaturesResources.Insert_await, newRoot);
}
private Task<SyntaxNode> GetNewRootAsync(
......
......@@ -10,31 +10,23 @@ namespace Microsoft.CodeAnalysis.CodeFixes.Async
{
internal abstract partial class AbstractAddAwaitCodeFixProvider : AbstractAsyncCodeFix
{
protected abstract Task<IList<DescriptionAndNode>> GetDescriptionsAndNodesAsync(
protected abstract Task<DescriptionAndNode> GetDescriptionAndNodeAsync(
SyntaxNode root, SyntaxNode oldNode, SemanticModel semanticModel, Diagnostic diagnostic, Document document, CancellationToken cancellationToken);
protected override async Task<IList<CodeAction>> GetCodeActionsAsync(
protected override async Task<CodeAction> GetCodeActionAsync(
SyntaxNode root, SyntaxNode node, Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var data = await this.GetDescriptionsAndNodesAsync(root, node, semanticModel, diagnostic, document, cancellationToken).ConfigureAwait(false);
if (data == null)
var data = await this.GetDescriptionAndNodeAsync(root, node, semanticModel, diagnostic, document, cancellationToken).ConfigureAwait(false);
if (data.Node == null)
{
return null;
}
var result = new List<CodeAction>();
foreach (var item in data)
{
var action = new MyCodeAction(
item.Description,
c => Task.FromResult(document.WithSyntaxRoot(item.Node)));
result.Add(action);
}
return result;
return new MyCodeAction(
data.Description,
c => Task.FromResult(document.WithSyntaxRoot(data.Node)));
}
protected static bool TryGetExpressionType(
......
......@@ -12,7 +12,7 @@ namespace Microsoft.CodeAnalysis.CodeFixes.Async
{
internal abstract partial class AbstractAsyncCodeFix : CodeFixProvider
{
protected abstract Task<IList<CodeAction>> GetCodeActionsAsync(
protected abstract Task<CodeAction> GetCodeActionAsync(
SyntaxNode root, SyntaxNode node, Document document, Diagnostic diagnostic, CancellationToken cancellationToken);
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
......@@ -27,9 +27,9 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
var diagnostic = context.Diagnostics.FirstOrDefault();
var codeActions = await GetCodeActionsAsync(
var codeAction = await GetCodeActionAsync(
root, node, context.Document, diagnostic, context.CancellationToken).ConfigureAwait(false);
context.RegisterFixes(codeActions, context.Diagnostics);
context.RegisterCodeFix(codeAction, context.Diagnostics);
}
private bool TryGetNode(SyntaxNode root, TextSpan span, out SyntaxNode node)
......
// 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.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes.Async
{
......@@ -14,23 +12,23 @@ internal abstract partial class AbstractChangeToAsyncCodeFixProvider : AbstractA
protected abstract Task<string> GetDescription(Diagnostic diagnostic, SyntaxNode node, SemanticModel semanticModel, CancellationToken cancellationToken);
protected abstract Task<Tuple<SyntaxTree, SyntaxNode>> GetRootInOtherSyntaxTree(SyntaxNode node, SemanticModel semanticModel, Diagnostic diagnostic, CancellationToken cancellationToken);
protected override async Task<IList<CodeAction>> GetCodeActionsAsync(
protected override async Task<CodeAction> GetCodeActionAsync(
SyntaxNode root, SyntaxNode node, Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var result = await GetRootInOtherSyntaxTree(node, semanticModel, diagnostic, cancellationToken).ConfigureAwait(false);
if (result != null)
if (result == null)
{
var syntaxTree = result.Item1;
var newRoot = result.Item2;
var otherDocument = document.Project.Solution.GetDocument(syntaxTree);
return SpecializedCollections.SingletonList<CodeAction>(new MyCodeAction(
await this.GetDescription(diagnostic, node, semanticModel, cancellationToken).ConfigureAwait(false),
token => Task.FromResult(otherDocument.WithSyntaxRoot(newRoot))));
return null;
}
return null;
var syntaxTree = result.Item1;
var newRoot = result.Item2;
var otherDocument = document.Project.Solution.GetDocument(syntaxTree);
return new MyCodeAction(
await this.GetDescription(diagnostic, node, semanticModel, cancellationToken).ConfigureAwait(false),
token => Task.FromResult(otherDocument.WithSyntaxRoot(newRoot)));
}
private class MyCodeAction : CodeAction.DocumentChangeAction
......
......@@ -29,16 +29,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Async
End Get
End Property
Protected Overrides Async Function GetDescriptionsAndNodesAsync(root As SyntaxNode, oldNode As SyntaxNode, semanticModel As SemanticModel, diagnostic As Diagnostic, document As Document, cancellationToken As CancellationToken) As Task(Of IList(Of DescriptionAndNode))
Protected Overrides Async Function GetDescriptionAndNodeAsync(root As SyntaxNode, oldNode As SyntaxNode, semanticModel As SemanticModel, diagnostic As Diagnostic, document As Document, cancellationToken As CancellationToken) As Task(Of DescriptionAndNode)
Dim newRoot = Await GetNewRootAsync(
root, oldNode, semanticModel, diagnostic, document, cancellationToken).ConfigureAwait(False)
If newRoot Is Nothing Then
Return Nothing
End If
Return SpecializedCollections.SingletonList(New DescriptionAndNode(
VBFeaturesResources.Insert_Await,
newRoot))
Return New DescriptionAndNode(VBFeaturesResources.Insert_Await, newRoot)
End Function
Private Function GetNewRootAsync(root As SyntaxNode, oldNode As SyntaxNode, semanticModel As SemanticModel, diagnostic As Diagnostic, document As Document, cancellationToken As CancellationToken) As Task(Of SyntaxNode)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册