Skip to content

Commit

Permalink
Merge pull request #743 from Youssef1313/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf authored Sep 17, 2021
2 parents 3e776c4 + 23cf647 commit 249b184
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
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

0 comments on commit 249b184

Please sign in to comment.