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

Update MakeConstCodeFixProvider #743

Merged
merged 4 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
namespace MakeConst
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MakeConstCodeFixProvider)), Shared]
public class MakeConstCodeFixProvider : CodeFixProvider
public sealed class MakeConstCodeFixProvider : CodeFixProvider
{
private const string title = "Make uppercase";

public sealed override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(MakeConstAnalyzer.MakeConstDiagnosticId);

public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken);
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

Diagnostic diagnostic = context.Diagnostics.First();
Microsoft.CodeAnalysis.Text.TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
Expand All @@ -44,7 +42,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(action, diagnostic);
}

private async Task<Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
private static async Task<Document> MakeConstAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken)
{
// Remove the leading trivia from the local declaration.
SyntaxToken firstToken = localDeclaration.GetFirstToken();
Expand All @@ -64,15 +62,15 @@ private async Task<Document> MakeConstAsync(Document document, LocalDeclarationS
TypeSyntax variableTypeName = variableDeclaration.Type;
if (variableTypeName.IsVar)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken);
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

// Special case: Ensure that 'var' isn't actually an alias to another type
// (e.g. using var = System.String).
IAliasSymbol aliasInfo = semanticModel.GetAliasInfo(variableTypeName);
IAliasSymbol aliasInfo = semanticModel.GetAliasInfo(variableTypeName, cancellationToken);
if (aliasInfo == null)
{
// Retrieve the type inferred for var.
ITypeSymbol type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
ITypeSymbol type = semanticModel.GetTypeInfo(variableTypeName, cancellationToken).ConvertedType;

// Special case: Ensure that 'var' isn't actually a type named 'var'.
if (type.Name != "var")
Expand Down Expand Up @@ -100,7 +98,7 @@ private async Task<Document> MakeConstAsync(Document document, LocalDeclarationS
LocalDeclarationStatementSyntax formattedLocal = newLocal.WithAdditionalAnnotations(Formatter.Annotation);

// Replace the old local declaration with the new local declaration.
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode newRoot = root.ReplaceNode(localDeclaration, formattedLocal);

// Return document with transformed tree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ Imports Microsoft.CodeAnalysis.VisualBasic
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax

<ExportCodeFixProvider(LanguageNames.VisualBasic, Name:="MakeConstVB"), [Shared]>
Public Class MakeConstCodeFixProvider
Public NotInheritable Class MakeConstCodeFixProvider
Inherits CodeFixProvider

Public NotOverridable Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String)
Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String)
Get
Return ImmutableArray.Create(MakeConstAnalyzer.MakeConstDiagnosticId)
End Get
End Property

Public NotOverridable Overrides Function GetFixAllProvider() As FixAllProvider
Return Nothing
Public Overrides Function GetFixAllProvider() As FixAllProvider
' See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers
Return WellKnownFixAllProviders.BatchFixer
End Function

Public NotOverridable Overrides Async Function RegisterCodeFixesAsync(context As CodeFixContext) As Task
Public Overrides Async Function RegisterCodeFixesAsync(context As CodeFixContext) As Task
Dim diagnostic = context.Diagnostics.First()
Dim diagnosticSpan = diagnostic.Location.SourceSpan
Dim root = Await context.Document.GetSyntaxRootAsync(context.CancellationToken)
Dim root = Await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(False)

' Find the local declaration identified by the diagnostic.
Dim declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType(Of LocalDeclarationStatementSyntax)().First()
Expand All @@ -42,7 +43,7 @@ Public Class MakeConstCodeFixProvider
context.RegisterCodeFix(action, diagnostic)
End Function

Private Async Function MakeConstAsync(document As Document, localDeclaration As LocalDeclarationStatementSyntax, cancellationToken As CancellationToken) As Task(Of Document)
Private Shared Async Function MakeConstAsync(document As Document, localDeclaration As LocalDeclarationStatementSyntax, cancellationToken As CancellationToken) As Task(Of Document)
' Create a const token with the leading trivia from the local declaration.
Dim firstToken = localDeclaration.GetFirstToken()
Dim constToken = SyntaxFactory.Token(
Expand All @@ -58,7 +59,7 @@ Public Class MakeConstCodeFixProvider
Dim formattedLocalDeclaration = newLocalDeclaration.WithAdditionalAnnotations(Formatter.Annotation)

' Replace the old local declaration with the new local declaration.
Dim root = Await document.GetSyntaxRootAsync(cancellationToken)
Dim root = Await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(False)
Dim newRoot = root.ReplaceNode(localDeclaration, formattedLocalDeclaration)

' Return document with transformed tree.
Expand Down