未验证 提交 71bdb27f 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #47294 from sharwell/codefixes-annotations

Update nullable annotations in CodeFixes folder
......@@ -58,7 +58,7 @@ internal partial class CodeFixService : ForegroundThreadAffinitizedObject, ICode
private readonly ImmutableDictionary<LanguageKind, Lazy<ImmutableArray<IConfigurationFixProvider>>> _configurationProvidersMap;
private readonly IEnumerable<Lazy<IErrorLoggerService>> _errorLoggers;
private ImmutableDictionary<object, FixAllProviderInfo> _fixAllProviderMap;
private ImmutableDictionary<object, FixAllProviderInfo?> _fixAllProviderMap;
[ImportingConstructor]
[SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
......@@ -85,7 +85,7 @@ internal partial class CodeFixService : ForegroundThreadAffinitizedObject, ICode
_projectFixersMap = new ConditionalWeakTable<IReadOnlyList<AnalyzerReference>, ImmutableDictionary<string, List<CodeFixProvider>>>();
_analyzerReferenceToFixersMap = new ConditionalWeakTable<AnalyzerReference, ProjectCodeFixProvider>();
_createProjectCodeFixProvider = new ConditionalWeakTable<AnalyzerReference, ProjectCodeFixProvider>.CreateValueCallback(r => new ProjectCodeFixProvider(r));
_fixAllProviderMap = ImmutableDictionary<object, FixAllProviderInfo>.Empty;
_fixAllProviderMap = ImmutableDictionary<object, FixAllProviderInfo?>.Empty;
}
public async Task<FirstDiagnosticResult> GetMostSevereFixableDiagnosticAsync(
......
......@@ -128,7 +128,7 @@ static bool IsRemoteApiUsage(SyntaxNode node, SemanticModel model)
}
}
protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, SemanticModel model, string equivalenceKey, CancellationToken cancellationToken)
protected override bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, SemanticModel model, string? equivalenceKey, CancellationToken cancellationToken)
{
var node = diagnostic.Location.FindNode(getInnermostNodeForTie: true, cancellationToken);
return equivalenceKey == GetEquivalenceKey(node, model);
......
......@@ -293,7 +293,7 @@ private static int GetEndIncludingLineBreak(SourceText text, int position)
private async Task<SyntaxNode> FixAllAsync(
Document document, ImmutableArray<Diagnostic> diagnostics,
string equivalenceKey, CancellationToken cancellationToken)
string? equivalenceKey, CancellationToken cancellationToken)
{
Debug.Assert(
equivalenceKey == TakeTopEquivalenceKey ||
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -123,15 +125,6 @@ public struct CodeFixContext : ITypeScriptCodeFixContext
{
}
internal CodeFixContext(
Project project,
ImmutableArray<Diagnostic> diagnostics,
Action<CodeAction, ImmutableArray<Diagnostic>> registerCodeFix,
CancellationToken cancellationToken)
: this(document: null, project: project, span: default, diagnostics: diagnostics, registerCodeFix: registerCodeFix, verifyArguments: false, isBlocking: false, cancellationToken: cancellationToken)
{
}
private CodeFixContext(
Document document,
Project project,
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Immutable;
using System.Threading.Tasks;
......@@ -32,7 +34,7 @@ public abstract class CodeFixProvider
/// Return null if the provider doesn't support fix all/multiple occurrences.
/// Otherwise, you can return any of the well known fix all providers from <see cref="WellKnownFixAllProviders"/> or implement your own fix all provider.
/// </summary>
public virtual FixAllProvider GetFixAllProvider()
public virtual FixAllProvider? GetFixAllProvider()
=> null;
}
}
......@@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.CodeAnalysis.CodeFixes
{
......@@ -17,7 +20,8 @@ public sealed class ExportCodeFixProviderAttribute : ExportAttribute
/// <summary>
/// Optional name of the <see cref="CodeFixProvider"/>.
/// </summary>
public string Name { get; set; }
[DisallowNull]
public string? Name { get; set; }
/// <summary>
/// The source languages this provider can provide fixes for. See <see cref="LanguageNames"/>.
......@@ -39,8 +43,6 @@ public sealed class ExportCodeFixProviderAttribute : ExportAttribute
throw new ArgumentNullException(nameof(additionalLanguages));
}
this.Name = null;
var languages = new string[additionalLanguages.Length + 1];
languages[0] = firstLanguage ?? throw new ArgumentNullException(nameof(firstLanguage));
for (var index = 0; index < additionalLanguages.Length; index++)
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
......@@ -14,6 +16,7 @@
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
......@@ -30,7 +33,7 @@ internal partial class BatchFixAllProvider : FixAllProvider
#region "AbstractFixAllProvider methods"
public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
public override async Task<CodeAction?> GetFixAsync(FixAllContext fixAllContext)
{
if (fixAllContext.Document != null)
{
......@@ -46,7 +49,7 @@ public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
#endregion
private async Task<CodeAction> GetFixAsync(
private async Task<CodeAction?> GetFixAsync(
ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
FixAllState fixAllState, CancellationToken cancellationToken)
{
......@@ -131,7 +134,7 @@ public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
await Task.WhenAll(fixerTasks).ConfigureAwait(false);
}
private async Task<CodeAction> GetFixAsync(
private async Task<CodeAction?> GetFixAsync(
ImmutableDictionary<Project, ImmutableArray<Diagnostic>> projectsAndDiagnosticsToFixMap,
FixAllState fixAllState, CancellationToken cancellationToken)
{
......@@ -198,19 +201,10 @@ public override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
ConcurrentBag<(Diagnostic diagnostic, CodeAction action)> fixes,
FixAllState fixAllState, CancellationToken cancellationToken)
{
Debug.Assert(!diagnostics.IsDefault);
cancellationToken.ThrowIfCancellationRequested();
var registerCodeFix = GetRegisterCodeFixAction(fixAllState, fixes);
var context = new CodeFixContext(
project, diagnostics, registerCodeFix, cancellationToken);
// TODO: Wrap call to ComputeFixesAsync() below in IExtensionManager.PerformFunctionAsync() so that
// a buggy extension that throws can't bring down the host?
return fixAllState.CodeFixProvider.RegisterCodeFixesAsync(context) ?? Task.CompletedTask;
return Task.CompletedTask;
}
public virtual async Task<CodeAction> TryGetMergedFixAsync(
public virtual async Task<CodeAction?> TryGetMergedFixAsync(
ImmutableArray<(Diagnostic diagnostic, CodeAction action)> batchOfFixes,
FixAllState fixAllState, CancellationToken cancellationToken)
{
......@@ -337,8 +331,8 @@ public virtual string GetFixAllTitle(FixAllState fixAllState)
var totalChangesIntervalTree = SimpleIntervalTree.Create(new TextChangeIntervalIntrospector(), Array.Empty<TextChange>());
var oldDocument = oldSolution.GetDocument(orderedDocuments[0].document.Id);
var differenceService = oldSolution.Workspace.Services.GetService<IDocumentTextDifferencingService>();
var oldDocument = oldSolution.GetRequiredDocument(orderedDocuments[0].document.Id);
var differenceService = oldSolution.Workspace.Services.GetRequiredService<IDocumentTextDifferencingService>();
foreach (var (_, currentDocument) in orderedDocuments)
{
......@@ -381,6 +375,11 @@ public virtual string GetFixAllTitle(FixAllState fixAllState)
var changedSolution = await codeAction.GetChangedSolutionInternalAsync(
cancellationToken: cancellationToken).ConfigureAwait(false);
if (changedSolution is null)
{
// No changed documents
return;
}
var solutionChanges = new SolutionChanges(changedSolution, oldSolution);
......@@ -393,7 +392,7 @@ public virtual string GetFixAllTitle(FixAllState fixAllState)
foreach (var documentId in documentIdsWithChanges)
{
var changedDocument = changedSolution.GetDocument(documentId);
var changedDocument = changedSolution.GetRequiredDocument(documentId);
documentIdToChangedDocuments.GetOrAdd(documentId, s_getValue).Add(
(codeAction, changedDocument));
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -21,7 +23,7 @@ public partial class FixAllContext
{
internal FixAllState State { get; }
internal FixAllProvider FixAllProvider => State.FixAllProvider;
internal FixAllProvider? FixAllProvider => State.FixAllProvider;
/// <summary>
/// Solution to fix all occurrences.
......@@ -37,7 +39,7 @@ public partial class FixAllContext
/// Document within which fix all occurrences was triggered.
/// Can be null if the context was created using <see cref="FixAllContext.FixAllContext(Project, CodeFixProvider, FixAllScope, string, IEnumerable{string}, DiagnosticProvider, CancellationToken)"/>.
/// </summary>
public Document Document => State.Document;
public Document? Document => State.Document;
/// <summary>
/// Underlying <see cref="CodeFixes.CodeFixProvider"/> which triggered this fix all.
......@@ -59,7 +61,7 @@ public partial class FixAllContext
/// <summary>
/// The <see cref="CodeAction.EquivalenceKey"/> value expected of a <see cref="CodeAction"/> participating in this fix all.
/// </summary>
public string CodeActionEquivalenceKey => State.CodeActionEquivalenceKey;
public string? CodeActionEquivalenceKey => State.CodeActionEquivalenceKey;
/// <summary>
/// CancellationToken for fix all session.
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Internal.Log;
......@@ -52,7 +54,7 @@ public static void LogState(FixAllState fixAllState, bool isInternalCodeFixProvi
}
else
{
m[CodeFixProvider] = fixAllState.CodeFixProvider.GetType().FullName.GetHashCode().ToString();
m[CodeFixProvider] = fixAllState.CodeFixProvider.GetType().FullName!.GetHashCode().ToString();
m[CodeActionEquivalenceKey] = fixAllState.CodeActionEquivalenceKey?.GetHashCode().ToString();
m[LanguageName] = fixAllState.Project.Language.GetHashCode().ToString();
}
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;
......@@ -36,6 +38,6 @@ public virtual IEnumerable<string> GetSupportedFixAllDiagnosticIds(CodeFixProvid
/// <summary>
/// Gets fix all occurrences fix for the given fixAllContext.
/// </summary>
public abstract Task<CodeAction> GetFixAsync(FixAllContext fixAllContext);
public abstract Task<CodeAction?> GetFixAsync(FixAllContext fixAllContext);
}
}
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -28,7 +30,7 @@ internal abstract class FixAllProviderInfo
/// <summary>
/// Gets an optional <see cref="FixAllProviderInfo"/> for the given code fix provider or suppression fix provider.
/// </summary>
public static FixAllProviderInfo Create(object provider)
public static FixAllProviderInfo? Create(object provider)
{
if (provider is CodeFixProvider codeFixProvider)
{
......@@ -41,7 +43,7 @@ public static FixAllProviderInfo Create(object provider)
/// <summary>
/// Gets an optional <see cref="FixAllProviderInfo"/> for the given code fix provider.
/// </summary>
private static FixAllProviderInfo CreateWithCodeFixer(CodeFixProvider provider)
private static FixAllProviderInfo? CreateWithCodeFixer(CodeFixProvider provider)
{
var fixAllProvider = provider.GetFixAllProvider();
if (fixAllProvider == null)
......@@ -67,7 +69,7 @@ private static FixAllProviderInfo CreateWithCodeFixer(CodeFixProvider provider)
/// <summary>
/// Gets an optional <see cref="FixAllProviderInfo"/> for the given suppression fix provider.
/// </summary>
private static FixAllProviderInfo CreateWithSuppressionFixer(IConfigurationFixProvider provider)
private static FixAllProviderInfo? CreateWithSuppressionFixer(IConfigurationFixProvider provider)
{
var fixAllProvider = provider.GetFixAllProvider();
if (fixAllProvider == null)
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
namespace Microsoft.CodeAnalysis.CodeFixes
{
/// <summary>
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using Microsoft.CodeAnalysis.CodeActions;
namespace Microsoft.CodeAnalysis.CodeFixes
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
......@@ -38,6 +40,6 @@ internal interface IConfigurationFixProvider
/// Return null if the provider doesn't support fix all/multiple occurrences.
/// Otherwise, you can return any of the well known fix all providers from <see cref="WellKnownFixAllProviders"/> or implement your own fix all provider.
/// </summary>
FixAllProvider GetFixAllProvider();
FixAllProvider? GetFixAllProvider();
}
}
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
namespace Microsoft.CodeAnalysis.CodeFixes
{
/// <summary>
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;
......@@ -13,24 +15,21 @@ internal static class CustomCodeActions
{
internal abstract class SimpleCodeAction : CodeAction
{
public SimpleCodeAction(string title, string equivalenceKey)
public SimpleCodeAction(string title, string? equivalenceKey)
{
Title = title;
EquivalenceKey = equivalenceKey;
}
public sealed override string Title { get; }
public sealed override string EquivalenceKey { get; }
protected override Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
=> SpecializedTasks.Null<Document>();
public sealed override string? EquivalenceKey { get; }
}
internal class DocumentChangeAction : SimpleCodeAction
{
private readonly Func<CancellationToken, Task<Document>> _createChangedDocument;
public DocumentChangeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument, string equivalenceKey = null)
public DocumentChangeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument, string? equivalenceKey = null)
: base(title, equivalenceKey)
{
_createChangedDocument = createChangedDocument;
......@@ -44,14 +43,14 @@ internal class SolutionChangeAction : SimpleCodeAction
{
private readonly Func<CancellationToken, Task<Solution>> _createChangedSolution;
public SolutionChangeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution, string equivalenceKey = null)
public SolutionChangeAction(string title, Func<CancellationToken, Task<Solution>> createChangedSolution, string? equivalenceKey = null)
: base(title, equivalenceKey)
{
_createChangedSolution = createChangedSolution;
}
protected override Task<Solution> GetChangedSolutionAsync(CancellationToken cancellationToken)
=> _createChangedSolution(cancellationToken);
protected override Task<Solution?> GetChangedSolutionAsync(CancellationToken cancellationToken)
=> _createChangedSolution(cancellationToken).AsNullable();
}
}
}
......@@ -9,10 +9,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.PooledObjects;
#if NETSTANDARD2_0
using Roslyn.Utilities;
#endif
#pragma warning disable RS0005 // Do not use generic CodeAction.Create to create CodeAction
......@@ -78,6 +75,8 @@ internal abstract class DocumentBasedFixAllProvider : FixAllProvider
private async Task<Document> GetDocumentFixesAsync(FixAllContext fixAllContext)
{
RoslynDebug.AssertNotNull(fixAllContext.Document);
var documentDiagnosticsToFix = await FixAllContextHelper.GetDocumentDiagnosticsToFixAsync(fixAllContext, progressTrackerOpt: null).ConfigureAwait(false);
if (!documentDiagnosticsToFix.TryGetValue(fixAllContext.Document, out var diagnostics))
{
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
......@@ -17,21 +19,21 @@ internal partial class FixAllState
internal FixAllContext.DiagnosticProvider DiagnosticProvider { get; }
public FixAllProvider FixAllProvider { get; }
public string CodeActionEquivalenceKey { get; }
public FixAllProvider? FixAllProvider { get; }
public string? CodeActionEquivalenceKey { get; }
public CodeFixProvider CodeFixProvider { get; }
public ImmutableHashSet<string> DiagnosticIds { get; }
public Document Document { get; }
public Document? Document { get; }
public Project Project { get; }
public FixAllScope Scope { get; }
public Solution Solution => this.Project.Solution;
internal FixAllState(
FixAllProvider fixAllProvider,
FixAllProvider? fixAllProvider,
Document document,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionEquivalenceKey,
string? codeActionEquivalenceKey,
IEnumerable<string> diagnosticIds,
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider)
: this(fixAllProvider, document, document.Project, codeFixProvider, scope, codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider)
......@@ -43,11 +45,11 @@ internal partial class FixAllState
}
internal FixAllState(
FixAllProvider fixAllProvider,
FixAllProvider? fixAllProvider,
Project project,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionEquivalenceKey,
string? codeActionEquivalenceKey,
IEnumerable<string> diagnosticIds,
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider)
: this(fixAllProvider, null, project, codeFixProvider, scope, codeActionEquivalenceKey, diagnosticIds, fixAllDiagnosticProvider)
......@@ -59,12 +61,12 @@ internal partial class FixAllState
}
private FixAllState(
FixAllProvider fixAllProvider,
Document document,
FixAllProvider? fixAllProvider,
Document? document,
Project project,
CodeFixProvider codeFixProvider,
FixAllScope scope,
string codeActionEquivalenceKey,
string? codeActionEquivalenceKey,
IEnumerable<string> diagnosticIds,
FixAllContext.DiagnosticProvider fixAllDiagnosticProvider)
{
......@@ -91,7 +93,7 @@ internal partial class FixAllState
internal bool IsFixMultiple => this.DiagnosticProvider is FixMultipleDiagnosticProvider;
public FixAllState WithScopeAndEquivalenceKey(FixAllScope scope, string codeActionEquivalenceKey)
public FixAllState WithScopeAndEquivalenceKey(FixAllScope scope, string? codeActionEquivalenceKey)
{
if (this.Scope == scope && this.CodeActionEquivalenceKey == codeActionEquivalenceKey)
{
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Linq;
......@@ -17,7 +19,7 @@ internal static class FixAllContextHelper
{
public static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(
FixAllContext fixAllContext,
IProgressTracker progressTrackerOpt)
IProgressTracker? progressTrackerOpt)
{
var cancellationToken = fixAllContext.CancellationToken;
......@@ -86,14 +88,15 @@ internal static class FixAllContextHelper
var treeToDocumentMap = await GetTreeToDocumentMapAsync(projects, cancellationToken).ConfigureAwait(false);
var builder = ImmutableDictionary.CreateBuilder<Document, ImmutableArray<Diagnostic>>();
foreach (var documentAndDiagnostics in diagnostics.GroupBy(d => GetReportedDocument(d, treeToDocumentMap)))
foreach (var (document, diagnosticsForDocument) in diagnostics.GroupBy(d => GetReportedDocument(d, treeToDocumentMap)))
{
if (document is null)
continue;
cancellationToken.ThrowIfCancellationRequested();
var document = documentAndDiagnostics.Key;
if (!await document.IsGeneratedCodeAsync(cancellationToken).ConfigureAwait(false))
{
var diagnosticsForDocument = documentAndDiagnostics.ToImmutableArray();
builder.Add(document, diagnosticsForDocument);
builder.Add(document, diagnosticsForDocument.ToImmutableArray());
}
}
......@@ -109,7 +112,7 @@ internal static class FixAllContextHelper
foreach (var document in project.Documents)
{
cancellationToken.ThrowIfCancellationRequested();
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var tree = await document.GetRequiredSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
builder.Add(tree, document);
}
}
......@@ -117,7 +120,7 @@ internal static class FixAllContextHelper
return builder.ToImmutable();
}
private static Document GetReportedDocument(Diagnostic diagnostic, ImmutableDictionary<SyntaxTree, Document> treeToDocumentsMap)
private static Document? GetReportedDocument(Diagnostic diagnostic, ImmutableDictionary<SyntaxTree, Document> treeToDocumentsMap)
{
var tree = diagnostic.Location.SourceTree;
if (tree != null)
......@@ -137,7 +140,7 @@ public static string GetDefaultFixAllTitle(FixAllContext fixAllContext)
public static string GetDefaultFixAllTitle(
FixAllScope fixAllScope,
ImmutableHashSet<string> diagnosticIds,
Document triggerDocument,
Document? triggerDocument,
Project triggerProject)
{
string diagnosticId;
......@@ -156,7 +159,7 @@ public static string GetDefaultFixAllTitle(FixAllContext fixAllContext)
return string.Format(WorkspaceExtensionsResources.Fix_all_0, diagnosticId);
case FixAllScope.Document:
return string.Format(WorkspaceExtensionsResources.Fix_all_0_in_1, diagnosticId, triggerDocument.Name);
return string.Format(WorkspaceExtensionsResources.Fix_all_0_in_1, diagnosticId, triggerDocument!.Name);
case FixAllScope.Project:
return string.Format(WorkspaceExtensionsResources.Fix_all_0_in_1, diagnosticId, triggerProject.Name);
......
......@@ -2,10 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CodeFixes
{
......@@ -24,7 +27,7 @@ internal sealed class SyntaxEditorBasedFixAllProvider : FixAllProvider
public SyntaxEditorBasedFixAllProvider(SyntaxEditorBasedCodeFixProvider codeFixProvider)
=> _codeFixProvider = codeFixProvider;
public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllContext)
public sealed override async Task<CodeAction?> GetFixAsync(FixAllContext fixAllContext)
{
var documentsAndDiagnosticsToFixMap = await GetDocumentDiagnosticsToFixAsync(fixAllContext).ConfigureAwait(false);
return await GetFixAsync(documentsAndDiagnosticsToFixMap, fixAllContext).ConfigureAwait(false);
......@@ -64,7 +67,7 @@ public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllCo
var updatedDocument = await task.ConfigureAwait(false);
currentSolution = currentSolution.WithDocumentSyntaxRoot(
updatedDocument.Id,
await updatedDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false));
await updatedDocument.GetRequiredSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false));
}
var title = FixAllContextHelper.GetDefaultFixAllTitle(fixAllContext);
......@@ -74,7 +77,7 @@ public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllCo
private async Task<Document> FixDocumentAsync(
Document document, ImmutableArray<Diagnostic> diagnostics, FixAllContext fixAllContext)
{
var model = await document.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
var model = await document.GetRequiredSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
// Ensure that diagnostics for this document are always in document location
// order. This provides a consistent and deterministic order for fixers
......@@ -82,7 +85,7 @@ public sealed override async Task<CodeAction> GetFixAsync(FixAllContext fixAllCo
// Also ensure that we do not pass in duplicates by invoking Distinct.
// See https://github.com/dotnet/roslyn/issues/31381, that seems to be causing duplicate diagnostics.
var filteredDiagnostics = diagnostics.Distinct()
.WhereAsArray(d => _codeFixProvider.IncludeDiagnosticDuringFixAll(d, fixAllContext.Document, model, fixAllContext.CodeActionEquivalenceKey, fixAllContext.CancellationToken))
.WhereAsArray(d => _codeFixProvider.IncludeDiagnosticDuringFixAll(d, document, model, fixAllContext.CodeActionEquivalenceKey, fixAllContext.CancellationToken))
.Sort((d1, d2) => d1.Location.SourceSpan.Start - d2.Location.SourceSpan.Start);
// PERF: Do not invoke FixAllAsync on the code fix provider if there are no diagnostics to be fixed.
......
......@@ -2,11 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CodeFixes
{
......@@ -17,7 +20,7 @@ internal abstract partial class SyntaxEditorBasedCodeFixProvider : CodeFixProvid
protected SyntaxEditorBasedCodeFixProvider(bool supportsFixAll = true)
=> _supportsFixAll = supportsFixAll;
public sealed override FixAllProvider GetFixAllProvider()
public sealed override FixAllProvider? GetFixAllProvider()
=> _supportsFixAll ? new SyntaxEditorBasedFixAllProvider(this) : null;
protected Task<Document> FixAsync(
......@@ -39,7 +42,7 @@ public sealed override FixAllProvider GetFixAllProvider()
Func<SyntaxEditor, Task> editAsync,
CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var editor = new SyntaxEditor(root, document.Project.Solution.Workspace);
await editAsync(editor).ConfigureAwait(false);
......@@ -70,10 +73,10 @@ public sealed override FixAllProvider GetFixAllProvider()
/// Only one of these three overloads needs to be overridden if you want to customize
/// behavior.
/// </summary>
protected virtual bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, SemanticModel model, string equivalenceKey, CancellationToken cancellationToken)
protected virtual bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, SemanticModel model, string? equivalenceKey, CancellationToken cancellationToken)
=> IncludeDiagnosticDuringFixAll(diagnostic, document, equivalenceKey, cancellationToken);
protected virtual bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, string equivalenceKey, CancellationToken cancellationToken)
protected virtual bool IncludeDiagnosticDuringFixAll(Diagnostic diagnostic, Document document, string? equivalenceKey, CancellationToken cancellationToken)
=> IncludeDiagnosticDuringFixAll(diagnostic);
/// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册