// 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.Composition; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.CodeAnalysis.Editing; using Microsoft.CodeAnalysis.Shared.Extensions; namespace Microsoft.CodeAnalysis.CSharp.UseIndexOrRangeOperator { using System.Linq; using static Helpers; [ExportCodeFixProvider(LanguageNames.CSharp), Shared] internal class CSharpUseIndexOperatorCodeFixProvider : SyntaxEditorBasedCodeFixProvider { public override ImmutableArray FixableDiagnosticIds { get; } = ImmutableArray.Create(IDEDiagnosticIds.UseIndexOperatorDiagnosticId); public override Task RegisterCodeFixesAsync(CodeFixContext context) { context.RegisterCodeFix(new MyCodeAction( c => FixAsync(context.Document, context.Diagnostics[0], c)), context.Diagnostics); return Task.CompletedTask; } protected override Task FixAllAsync( Document document, ImmutableArray diagnostics, SyntaxEditor editor, CancellationToken cancellationToken) { // Process diagnostics from innermost to outermost in case any are nested. foreach (var diagnostic in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start)) { var node = diagnostic.Location.FindNode(getInnermostNodeForTie: true, cancellationToken); editor.ReplaceNode( node, (currentNode, _) => IndexExpression(((BinaryExpressionSyntax)currentNode).Right)); } return Task.CompletedTask; } private class MyCodeAction : CodeAction.DocumentChangeAction { public MyCodeAction(Func> createChangedDocument) : base(FeaturesResources.Use_index_operator, createChangedDocument, FeaturesResources.Use_index_operator) { } } } }