public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocation) { if (invocation.IsParentKind(SyntaxKind.ExpressionStatement)) { return; } SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocation); if (!invocationInfo.Success) { return; } switch (invocationInfo.NameText) { case "First": { if (invocationInfo.Arguments.Any()) { break; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (!UseElementAccessAnalysis.IsFixableFirst(invocationInfo, semanticModel, context.CancellationToken)) { break; } context.RegisterRefactoring( "Use [] instead of calling 'First'", ct => UseElementAccessInsteadOfEnumerableMethodRefactoring.UseElementAccessInsteadOfFirstAsync(context.Document, invocation, ct), RefactoringDescriptors.UseElementAccessInsteadOfLinqMethod); break; } case "Last": { if (invocationInfo.Arguments.Any()) { break; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (!UseElementAccessAnalysis.IsFixableLast(invocationInfo, semanticModel, context.CancellationToken)) { break; } string propertyName = CSharpUtility.GetCountOrLengthPropertyName(invocationInfo.Expression, semanticModel, context.CancellationToken); if (propertyName == null) { break; } context.RegisterRefactoring( "Use [] instead of calling 'Last'", ct => UseElementAccessInsteadOfEnumerableMethodRefactoring.UseElementAccessInsteadOfLastAsync(context.Document, invocation, propertyName, ct), RefactoringDescriptors.UseElementAccessInsteadOfLinqMethod); break; } case "ElementAt": { if (invocationInfo.Arguments.SingleOrDefault(shouldThrow: false)?.Expression?.IsMissing != false) { break; } SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (!UseElementAccessAnalysis.IsFixableElementAt(invocationInfo, semanticModel, context.CancellationToken)) { break; } context.RegisterRefactoring( "Use [] instead of calling 'ElementAt'", ct => UseElementAccessInsteadOfEnumerableMethodRefactoring.UseElementAccessInsteadOfElementAtAsync(context.Document, invocation, ct), RefactoringDescriptors.UseElementAccessInsteadOfLinqMethod); break; } } }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, InvocationExpressionSyntax invocationExpression) { if (context.IsAnyRefactoringEnabled( RefactoringIdentifiers.UseElementAccessInsteadOfEnumerableMethod, RefactoringIdentifiers.ReplaceAnyWithAllOrAllWithAny, RefactoringIdentifiers.CallExtensionMethodAsInstanceMethod, RefactoringIdentifiers.ReplaceStringContainsWithStringIndexOf)) { ExpressionSyntax expression = invocationExpression.Expression; if (expression != null && invocationExpression.ArgumentList != null) { if (expression.IsKind(SyntaxKind.SimpleMemberAccessExpression) && ((MemberAccessExpressionSyntax)expression).Name?.Span.Contains(context.Span) == true) { if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseElementAccessInsteadOfEnumerableMethod)) { await UseElementAccessInsteadOfEnumerableMethodRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceAnyWithAllOrAllWithAny)) { await ReplaceAnyWithAllOrAllWithAnyRefactoring.ComputeRefactoringAsync(context, invocationExpression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStringContainsWithStringIndexOf)) { await ReplaceStringContainsWithStringIndexOfRefactoring.ComputeRefactoringAsync(context, invocationExpression).ConfigureAwait(false); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.CallExtensionMethodAsInstanceMethod)) { SyntaxNodeOrToken nodeOrToken = CallExtensionMethodAsInstanceMethodAnalysis.GetNodeOrToken(expression); if (nodeOrToken.Span.Contains(context.Span)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); CallExtensionMethodAsInstanceMethodAnalysisResult analysis = CallExtensionMethodAsInstanceMethodAnalysis.Analyze(invocationExpression, semanticModel, allowAnyExpression: true, cancellationToken: context.CancellationToken); if (analysis.Success) { context.RegisterRefactoring( CallExtensionMethodAsInstanceMethodRefactoring.Title, cancellationToken => { return(context.Document.ReplaceNodeAsync( analysis.InvocationExpression, analysis.NewInvocationExpression, cancellationToken)); }); } } } } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceStringFormatWithInterpolatedString) && context.SupportsCSharp6) { await ReplaceStringFormatWithInterpolatedStringRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false); } if (context.IsRefactoringEnabled(RefactoringIdentifiers.UseBitwiseOperationInsteadOfCallingHasFlag)) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (UseBitwiseOperationInsteadOfCallingHasFlagAnalysis.IsFixable(invocationExpression, semanticModel, context.CancellationToken)) { context.RegisterRefactoring( UseBitwiseOperationInsteadOfCallingHasFlagRefactoring.Title, cancellationToken => { return(UseBitwiseOperationInsteadOfCallingHasFlagRefactoring.RefactorAsync( context.Document, invocationExpression, cancellationToken)); }); } } if (context.IsRefactoringEnabled(RefactoringIdentifiers.InlineMethod)) { await InlineMethodRefactoring.ComputeRefactoringsAsync(context, invocationExpression).ConfigureAwait(false); } }