提交 e47fd1fe 编写于 作者: S shyamn

Rollback code fix public API changes checked in in C1353855. This takes us...

Rollback code fix public API changes checked in in C1353855. This takes us closer to proposed new API that I am currently working on. (changeset 1360209)
上级 9cabf877
......@@ -26,18 +26,20 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var actions = SpecializedCollections.EmptyEnumerable<CodeAction>();
var diagnostic = context.Diagnostic;
cancellationToken.ThrowIfCancellationRequested();
foreach (var diagnostic in context.Diagnostics)
{
cancellationToken.ThrowIfCancellationRequested();
var nodeToFix = root.FindNode(diagnostic.Location.SourceSpan);
var nodeToFix = root.FindNode(diagnostic.Location.SourceSpan);
var newDocument = await GetUpdatedDocumentAsync(document, model, root, nodeToFix, diagnostic.Id, cancellationToken).ConfigureAwait(false);
var newDocument = await GetUpdatedDocumentAsync(document, model, root, nodeToFix, diagnostic.Id, cancellationToken).ConfigureAwait(false);
Debug.Assert(newDocument != null);
if (newDocument != document)
{
var codeFixDescription = GetCodeFixDescription(diagnostic.Id);
actions = actions.Concat(new MyCodeAction(codeFixDescription, newDocument));
Debug.Assert(newDocument != null);
if (newDocument != document)
{
var codeFixDescription = GetCodeFixDescription(diagnostic.Id);
actions = actions.Concat(new MyCodeAction(codeFixDescription, newDocument));
}
}
return actions;
......
......@@ -28,15 +28,18 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var actions = SpecializedCollections.EmptyEnumerable<CodeAction>();
cancellationToken.ThrowIfCancellationRequested();
foreach (var diagnostic in context.Diagnostics)
{
cancellationToken.ThrowIfCancellationRequested();
var nodeToFix = root.FindNode(context.Diagnostic.Location.SourceSpan);
var nodeToFix = root.FindNode(diagnostic.Location.SourceSpan);
var newActions = await GetFixesAsync(document, model, root, nodeToFix, cancellationToken).ConfigureAwait(false);
var newActions = await GetFixesAsync(document, model, root, nodeToFix, cancellationToken).ConfigureAwait(false);
if (newActions != null)
{
actions = actions.Concat(newActions);
if (newActions != null)
{
actions = actions.Concat(newActions);
}
}
return actions;
......
......@@ -26,18 +26,20 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var actions = SpecializedCollections.EmptyEnumerable<CodeAction>();
var diagnostic = context.Diagnostic;
cancellationToken.ThrowIfCancellationRequested();
foreach (var diagnostic in context.Diagnostics)
{
cancellationToken.ThrowIfCancellationRequested();
var nodeToFix = root.FindNode(diagnostic.Location.SourceSpan);
var nodeToFix = root.FindNode(diagnostic.Location.SourceSpan);
var newDocument = await GetUpdatedDocumentAsync(document, model, root, nodeToFix, diagnostic.Id, cancellationToken).ConfigureAwait(false);
var newDocument = await GetUpdatedDocumentAsync(document, model, root, nodeToFix, diagnostic.Id, cancellationToken).ConfigureAwait(false);
Debug.Assert(newDocument != null);
if (newDocument != document)
{
var codeFixDescription = GetCodeFixDescription(diagnostic.Id);
actions = actions.Concat(new MyCodeAction(codeFixDescription, newDocument));
Debug.Assert(newDocument != null);
if (newDocument != document)
{
var codeFixDescription = GetCodeFixDescription(diagnostic.Id);
actions = actions.Concat(new MyCodeAction(codeFixDescription, newDocument));
}
}
return actions;
......
......@@ -27,21 +27,22 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
return GetFixesCore(context.Document, root, context.Diagnostic, context.CancellationToken);
return GetFixesCore(context.Document, root, context.Diagnostics, context.CancellationToken);
}
private IEnumerable<CodeAction> GetFixesCore(Document document, SyntaxNode root, Diagnostic diagnostic, CancellationToken cancellationToken)
private IEnumerable<CodeAction> GetFixesCore(Document document, SyntaxNode root, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
var expression = root.FindNode(diagnostic.Location.SourceSpan) as TExpressionSyntax;
if (expression != null)
foreach (var diagnostic in diagnostics)
{
return ImmutableArray.Create(new MyCodeAction(
"Append .ConfigureAwait(" + FalseLiteralString + ")",
c => GetFix(document, root, expression, c)));
}
var expression = root.FindNode(diagnostic.Location.SourceSpan) as TExpressionSyntax;
return null;
if (expression != null)
{
yield return new MyCodeAction(
"Append .ConfigureAwait(" + FalseLiteralString + ")",
c => GetFix(document, root, expression, c));
}
}
}
private Task<Document> GetFix(Document document, SyntaxNode root, TExpressionSyntax expression, CancellationToken cancellationToken)
......
......@@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
......@@ -35,7 +36,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
Debug.Assert(root != null);
var parent = root.FindToken(diagnosticSpan.Start).Parent;
......
......@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
......@@ -33,7 +34,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<MethodDeclarationSyntax>();
......
......@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
......@@ -29,7 +30,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var invocation = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<InvocationExpressionSyntax>();
......@@ -138,7 +139,7 @@ private async Task<Document> ChangetoAwaitAsync(Document document, InvocationExp
{
oldExpression = invocation.Parent.FirstAncestorOrSelf<MemberAccessExpressionSyntax>();
newExpression = SyntaxFactory.PrefixUnaryExpression(
SyntaxKind.AwaitExpression,
SyntaxKind.AwaitExpression,
invocation).WithAdditionalAnnotations(Formatter.Annotation);
}
......
......@@ -33,7 +33,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var invocation = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<InvocationExpressionSyntax>();
......
......@@ -29,7 +29,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
// Find the type declaration identified by the diagnostic.
var methodDeclaration = root.FindToken(diagnosticSpan.Start).Parent.FirstAncestorOrSelf<MethodDeclarationSyntax>();
......
......@@ -53,7 +53,7 @@ public sealed override async Task<IEnumerable<CodeAction>> GetFixesAsync(CodeFix
{
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
var diagnosticSpan = context.Diagnostic.Location.SourceSpan;
var diagnosticSpan = context.Diagnostics.First().Location.SourceSpan;
// Find the local declaration identified by the diagnostic.
var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LocalDeclarationStatementSyntax>().First();
......
......@@ -44,7 +44,7 @@ Class MakeConstCodeFixProvider
End Function
Public NotOverridable Overrides Async Function GetFixesAsync(context As CodeFixContext) As Task(Of IEnumerable(Of CodeAction))
Dim diagnosticSpan = context.Diagnostic.Location.SourceSpan
Dim diagnosticSpan = context.Diagnostics.First().Location.SourceSpan
Dim root = Await context.Document.GetSyntaxRootAsync(context.CancellationToken)
' Find the local declaration identified by the diagnostic.
......
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Threading;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CodeFixes
{
......@@ -11,7 +13,7 @@ public struct CodeFixContext
{
private readonly Document document;
private readonly TextSpan span;
private readonly Diagnostic diagnostic;
private readonly IEnumerable<Diagnostic> diagnostics;
private readonly CancellationToken cancellationToken;
/// <summary>
......@@ -25,9 +27,9 @@ public struct CodeFixContext
public TextSpan Span { get { return this.span; } }
/// <summary>
/// Diagnostic to fix.
/// Diagnostics to fix.
/// </summary>
public Diagnostic Diagnostic { get { return this.diagnostic; } }
public IEnumerable<Diagnostic> Diagnostics { get { return this.diagnostics; } }
/// <summary>
/// CancellationToken.
......@@ -40,12 +42,12 @@ public struct CodeFixContext
public CodeFixContext(
Document document,
TextSpan span,
Diagnostic diagnostic,
IEnumerable<Diagnostic> diagnostics,
CancellationToken cancellationToken)
{
this.document = document;
this.span = span;
this.diagnostic = diagnostic;
this.diagnostics = diagnostics;
this.cancellationToken = cancellationToken;
}
......@@ -56,7 +58,7 @@ public struct CodeFixContext
Document document,
Diagnostic diagnostic,
CancellationToken cancellationToken)
: this(document, diagnostic.Location.SourceSpan, diagnostic, cancellationToken)
: this(document, diagnostic.Location.SourceSpan, SpecializedCollections.SingletonEnumerable(diagnostic), cancellationToken)
{
}
}
......
......@@ -170,7 +170,16 @@ public virtual async Task<CodeAction> TryGetMergedFixAsync(IEnumerable<CodeActio
public virtual string GetFixAllTitle(FixAllContext fixAllContext)
{
var diagnosticId = fixAllContext.DiagnosticId;
var diagnosticIds = fixAllContext.DiagnosticIds;
string diagnosticId;
if (diagnosticIds.Count() == 1)
{
diagnosticId = diagnosticIds.Single();
}
else
{
diagnosticId = string.Join(",", diagnosticIds.ToArray());
}
switch (fixAllContext.Scope)
{
......
......@@ -14,8 +14,8 @@ namespace Microsoft.CodeAnalysis.CodeFixes
/// </summary>
public class FixAllContext
{
private readonly Func<Project, Document, string, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync;
private readonly Func<Project, Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync;
/// <summary>
/// Solution to fix all occurences.
/// </summary>
......@@ -42,11 +42,11 @@ public class FixAllContext
public FixAllScope Scope { get; private set; }
/// <summary>
/// Diagnostic Id to fix.
/// Diagnostic Ids to fix.
/// Note that <see cref="GetDiagnosticsAsync(Document)"/> and <see cref="GetDiagnosticsAsync(Project)"/> methods
/// return only diagnostics whose Ids are contained in this set of Ids.
/// return only diagnostics whose IDs are contained in this set of Ids.
/// </summary>
public string DiagnosticId { get; private set; }
public ImmutableHashSet<string> DiagnosticIds { get; private set; }
/// <summary>
/// CodeAction Id to generate a fix all occurrences code fix.
......@@ -60,13 +60,13 @@ public class FixAllContext
internal FixAllContext(
Document document,
CodeFixProvider codeFixProvider,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionId,
string diagnosticId,
Func<Project, Document, string, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
IEnumerable<string> diagnosticIds,
Func<Project, Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
CancellationToken cancellationToken)
: this(document, document.Project, codeFixProvider, scope, codeActionId, diagnosticId, getDiagnosticsAsync, cancellationToken)
: this(document, document.Project, codeFixProvider, scope, codeActionId, diagnosticIds, getDiagnosticsAsync, cancellationToken)
{
}
......@@ -75,10 +75,10 @@ public class FixAllContext
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionId,
string diagnosticId,
Func<Project, Document, string, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
IEnumerable<string> diagnosticIds,
Func<Project, Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
CancellationToken cancellationToken)
: this(null, project, codeFixProvider, scope, codeActionId, diagnosticId, getDiagnosticsAsync, cancellationToken)
: this(null, project, codeFixProvider, scope, codeActionId, diagnosticIds, getDiagnosticsAsync, cancellationToken)
{
}
......@@ -88,8 +88,8 @@ public class FixAllContext
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionId,
string diagnosticId,
Func<Project, Document, string, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
IEnumerable<string> diagnosticIds,
Func<Project, Document, ImmutableHashSet<string>, CancellationToken, Task<IEnumerable<Diagnostic>>> getDiagnosticsAsync,
CancellationToken cancellationToken)
{
this.Document = document;
......@@ -97,13 +97,13 @@ public class FixAllContext
this.CodeFixProvider = codeFixProvider;
this.Scope = scope;
this.CodeActionId = codeActionId;
this.DiagnosticId = diagnosticId;
this.DiagnosticIds = ImmutableHashSet.CreateRange(diagnosticIds);
this.getDiagnosticsAsync = getDiagnosticsAsync;
this.CancellationToken = cancellationToken;
}
/// <summary>
/// Gets all the diagnostics in the given document filtered by <see cref="DiagnosticId"/>.
/// Gets all the diagnostics in the given document filtered by <see cref="DiagnosticIds"/>.
/// </summary>
public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Document document)
{
......@@ -114,13 +114,13 @@ public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Document docum
return ImmutableArray<Diagnostic>.Empty;
}
var diagnostics = await this.getDiagnosticsAsync(document.Project, document, this.DiagnosticId, this.CancellationToken).ConfigureAwait(false);
Contract.ThrowIfFalse(diagnostics.All(d => d.Id == DiagnosticId));
var diagnostics = await this.getDiagnosticsAsync(document.Project, document, this.DiagnosticIds, this.CancellationToken).ConfigureAwait(false);
Contract.ThrowIfFalse(diagnostics.All(d => this.DiagnosticIds.Contains(d.Id)));
return diagnostics.ToImmutableArray();
}
/// <summary>
/// Gets all the diagnostics in the given project filtered by <see cref="DiagnosticId"/>.
/// Gets all the diagnostics in the given project filtered by <see cref="DiagnosticIds"/>.
/// </summary>
public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Project project)
{
......@@ -131,8 +131,8 @@ public async Task<ImmutableArray<Diagnostic>> GetDiagnosticsAsync(Project projec
return ImmutableArray<Diagnostic>.Empty;
}
var diagnostics = await this.getDiagnosticsAsync(project, null, this.DiagnosticId, this.CancellationToken).ConfigureAwait(false);
Contract.ThrowIfFalse(diagnostics.All(d => d.Id == DiagnosticId));
var diagnostics = await this.getDiagnosticsAsync(project, null, this.DiagnosticIds, this.CancellationToken).ConfigureAwait(false);
Contract.ThrowIfFalse(diagnostics.All(d => this.DiagnosticIds.Contains(d.Id)));
return diagnostics.ToImmutableArray();
}
......@@ -147,13 +147,13 @@ public FixAllContext WithCancellationToken(CancellationToken cancellationToken)
}
return new FixAllContext(
this.Document,
this.Document,
this.Project,
this.CodeFixProvider,
this.Scope,
this.CodeActionId,
this.DiagnosticId,
this.getDiagnosticsAsync,
this.CodeActionId,
this.DiagnosticIds,
this.getDiagnosticsAsync,
cancellationToken);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册