private static void ChangeType( RefactoringContext context, VariableDeclarationSyntax variableDeclaration, ITypeSymbol typeSymbol, SemanticModel semanticModel, CancellationToken cancellationToken) { TypeSyntax type = variableDeclaration.Type; if (variableDeclaration.Variables.Count == 1 && variableDeclaration.Variables[0].Initializer?.Value != null && typeSymbol.IsNamedType()) { INamedTypeSymbol taskOfT = semanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task`1"); if (((INamedTypeSymbol)typeSymbol).ConstructedFrom.Equals(taskOfT) && AsyncAnalysis.IsPartOfAsyncBlock(variableDeclaration, semanticModel, cancellationToken)) { ITypeSymbol typeArgumentType = ((INamedTypeSymbol)typeSymbol).TypeArguments[0]; context.RegisterRefactoring( $"Change type to '{typeArgumentType.ToDisplayString(SyntaxUtility.DefaultSymbolDisplayFormat)}' and insert 'await'", c => { return(ChangeTypeAndAddAwaitAsync( context.Document, variableDeclaration, typeArgumentType, c)); }); } } context.RegisterRefactoring( $"Change type to '{typeSymbol.ToDisplayString(SyntaxUtility.DefaultSymbolDisplayFormat)}'", c => { return(ChangeTypeRefactoring.ChangeTypeAsync( context.Document, type, typeSymbol, c)); }); }
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var methodDeclaration = (MethodDeclarationSyntax)context.Node; IMethodSymbol methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclaration, context.CancellationToken); if (methodSymbol != null) { SyntaxToken identifier = methodDeclaration.Identifier; if (methodSymbol.IsAsync) { if (!methodSymbol.Name.EndsWith(AsyncSuffix, StringComparison.Ordinal) && AsyncAnalysis.ContainsAwait(methodDeclaration)) { context.ReportDiagnostic( DiagnosticDescriptors.AsynchronousMethodNameShouldEndWithAsync, identifier.GetLocation()); } } else if (!methodSymbol.IsAbstract && methodSymbol.Name.EndsWith(AsyncSuffix, StringComparison.Ordinal) && ShouldRemoveSuffix(methodSymbol, context.SemanticModel)) { context.ReportDiagnostic( DiagnosticDescriptors.NonAsynchronousMethodNameShouldNotEndWithAsync, identifier.GetLocation()); context.ReportDiagnostic( DiagnosticDescriptors.NonAsynchronousMethodNameShouldNotEndWithAsyncFadeOut, Location.Create(identifier.SyntaxTree, TextSpan.FromBounds(identifier.Span.End - AsyncSuffix.Length, identifier.Span.End))); } } }