public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken); ForEachStatementSyntax forEachStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ForEachStatementSyntax>(); if (forEachStatement == null) { return; } SemanticModel semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken); if (semanticModel == null) { return; } ChangeType(context, semanticModel, forEachStatement); RenameIdentifierNameAccordingToTypeName(context, semanticModel, forEachStatement); ChangeTypeAccordingToExpression(context, semanticModel, forEachStatement); if (ForEachToForRefactoring.CanRefactor(forEachStatement, semanticModel, context.CancellationToken)) { context.RegisterRefactoring( "Convert foreach to for", cancellationToken => ForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, cancellationToken)); } }
private void AnalyzeForEachStatement(SyntaxNodeAnalysisContext context) { if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true) { return; } var forEachStatement = (ForEachStatementSyntax)context.Node; if (!ForEachToForRefactoring.CanRefactor(forEachStatement, context.SemanticModel, context.CancellationToken)) { return; } context.ReportDiagnostic( DiagnosticDescriptors.ConvertForEachToFor, forEachStatement.Type.GetLocation()); TextSpan span = TextSpan.FromBounds(forEachStatement.ForEachKeyword.SpanStart + 3, forEachStatement.ForEachKeyword.Span.End); Location location = Location.Create(forEachStatement.SyntaxTree, span); context.ReportDiagnostic(DiagnosticDescriptors.ConvertForEachToForFadeOut, location); }