public static void ComputeRefactorings(RefactoringContext context, StatementListSelection selectedStatements) { if (selectedStatements.Count != 2) { return; } SimpleAssignmentStatementInfo simpleAssignment = SyntaxInfo.SimpleAssignmentStatementInfo(selectedStatements.First()); if (!simpleAssignment.Success) { return; } if (!(selectedStatements.Last() is ReturnStatementSyntax returnStatement)) { return; } if (returnStatement.Expression == null) { return; } if (!CSharpFactory.AreEquivalent(simpleAssignment.Left, returnStatement.Expression)) { return; } context.RegisterRefactoring( "Merge statements", ct => RefactorAsync(context.Document, simpleAssignment.Statement, returnStatement, ct), RefactoringIdentifiers.MergeAssignmentExpressionWithReturnStatement); }
public static void ComputeRefactorings(RefactoringContext context, StatementListSelection selectedStatements) { if (selectedStatements.Count != 2) { return; } SimpleAssignmentStatementInfo simpleAssignment = SyntaxInfo.SimpleAssignmentStatementInfo(selectedStatements.First()); if (!simpleAssignment.Success) { return; } if (selectedStatements.Last() is not ReturnStatementSyntax returnStatement) { return; } if (returnStatement.Expression == null) { return; } if (!CSharpFactory.AreEquivalent(simpleAssignment.Left, returnStatement.Expression)) { return; } context.RegisterRefactoring( "Remove unnecessary assignment", ct => RefactorAsync(context.Document, simpleAssignment.Statement, returnStatement, ct), RefactoringDescriptors.RemoveUnnecessaryAssignment); }
public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements) { if (!(selectedStatements.Last() is WhileStatementSyntax whileStatement)) { return; } if (selectedStatements.Count == 1) { context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, cancellationToken), RefactoringIdentifiers.ConvertWhileToFor); } else { SyntaxKind kind = selectedStatements.First().Kind(); if (kind == SyntaxKind.LocalDeclarationStatement) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (FindLocalDeclarationStatementIndex( whileStatement, selectedStatements.UnderlyingList, selectedStatements.FirstIndex, selectedStatements.Count, mustBeReferencedInsideWhileStatement: false, semanticModel: semanticModel, cancellationToken: context.CancellationToken) == selectedStatements.FirstIndex) { List <LocalDeclarationStatementSyntax> localDeclarations = selectedStatements .Take(selectedStatements.Count - 1) .Cast <LocalDeclarationStatementSyntax>() .ToList(); context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, localDeclarations, cancellationToken), RefactoringIdentifiers.ConvertWhileToFor); } } else if (kind == SyntaxKind.ExpressionStatement) { if (VerifyExpressionStatements(selectedStatements)) { List <ExpressionStatementSyntax> expressionStatements = selectedStatements .Take(selectedStatements.Count - 1) .Cast <ExpressionStatementSyntax>() .ToList(); context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, expressionStatements, cancellationToken), RefactoringIdentifiers.ConvertWhileToFor); } } } }
public static void ComputeRefactoring(RefactoringContext context, StatementListSelection selectedStatements) { if (selectedStatements.FirstIndex == 0) { return; } if (!CSharpFacts.IsJumpStatement(selectedStatements.Last().Kind())) { return; } StatementSyntax prevStatement = selectedStatements.UnderlyingList[selectedStatements.FirstIndex - 1]; if (!(prevStatement is IfStatementSyntax ifStatement)) { return; } foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade()) { if (ifOrElse.IsElse) { return; } StatementSyntax statement = ifOrElse.AsIf().Statement; if (statement is BlockSyntax block) { statement = block.Statements.LastOrDefault(); } if (statement == null) { return; } if (!CSharpFacts.IsJumpStatement(statement.Kind())) { return; } } context.RegisterRefactoring( "Wrap in else clause", cancellationToken => RefactorAsync(context.Document, ifStatement, selectedStatements, cancellationToken), RefactoringIdentifiers.WrapInElseClause); }
public static void ComputeRefactoring(RefactoringContext context, StatementListSelection selectedStatements) { StatementSyntax lastStatement = selectedStatements.Last(); if (!lastStatement.IsKind(SyntaxKind.ReturnStatement)) { return; } if (selectedStatements.FirstIndex == 0) { return; } var returnStatement = (ReturnStatementSyntax)lastStatement; ExpressionSyntax expression = returnStatement.Expression; if (expression == null) { return; } StatementSyntax prevStatement = selectedStatements.UnderlyingList[selectedStatements.FirstIndex - 1]; if (!prevStatement.IsKind(SyntaxKind.IfStatement)) { return; } var ifStatement = (IfStatementSyntax)prevStatement; foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade()) { if (ifOrElse.IsElse) { return; } if (!IsLastStatementReturnStatement(ifOrElse)) { return; } } context.RegisterRefactoring( "Wrap in else clause", cancellationToken => RefactorAsync(context.Document, ifStatement, selectedStatements, cancellationToken)); }
public static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements) { if (!(selectedStatements.Last() is WhileStatementSyntax whileStatement)) { return; } if (selectedStatements.Count == 1) { context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, cancellationToken)); } else { SyntaxKind kind = selectedStatements.First().Kind(); if (kind == SyntaxKind.LocalDeclarationStatement) { SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (VerifyLocalDeclarationStatements(selectedStatements, semanticModel, context.CancellationToken)) { List <LocalDeclarationStatementSyntax> localDeclarations = selectedStatements .Take(selectedStatements.Count - 1) .Cast <LocalDeclarationStatementSyntax>() .ToList(); context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, localDeclarations, cancellationToken)); } } else if (kind == SyntaxKind.ExpressionStatement) { if (VerifyExpressionStatements(selectedStatements)) { List <ExpressionStatementSyntax> expressionStatements = selectedStatements .Take(selectedStatements.Count - 1) .Cast <ExpressionStatementSyntax>() .ToList(); context.RegisterRefactoring( Title, cancellationToken => RefactorAsync(context.Document, whileStatement, expressionStatements, cancellationToken)); } } } }
private static Task <Document> RefactorAsync( Document document, StatementListSelection selectedStatements, int ifStatementCount, CancellationToken cancellationToken) { IfStatementSyntax newIfStatement = null; for (int i = ifStatementCount - 1; i >= 0; i--) { var ifStatement = (IfStatementSyntax)selectedStatements[i]; IfStatementSyntax lastIf = ifStatement.GetCascadeInfo().Last.AsIf(); StatementSyntax elseStatement = newIfStatement; if (elseStatement == null) { if (selectedStatements.Count - ifStatementCount > 1) { elseStatement = Block(selectedStatements.Skip(ifStatementCount)); } else { elseStatement = selectedStatements.Last(); if (!elseStatement.IsKind(SyntaxKind.IfStatement) && lastIf.Statement.IsKind(SyntaxKind.Block)) { elseStatement = Block(elseStatement); } } } StatementSyntax newStatement = lastIf.Statement; if (!newStatement.IsKind(SyntaxKind.Block)) { if (elseStatement.IsKind(SyntaxKind.Block)) { newStatement = Block(newStatement); } else if (elseStatement.IsKind(SyntaxKind.IfStatement) && ((IfStatementSyntax)elseStatement).AsCascade().All(f => f.Statement.IsKind(SyntaxKind.Block))) { newStatement = Block(newStatement); } } IfStatementSyntax newLastIf = lastIf.Update( lastIf.IfKeyword, lastIf.OpenParenToken, lastIf.Condition, lastIf.CloseParenToken, newStatement, ElseClause(elseStatement)); newIfStatement = ifStatement.ReplaceNode(lastIf, newLastIf); } SyntaxList <StatementSyntax> newStatements = selectedStatements.UnderlyingList .Replace(selectedStatements.First(), newIfStatement.WithFormatterAnnotation()) .RemoveRange(selectedStatements.FirstIndex + 1, selectedStatements.Count - 1); return(document.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, cancellationToken)); }