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 2 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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add Custom FixAllProvider for [RCS1014](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1014.md) ([#1070](https://github.com/JosefPihrt/Roslynator/pull/1070)).
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved

### Changed

- [CLI] Bump Roslyn to 4.5.0 ([#1043](https://github.com/josefpihrt/roslynator/pull/1043)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Roslynator.CodeFixes;
using Roslynator.CSharp.CSharp.CodeFixes;
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Roslynator.CSharp.CodeFixes;
Expand All @@ -22,6 +23,11 @@ public override ImmutableArray<string> FixableDiagnosticIds
{
get { return ImmutableArray.Create(DiagnosticIdentifiers.UseExplicitlyOrImplicitlyTypedArray); }
}

public override FixAllProvider GetFixAllProvider()
{
return UseExplicitlyOrImplicitlyTypedArrayFixAllProvider.Instance;
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved
}

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
Expand All @@ -39,29 +45,41 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Document document = context.Document;
Diagnostic diagnostic = context.Diagnostics[0];

switch (node)
var title = node switch
{
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;
}
ImplicitArrayCreationExpressionSyntax => "Use explicitly typed array",
ArrayCreationExpressionSyntax => "Use implicitly typed array",
_ => "",
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
};

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

context.RegisterCodeFix(codeAction, diagnostic);
}

public async Task<Document> ApplyFixToDocument(Document document, Diagnostic diag, CancellationToken cancellationToken)
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

if (!TryFindFirstAncestorOrSelf(
root,
diag.Location.SourceSpan,
out SyntaxNode node,
predicate: f =>
f.IsKind(SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.ArrayCreationExpression)))
{
return null;
}

return node switch
{
ImplicitArrayCreationExpressionSyntax implicitArrayCreation => await ChangeArrayTypeToExplicitAsync(document, implicitArrayCreation, cancellationToken),
ArrayCreationExpressionSyntax arrayCreation => await ChangeArrayTypeToImplicitAsync(document, arrayCreation, cancellationToken),
_ => null
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
};
}

private static async Task<Document> ChangeArrayTypeToExplicitAsync(
Expand Down Expand Up @@ -101,13 +119,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Roslynator.CSharp.CodeFixes;

namespace Roslynator.CSharp.CSharp.CodeFixes;
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved

public class UseExplicitlyOrImplicitlyTypedArrayFixAllProvider : DocumentBasedFixAllProvider
{
public static readonly UseExplicitlyOrImplicitlyTypedArrayFixAllProvider Instance = new();
private UseExplicitlyOrImplicitlyTypedArrayFixAllProvider() { }

protected override async Task<Document> FixAllAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
{
var codeFixProvider = (UseExplicitlyOrImplicitlyTypedArrayCodeFixProvider)fixAllContext.CodeFixProvider;
foreach (var diag in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start))
{
document = await codeFixProvider.ApplyFixToDocument(document, diag, fixAllContext.CancellationToken);
}
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved

return document;
}
}