public static bool IsFixable( SimpleMemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken) { ExpressionSyntax argumentExpression = invocationInfo.Arguments[0].Expression; if (argumentExpression?.IsMissing != false) { return(false); } if (invocationInfo.Expression?.IsMissing != false) { return(false); } IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol; if (methodSymbol == null) { return(false); } if (!SymbolUtility.IsLinqElementAt(methodSymbol, semanticModel, allowImmutableArrayExtension: true)) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken); return(SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart)); }
public static bool IsFixable( SimpleMemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken) { if (invocationInfo.Expression?.IsMissing != false) { return(false); } IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol; if (methodSymbol == null) { return(false); } if (!SymbolUtility.IsLinqExtensionOfIEnumerableOfTWithoutParameters(methodSymbol, "First", semanticModel, allowImmutableArrayExtension: true)) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken); return(SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart)); }
public static bool CanRefactor( MemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken) { ExpressionSyntax argumentExpression = invocationInfo.Arguments[0].Expression; if (argumentExpression?.IsMissing != false) { return(false); } if (invocationInfo.Expression?.IsMissing != false) { return(false); } if (!semanticModel.TryGetExtensionMethodInfo(invocationInfo.InvocationExpression, out MethodInfo methodInfo, ExtensionMethodKind.Reduced, cancellationToken)) { return(false); } if (!methodInfo.IsLinqElementAt(allowImmutableArrayExtension: true)) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken); return(SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart)); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement) { if (context.IsAnyRefactoringEnabled( RefactoringDescriptors.UseImplicitType, RefactoringDescriptors.UseExplicitType, RefactoringDescriptors.ChangeTypeAccordingToExpression)) { await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringDescriptors.RenameIdentifierAccordingToTypeName)) { await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false); } SemanticModel semanticModel = null; if (context.IsAnyRefactoringEnabled(RefactoringDescriptors.ConvertForEachToFor, RefactoringDescriptors.ConvertForEachToForAndReverseLoop) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forEachStatement)) { semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(forEachStatement.Expression, context.CancellationToken); if (SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, forEachStatement.SpanStart)) { if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertForEachToFor)) { context.RegisterRefactoring( "Convert to 'for'", ct => ConvertForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: false, cancellationToken: ct), RefactoringDescriptors.ConvertForEachToFor); } if (context.IsRefactoringEnabled(RefactoringDescriptors.ConvertForEachToForAndReverseLoop)) { context.RegisterRefactoring( "Convert to 'for' and reverse loop", ct => ConvertForEachToForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: true, cancellationToken: ct), RefactoringDescriptors.ConvertForEachToForAndReverseLoop); } } } if (context.IsRefactoringEnabled(RefactoringDescriptors.DeconstructForeachVariable) && TextSpan.FromBounds(forEachStatement.Type.SpanStart, forEachStatement.Identifier.Span.End).Contains(context.Span)) { semanticModel ??= await context.GetSemanticModelAsync().ConfigureAwait(false); DeconstructForeachVariableRefactoring.ComputeRefactoring(context, forEachStatement, semanticModel); } if (context.IsRefactoringEnabled(RefactoringDescriptors.UseEnumeratorExplicitly) && context.Span.IsEmptyAndContainedInSpan(forEachStatement.ForEachKeyword)) { UseEnumeratorExplicitlyRefactoring.ComputeRefactoring(context, forEachStatement); } }
public static bool CanRefactor( ForEachStatementSyntax forEachStatement, SemanticModel semanticModel, CancellationToken cancellationToken = default(CancellationToken)) { ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(forEachStatement.Expression, cancellationToken); return(SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, forEachStatement.SpanStart)); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, ForEachStatementSyntax forEachStatement) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.ChangeExplicitTypeToVar, RefactoringIdentifiers.ChangeVarToExplicitType, RefactoringIdentifiers.ChangeTypeAccordingToExpression)) { await ChangeTypeAsync(context, forEachStatement).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.RenameIdentifierAccordingToTypeName)) { await RenameIdentifierAccordingToTypeNameAsync(context, forEachStatement).ConfigureAwait(false); } if (context.IsAnyRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor, RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop) && context.Span.IsEmptyAndContainedInSpanOrBetweenSpans(forEachStatement)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(forEachStatement.Expression, context.CancellationToken); if (SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, forEachStatement.SpanStart)) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithFor)) { context.RegisterRefactoring( "Replace foreach with for", cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: false, cancellationToken: cancellationToken), RefactoringIdentifiers.ReplaceForEachWithFor); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop)) { context.RegisterRefactoring( "Replace foreach with for and reverse loop", cancellationToken => ReplaceForEachWithForRefactoring.RefactorAsync(context.Document, forEachStatement, semanticModel: semanticModel, reverseLoop: true, cancellationToken: cancellationToken), RefactoringIdentifiers.ReplaceForEachWithForAndReverseLoop); } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceForEachWithEnumerator) && context.Span.IsEmptyAndContainedInSpan(forEachStatement.ForEachKeyword)) { ReplaceForEachWithEnumeratorRefactoring.ComputeRefactoring(context, forEachStatement); } }
public static bool CanRefactor(MemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken) { if (invocationInfo.Expression?.IsMissing != false) { return(false); } if (!semanticModel.TryGetExtensionMethodInfo(invocationInfo.InvocationExpression, out MethodInfo methodInfo, ExtensionMethodKind.Reduced, cancellationToken)) { return(false); } if (!methodInfo.IsLinqExtensionOfIEnumerableOfTWithoutParameters("First", allowImmutableArrayExtension: true)) { return(false); } ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(invocationInfo.Expression, cancellationToken); return(SymbolUtility.HasAccessibleIndexer(typeSymbol, semanticModel, invocationInfo.InvocationExpression.SpanStart)); }