Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FixAllProvider for RCS1014 #1070

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e4c6b22
with fix all provider
jamesHargreaves12 Apr 13, 2023
1227a1e
ChangeLog entry
jamesHargreaves12 Apr 13, 2023
040e18d
Merge branch 'main' into add-fix-all-provider-for-RCS1014
josefpihrt Jul 16, 2023
5e678f3
formatting
jamesHargreaves12 Jul 20, 2023
7de5056
Merge branch 'add-fix-all-provider-for-RCS1014' of https://github.com…
jamesHargreaves12 Jul 20, 2023
0aba05f
Update src/Analyzers.CodeFixes/CSharp/CodeFixes/UseExplicitlyOrImplic…
jamesHargreaves12 Jul 31, 2023
94b911e
Update src/Analyzers.CodeFixes/CSharp/CodeFixes/UseExplicitlyOrImplic…
jamesHargreaves12 Jul 31, 2023
86e9f3a
Update src/Analyzers.CodeFixes/CSharp/CodeFixes/UseExplicitlyOrImplic…
jamesHargreaves12 Jul 31, 2023
33e2005
CR comments
jamesHargreaves12 Jul 31, 2023
1ab7a88
Merge branch 'main' into add-fix-all-provider-for-RCS1014
jamesHargreaves12 Jul 31, 2023
8ae3d5f
remove unused class
jamesHargreaves12 Jul 31, 2023
0ab7ada
format
jamesHargreaves12 Jul 31, 2023
7d80534
more formatting
jamesHargreaves12 Jul 31, 2023
d6c0daf
Merge branch 'main' into add-fix-all-provider-for-RCS1014
jamesHargreaves12 Jul 31, 2023
e603828
Merge branch 'main' into add-fix-all-provider-for-RCS1014
jamesHargreaves12 Aug 9, 2023
7c6a069
Update ChangeLog.md
jamesHargreaves12 Aug 9, 2023
0e0c7a2
Update UseExplicitlyOrImplicitlyTypedArrayCodeFixProvider.cs
jamesHargreaves12 Aug 9, 2023
bdf672b
Update UseExplicitlyOrImplicitlyTypedArrayCodeFixProvider.cs
jamesHargreaves12 Aug 9, 2023
94f2456
Merge branch 'main' into add-fix-all-provider-for-RCS1014
josefpihrt Aug 14, 2023
35d986d
CR comments
jamesHargreaves12 Aug 16, 2023
faf044c
Update ChangeLog.md
josefpihrt Aug 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add SECURITY.md ([#1147](https://github.com/josefpihrt/roslynator/pull/1147))
- Add custom FixAllProvider for [RCS1014](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1014.md) ([#1070](https://github.com/JosefPihrt/Roslynator/pull/1070)).

### Fixed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Josef Pihrt and Contributors. 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -23,6 +25,21 @@ public override ImmutableArray<string> FixableDiagnosticIds
get { return ImmutableArray.Create(DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray); }
}

public override FixAllProvider GetFixAllProvider()
{
return FixAllProvider.Create(async (context, document, diagnostics) => await FixAllAsync(document, diagnostics, context.CancellationToken).ConfigureAwait(false));
}

private static async Task<Document> FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
{
foreach (Diagnostic diagnostic in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start))
{
document = await ApplyFixToDocumentAsync(document, diagnostic, cancellationToken).ConfigureAwait(false);
}

return document;
}

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false);
Expand All @@ -39,29 +56,40 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Document document = context.Document;
Diagnostic diagnostic = context.Diagnostics[0];

switch (node)
string title = node switch
{
ImplicitArrayCreationExpressionSyntax => "Use explicitly typed array",
ArrayCreationExpressionSyntax => "Use implicitly typed array",
_ => throw new InvalidOperationException(),
};

CodeAction codeAction = CodeAction.Create(
title,
ct => ApplyFixToDocumentAsync(document, diagnostic, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
}

public static async Task<Document> ApplyFixToDocumentAsync(Document document, Diagnostic diag, CancellationToken cancellationToken)
{
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (!TryFindFirstAncestorOrSelf(
root,
diag.Location.SourceSpan,
out SyntaxNode node,
predicate: f => f.IsKind(SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.ArrayCreationExpression)))
{
case ImplicitArrayCreationExpressionSyntax implicitArrayCreation:
{
CodeAction codeAction = CodeAction.Create(
"Use explicitly typed array",
ct => ChangeArrayTypeToExplicitAsync(document, implicitArrayCreation, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case ArrayCreationExpressionSyntax arrayCreation:
{
CodeAction codeAction = CodeAction.Create(
"Use implicitly typed array",
ct => ChangeArrayTypeToImplicitAsync(document, arrayCreation, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
break;
}
return null;
}

return node switch
{
ImplicitArrayCreationExpressionSyntax implicitArrayCreation => await ChangeArrayTypeToExplicitAsync(document, implicitArrayCreation, cancellationToken).ConfigureAwait(false),
ArrayCreationExpressionSyntax arrayCreation => await ChangeArrayTypeToImplicitAsync(document, arrayCreation, cancellationToken).ConfigureAwait(false),
_ => throw new InvalidOperationException()
};
}

private static async Task<Document> ChangeArrayTypeToExplicitAsync(
Expand Down Expand Up @@ -101,13 +129,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
ArrayCreationExpressionSyntax arrayCreation,
CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

ArrayTypeSyntax arrayType = arrayCreation.Type;
SyntaxList<ArrayRankSpecifierSyntax> rankSpecifiers = arrayType.RankSpecifiers;
InitializerExpressionSyntax initializer = arrayCreation.Initializer;

ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(arrayType.ElementType, cancellationToken);
TypeSyntax castType;

if (rankSpecifiers.Count > 1)
Expand Down