private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, StatementListSelection selectedStatements, CancellationToken cancellationToken) { int count = selectedStatements.Count; StatementSyntax newStatement; if (count == 1 && !ifStatement.AsCascade().Any(f => f.Statement?.Kind() == SyntaxKind.Block)) { newStatement = selectedStatements.First(); } else { newStatement = SyntaxFactory.Block(selectedStatements); } ElseClauseSyntax elseClause = SyntaxFactory.ElseClause(newStatement).WithFormatterAnnotation(); IfStatementSyntax lastIfStatement = ifStatement.AsCascade().Last(); IfStatementSyntax newIfStatement = ifStatement.ReplaceNode( lastIfStatement, lastIfStatement.WithElse(elseClause)); SyntaxList <StatementSyntax> newStatements = selectedStatements .UnderlyingList .Replace(ifStatement, newIfStatement) .RemoveRange(selectedStatements.FirstIndex, count); return(document.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, ObjectCreationExpressionSyntax objectCreation, StatementListSelection selectedStatements, CancellationToken cancellationToken = default) { ExpressionStatementSyntax[] expressionStatements = selectedStatements .Skip(1) .Cast <ExpressionStatementSyntax>() .ToArray(); StatementSyntax firstStatement = selectedStatements.First(); SyntaxList <StatementSyntax> newStatements = selectedStatements.UnderlyingList.Replace( firstStatement, firstStatement.ReplaceNode( objectCreation, objectCreation.WithInitializer(CreateInitializer(objectCreation, expressionStatements)))); int count = expressionStatements.Length; int index = selectedStatements.FirstIndex + 1; while (count > 0) { newStatements = newStatements.RemoveAt(index); count--; } return(document.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, 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), 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 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)); } } } }
internal static async Task ComputeRefactoringAsync(RefactoringContext context, StatementListSelection selectedStatements) { if (selectedStatements.Count <= 1) { return; } StatementSyntax statement = selectedStatements.First(); SyntaxKind kind = statement.Kind(); if (kind == SyntaxKind.LocalDeclarationStatement) { var localDeclaration = (LocalDeclarationStatementSyntax)statement; SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo(localDeclaration); SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (!CanRefactor(localInfo, semanticModel, context.CancellationToken)) { return; } RegisterRefactoring(context, IdentifierName(localInfo.Identifier), localDeclaration, selectedStatements.Count - 1); } else if (kind == SyntaxKind.ExpressionStatement) { var expressionStatement = (ExpressionStatementSyntax)statement; SimpleAssignmentStatementInfo assignmentInfo = SyntaxInfo.SimpleAssignmentStatementInfo(expressionStatement); SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); if (!CanRefactor(assignmentInfo, assignmentInfo.Left, semanticModel, context.CancellationToken)) { return; } RegisterRefactoring(context, assignmentInfo.Left, expressionStatement, selectedStatements.Count - 1); } }
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); }
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)); }
public static async Task ComputeRefactoringsAsync(RefactoringContext context, StatementListSelection selectedStatements) { if (selectedStatements.Count <= 1) { return; } StatementSyntax firstStatement = selectedStatements.First(); SemanticModel semanticModel = null; ISymbol symbol = null; ObjectCreationExpressionSyntax objectCreation = null; SyntaxKind kind = firstStatement.Kind(); if (kind == SyntaxKind.LocalDeclarationStatement) { var localDeclaration = (LocalDeclarationStatementSyntax)firstStatement; VariableDeclaratorSyntax variable = localDeclaration .Declaration? .Variables .SingleOrDefault(shouldThrow: false); objectCreation = variable?.Initializer?.Value as ObjectCreationExpressionSyntax; if (objectCreation != null) { semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); symbol = semanticModel.GetDeclaredSymbol(variable, context.CancellationToken); } } else if (kind == SyntaxKind.ExpressionStatement) { var expressionStatement = (ExpressionStatementSyntax)firstStatement; if (expressionStatement.Expression is AssignmentExpressionSyntax assignment) { objectCreation = assignment.Right as ObjectCreationExpressionSyntax; if (objectCreation != null) { semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false); symbol = semanticModel.GetSymbol(assignment.Left, context.CancellationToken); } } } if (objectCreation == null) { return; } if (symbol?.IsErrorType() != false) { return; } for (int i = 1; i < selectedStatements.Count; i++) { if (!IsValidAssignmentStatement(selectedStatements[i], symbol, semanticModel, context.CancellationToken)) { return; } } context.RegisterRefactoring( "Collapse to initializer", ct => RefactorAsync(context.Document, objectCreation, selectedStatements, ct), RefactoringIdentifiers.CollapseToInitializer); }
internal static StatementListInfo Create(StatementListSelection selectedStatements) { return(Create(selectedStatements?.First())); }
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); }