public static async Task <Document> RefactorAsync( Document document, StatementContainer container, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement, CancellationToken cancellationToken) { ExpressionSyntax expression = ReplaceIfWithStatementRefactoring.GetExpression( ifStatement.Condition, ReplaceIfWithStatementRefactoring.GetReturnExpression(ifStatement), returnStatement.Expression); ReturnStatementSyntax newReturnStatement = ReturnStatement(expression); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newReturnStatement); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); }
private static Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, SyntaxList <StatementSyntax> statements, StatementContainer container, int statementIndex, int lastStatementIndex, CancellationToken cancellationToken) { IEnumerable <StatementSyntax> blockStatements = statements .Skip(statementIndex + 1) .Take(lastStatementIndex - statementIndex); IfStatementSyntax ifStatement = CreateNullCheck(expression, List(blockStatements)); if (lastStatementIndex < statements.Count - 1) { ifStatement = ifStatement.AppendToTrailingTrivia(NewLine()); } IEnumerable <StatementSyntax> newStatements = statements.Take(statementIndex + 1) .Concat(new IfStatementSyntax[] { ifStatement }) .Concat(statements.Skip(lastStatementIndex + 1)); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
private static async Task <Document> RefactorAsync( Document document, StatementContainer container, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement, ExpressionSyntax whenTrue, ExpressionSyntax whenFalse, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); ConditionalExpressionSyntax conditionalExpression = ReplaceIfWithStatementRefactoring.CreateConditionalExpression(ifStatement.Condition, whenTrue, whenFalse); ReturnStatementSyntax newReturnStatement = ReturnStatement(conditionalExpression) .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .Remove(returnStatement) .ReplaceAt(index, newReturnStatement); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); }
private static Task <Document> DuplicateStatementAsync( Document document, StatementSyntax statement, CancellationToken cancellationToken = default(CancellationToken)) { StatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { int index = container.Statements.IndexOf(statement); if (index == 0 && container.IsBlock && container.Block.OpenBraceToken.GetFullSpanEndLine() == statement.GetFullSpanStartLine()) { statement = statement.PrependToLeadingTrivia(CSharpFactory.NewLine()); } SyntaxList <StatementSyntax> newStatements = container.Statements.Insert(index + 1, statement); SyntaxNode newNode = container.NodeWithStatements(newStatements); return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken)); } else { SyntaxList <StatementSyntax> statements = SyntaxFactory.List(new StatementSyntax[] { statement, statement }); BlockSyntax block = SyntaxFactory.Block(statements); return(document.ReplaceNodeAsync(statement, block, cancellationToken)); } }
public static Task <Document> RefactorAsync( Document document, StatementContainer container, ImmutableArray <IfStatementSyntax> ifStatements, CancellationToken cancellationToken = default(CancellationToken)) { IfStatementSyntax newIfStatement = IfStatement( CreateCondition(ifStatements), Block(CreateStatements(ifStatements))); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatements[0]); SyntaxList <StatementSyntax> newStatements = statements.Replace( ifStatements[0], newIfStatement .WithLeadingTrivia(ifStatements[0].GetLeadingTrivia()) .WithTrailingTrivia(ifStatements[ifStatements.Length - 1].GetTrailingTrivia())); for (int i = 1; i < ifStatements.Length; i++) { newStatements = newStatements.RemoveAt(index + 1); } return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
public override async Task <Document> RefactorAsync(Document document, CancellationToken cancellationToken = default(CancellationToken)) { StatementContainer container = StatementContainer.Create(IfStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(IfStatement); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); ExpressionSyntax expression = IfRefactoringHelper.GetBooleanExpression( IfStatement.Condition, Expression1, Expression2, semanticModel, cancellationToken); StatementSyntax newStatement = CreateStatement(expression) .WithLeadingTrivia(IfStatement.GetLeadingTrivia()) .WithTrailingTrivia(statements[index + 1].GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newStatement); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); }
private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement, StatementContainer container, CancellationToken cancellationToken) { SyntaxList <StatementSyntax> statements = container.Statements; IfStatement ifElse = IfStatement.Create(ifStatement); StatementSyntax statement = returnStatement; if (ifElse.Nodes.Any(f => f.Statement?.IsKind(SyntaxKind.Block) == true)) { statement = SyntaxFactory.Block(statement); } ElseClauseSyntax elseClause = SyntaxFactory.ElseClause(statement).WithFormatterAnnotation(); IfStatementSyntax lastIfStatement = ifElse.Nodes.Last(); IfStatementSyntax newIfStatement = ifStatement.ReplaceNode( lastIfStatement, lastIfStatement.WithElse(elseClause)); SyntaxList <StatementSyntax> newStatements = statements .Replace(ifStatement, newIfStatement) .RemoveAt(statements.IndexOf(returnStatement)); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, UsingStatementSyntax usingStatement, CancellationToken cancellationToken = default(CancellationToken)) { StatementContainer container = StatementContainer.Create(usingStatement); int index = container.Statements.IndexOf(usingStatement); StatementContainer newContainer = container.RemoveStatementAt(index); var statements = new List <StatementSyntax>() { SyntaxFactory.LocalDeclarationStatement(usingStatement.Declaration) }; statements.AddRange(GetStatements(usingStatement)); if (statements.Count > 0) { statements[0] = statements[0] .WithLeadingTrivia(usingStatement.GetLeadingTrivia()); statements[statements.Count - 1] = statements[statements.Count - 1] .WithTrailingTrivia(usingStatement.GetTrailingTrivia()); } newContainer = newContainer.WithStatements(newContainer.Statements.InsertRange(index, statements)); return(document.ReplaceNodeAsync(container.Node, newContainer.Node.WithFormatterAnnotation(), cancellationToken)); }
public override async Task <Document> RefactorAsync(Document document, CancellationToken cancellationToken) { IStatementContainer container = StatementContainer.Create(IfStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(IfStatement); ExpressionSyntax left = Left.WithoutTrivia(); ExpressionSyntax right = Right.WithoutTrivia(); SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); right = AddCastExpressionIfNecessary(right, semanticModel, IfStatement.SpanStart, cancellationToken); StatementSyntax newNode = CreateStatement( CoalesceExpression( left.Parenthesize().WithSimplifierAnnotation(), right.Parenthesize().WithSimplifierAnnotation())); newNode = newNode .WithLeadingTrivia(IfStatement.GetLeadingTrivia()) .WithTrailingTrivia(statements[index + 1].GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newNode); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); }
public static Task <Document> RefactorAsync( Document document, ObjectCreationExpressionSyntax objectCreation, StatementContainerSelection selectedStatements, CancellationToken cancellationToken = default(CancellationToken)) { StatementContainer container = selectedStatements.Container; ExpressionStatementSyntax[] expressionStatements = selectedStatements .Skip(1) .Cast <ExpressionStatementSyntax>() .ToArray(); StatementSyntax firstStatement = selectedStatements.First(); SyntaxList <StatementSyntax> newStatements = container.Statements.Replace( firstStatement, firstStatement.ReplaceNode( objectCreation, objectCreation.WithInitializer(CreateInitializer(objectCreation, expressionStatements)))); int count = expressionStatements.Length; int index = selectedStatements.StartIndex + 1; while (count > 0) { newStatements = newStatements.RemoveAt(index); count--; } return(document.ReplaceNodeAsync( container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
private static async Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, StatementSyntax statement, int statementCount, CancellationToken cancellationToken) { StatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int statementIndex = statements.IndexOf(statement); return(await RefactorAsync( document, expression, statements, container, statementIndex, statementIndex + statementCount, cancellationToken).ConfigureAwait(false)); } return(document); }
private SyntaxNode Rewrite(StatementContainer container) { _container = container; var ifStatement = (IfStatementSyntax)_container.Statements.LastButOne(); return(Rewrite(_container, ifStatement)); }
internal static void ComputeRefactoring(RefactoringContext context, BinaryExpressionSelection binaryExpressionSelection) { BinaryExpressionSyntax binaryExpression = binaryExpressionSelection.BinaryExpression; SyntaxKind kind = binaryExpression.Kind(); if (kind == SyntaxKind.LogicalAndExpression || kind == SyntaxKind.LogicalOrExpression) { BinaryExpressionSyntax condition = GetCondition(binaryExpression); if (condition != null) { SyntaxNode parent = condition.Parent; switch (parent?.Kind()) { case SyntaxKind.IfStatement: { if (kind == SyntaxKind.LogicalAndExpression) { var refactoring = new ExtractConditionFromIfToNestedIfRefactoring(); context.RegisterRefactoring( refactoring.Title, cancellationToken => refactoring.RefactorAsync(context.Document, (IfStatementSyntax)parent, condition, binaryExpressionSelection, cancellationToken)); } else if (kind == SyntaxKind.LogicalOrExpression) { StatementContainer container; if (StatementContainer.TryCreate((StatementSyntax)parent, out container)) { var refactoring = new ExtractConditionFromIfToIfRefactoring(); context.RegisterRefactoring( refactoring.Title, cancellationToken => refactoring.RefactorAsync(context.Document, container, condition, binaryExpressionSelection, cancellationToken)); } } break; } case SyntaxKind.WhileStatement: { if (kind == SyntaxKind.LogicalAndExpression) { var refactoring = new ExtractConditionFromWhileToNestedIfRefactoring(); context.RegisterRefactoring( refactoring.Title, cancellationToken => refactoring.RefactorAsync(context.Document, (WhileStatementSyntax)parent, condition, binaryExpressionSelection, cancellationToken)); } break; } } } } }
private static Task <Document> RefactorAsync( Document document, StatementContainer statementContainer, StatementSyntax statement, InitializerExpressionSyntax initializer, ExpressionSyntax initializedExpression, CancellationToken cancellationToken) { ExpressionStatementSyntax[] expressions = CreateExpressionStatements(initializer, initializedExpression).ToArray(); expressions[expressions.Length - 1] = expressions[expressions.Length - 1] .WithTrailingTrivia(statement.GetTrailingTrivia()); var objectCreationExpression = (ObjectCreationExpressionSyntax)initializer.Parent; ObjectCreationExpressionSyntax newObjectCreationExpression = objectCreationExpression.WithInitializer(null); if (newObjectCreationExpression.ArgumentList == null) { TypeSyntax type = newObjectCreationExpression.Type; newObjectCreationExpression = newObjectCreationExpression .WithArgumentList(ArgumentList().WithTrailingTrivia(type.GetTrailingTrivia())) .WithType(type.WithoutTrailingTrivia()); } SyntaxList <StatementSyntax> statements = statementContainer.Statements; int index = statements.IndexOf(statement); StatementSyntax newStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression); SyntaxKind statementKind = statement.Kind(); if (statementKind == SyntaxKind.ExpressionStatement) { var expressionStatement = (ExpressionStatementSyntax)newStatement; newStatement = expressionStatement .WithExpression(expressionStatement.Expression.WithoutTrailingTrivia()); } else if (statementKind == SyntaxKind.LocalDeclarationStatement) { var localDeclaration = (LocalDeclarationStatementSyntax)newStatement; newStatement = localDeclaration .WithDeclaration(localDeclaration.Declaration.WithoutTrailingTrivia()); } SyntaxList <StatementSyntax> newStatements = statements.Replace(statement, newStatement); SyntaxNode newNode = statementContainer .NodeWithStatements(newStatements.InsertRange(index + 1, expressions)) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(statementContainer.Node, newNode, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, VariableDeclaratorSyntax declarator, CancellationToken cancellationToken) { var declaration = (VariableDeclarationSyntax)declarator.Parent; var localDeclaration = (LocalDeclarationStatementSyntax)declaration.Parent; StatementContainer container = StatementContainer.Create(localDeclaration); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(localDeclaration); StatementSyntax nextStatement = statements[index + 1]; var expressionStatement = (ExpressionStatementSyntax)nextStatement; var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression; ExpressionSyntax right = assignment.Right; EqualsValueClauseSyntax initializer = declarator.Initializer; ExpressionSyntax value = initializer?.Value; VariableDeclaratorSyntax newDeclarator = (value != null) ? declarator.ReplaceNode(value, right) : declarator.WithInitializer(EqualsValueClause(right)); LocalDeclarationStatementSyntax newLocalDeclaration = localDeclaration.ReplaceNode(declarator, newDeclarator); SyntaxTriviaList trailingTrivia = localDeclaration.GetTrailingTrivia(); if (!trailingTrivia.IsEmptyOrWhitespace()) { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(trailingTrivia.Concat(nextStatement.GetTrailingTrivia())); } else { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(nextStatement.GetTrailingTrivia()); } SyntaxTriviaList leadingTrivia = nextStatement.GetLeadingTrivia(); if (!leadingTrivia.IsEmptyOrWhitespace()) { newLocalDeclaration = newLocalDeclaration.WithLeadingTrivia(newLocalDeclaration.GetLeadingTrivia().Concat(leadingTrivia)); } SyntaxList <StatementSyntax> newStatements = statements .Replace(localDeclaration, newLocalDeclaration) .RemoveAt(index + 1); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
private static async Task <Document> RefactorAsync( Document document, List <LocalDeclarationStatementSyntax> localDeclarations, List <ExpressionSyntax> expressions, WhileStatementSyntax whileStatement, CancellationToken cancellationToken) { var declaration = default(VariableDeclarationSyntax); var initializers = default(SeparatedSyntaxList <ExpressionSyntax>); var incrementors = default(SeparatedSyntaxList <ExpressionSyntax>); if (localDeclarations != null) { IEnumerable <VariableDeclarationSyntax> declarations = localDeclarations .Select(f => f.Declaration) .Where(f => f != null); TypeSyntax type = declarations.First().Type.TrimTrivia(); IEnumerable <VariableDeclaratorSyntax> variables = declarations .SelectMany(f => f.Variables) .Select(f => f.TrimTrivia()); declaration = VariableDeclaration(type, SeparatedList(variables)); StatementContainer container; if (StatementContainer.TryCreate(whileStatement, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(localDeclarations[0]); ForStatementSyntax forStatement = CreateForStatement(whileStatement, declaration, initializers, incrementors); forStatement = forStatement .TrimLeadingTrivia() .PrependToLeadingTrivia(localDeclarations[0].GetLeadingTrivia()); IEnumerable <StatementSyntax> newStatements = statements.Take(index) .Concat(new ForStatementSyntax[] { forStatement }) .Concat(statements.Skip(index + localDeclarations.Count + 1)); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); } } else if (expressions != null) { initializers = SeparatedList(expressions); } return(await document.ReplaceNodeAsync( whileStatement, CreateForStatement(whileStatement, declaration, initializers, incrementors), cancellationToken).ConfigureAwait(false)); }
public static void AnalyzeLocalDeclarationStatement(SyntaxNodeAnalysisContext context) { var localDeclaration = (LocalDeclarationStatementSyntax)context.Node; SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; if (!localDeclaration.IsConst) { StatementContainer container; if (StatementContainer.TryCreate(localDeclaration, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; if (statements.Count > 1) { int index = statements.IndexOf(localDeclaration); if (index < statements.Count - 1) { VariableDeclarationSyntax variableDeclaration = localDeclaration.Declaration; if (variableDeclaration?.IsMissing == false) { SeparatedSyntaxList <VariableDeclaratorSyntax> variables = variableDeclaration.Variables; if (variables.Any()) { TypeSyntax type = variableDeclaration.Type; ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(type, cancellationToken); if (typeSymbol?.SupportsConstantValue() == true && variables.All(variable => HasConstantValue(variable.Initializer?.Value, typeSymbol, semanticModel, cancellationToken))) { TextSpan span = TextSpan.FromBounds(statements[index + 1].Span.Start, statements.Last().Span.End); IEnumerable <SyntaxNode> nodes = container.Node.DescendantNodes(span); if (!IsAnyVariableInvalidOrAssigned( variables, nodes, semanticModel, cancellationToken)) { context.ReportDiagnostic( DiagnosticDescriptors.MarkLocalVariableAsConst, type); } } } } } } } } }
private static IStatementContainer GetStatementContainer(StatementSyntax statement) { IStatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { return(container); } return(null); }
public bool Analyze( MemberInvocationExpression memberInvocation, StatementSyntax statement, string name, SemanticModel semanticModel, CancellationToken cancellationToken) { if (statement.SpanOrTrailingTriviaContainsDirectives()) { return(false); } if (!StatementContainer.TryCreate(statement, out StatementContainer container)) { return(false); } SyntaxList <StatementSyntax> statements = container.Statements; if (statements.Count == 1) { return(false); } if (!semanticModel.TryGetMethodInfo(memberInvocation.InvocationExpression, out MethodInfo methodInfo, cancellationToken)) { return(false); } ITypeSymbol typeSymbol = methodInfo.ReturnType; int i = statements.IndexOf(statement); if (i != 0 && IsFixableStatement(statements[i - 1], name, typeSymbol, semanticModel, cancellationToken)) { return(false); } int j = i; while (j < statements.Count - 1) { if (!IsFixableStatement(statements[j + 1], name, typeSymbol, semanticModel, cancellationToken)) { break; } j++; } return(j > i); }
public static void Analyze(SyntaxNodeAnalysisContext context, MemberInvocationExpression memberInvocation) { InvocationExpressionSyntax invocationExpression = memberInvocation.InvocationExpression; if (invocationExpression.IsParentKind(SyntaxKind.ExpressionStatement) && !invocationExpression.SpanOrTrailingTriviaContainsDirectives()) { StatementSyntax expressionStatement = (ExpressionStatementSyntax)invocationExpression.Parent; StatementContainer container; if (StatementContainer.TryCreate(expressionStatement, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; if (statements.Count > 1) { SemanticModel semanticModel = context.SemanticModel; CancellationToken cancellationToken = context.CancellationToken; INamedTypeSymbol symbol = context.GetTypeByMetadataName(MetadataNames.System_Text_StringBuilder); if (symbol != null && IsFixable(memberInvocation, symbol, semanticModel, cancellationToken)) { ExpressionSyntax expression = GetFirstInvocationInMethodChain(memberInvocation, symbol, semanticModel, cancellationToken).Expression; int i = statements.IndexOf(expressionStatement); if (i == 0 || !IsFixable(statements[i - 1], expression, symbol, semanticModel, cancellationToken)) { int j = i; while (j < statements.Count - 1) { if (!IsFixable(statements[j + 1], expression, symbol, semanticModel, cancellationToken)) { break; } j++; } if (j > i) { context.ReportDiagnostic(DiagnosticDescriptors.UseMethodChaining, expressionStatement); } } } } } } }
public static async Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { StatementContainer container; if (StatementContainer.TryCreate(localDeclaration, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(localDeclaration); ExpressionSyntax value = localDeclaration .Declaration .Variables .First() .Initializer .Value; StatementSyntax nextStatement = statements[index + 1]; StatementSyntax newStatement = GetStatementWithReplacedValue(nextStatement, value); SyntaxTriviaList leadingTrivia = localDeclaration.GetLeadingTrivia(); IEnumerable <SyntaxTrivia> trivia = container .Node .DescendantTrivia(TextSpan.FromBounds(localDeclaration.SpanStart, nextStatement.Span.Start)); if (!trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia())) { newStatement = newStatement.WithLeadingTrivia(leadingTrivia.Concat(trivia)); } else { newStatement = newStatement.WithLeadingTrivia(leadingTrivia); } SyntaxList <StatementSyntax> newStatements = statements .Replace(nextStatement, newStatement) .RemoveAt(index); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); } Debug.Assert(false, ""); return(document); }
private static async Task <Document> RefactorAsync( Document document, ExpressionSyntax expression, StatementSyntax statement, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); if (EmbeddedStatementHelper.IsEmbeddedStatement(statement)) { return(await document.ReplaceNodeAsync(statement, Block(statement, CreateNullCheck(expression)), cancellationToken).ConfigureAwait(false)); } else { StatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int statementIndex = statements.IndexOf(statement); ISymbol symbol = (statement.IsKind(SyntaxKind.LocalDeclarationStatement)) ? semanticModel.GetDeclaredSymbol(((LocalDeclarationStatementSyntax)statement).Declaration.Variables.First(), cancellationToken) : semanticModel.GetSymbol(expression, cancellationToken); int lastStatementIndex = IncludeAllReferencesOfSymbol(symbol, expression.Kind(), statements, statementIndex + 1, semanticModel, cancellationToken); if (lastStatementIndex != -1) { if (lastStatementIndex < statements.Count - 1) { lastStatementIndex = IncludeAllReferencesOfVariablesDeclared(statements, statementIndex + 1, lastStatementIndex, semanticModel, cancellationToken); } return(await RefactorAsync( document, expression, statements, container, statementIndex, lastStatementIndex, cancellationToken).ConfigureAwait(false)); } } } return(await document.InsertNodeAfterAsync(statement, CreateNullCheck(expression), cancellationToken).ConfigureAwait(false)); }
private StatementContainer GetStatementContainer(BinaryExpressionSyntax binaryExpression) { SyntaxNode node = binaryExpression.Parent.Parent; if (node != null) { StatementContainer container; if (StatementContainer.TryCreate(node, out container)) { return(container); } } return(null); }
private static StatementContainer GetStatementContainer(SyntaxNode node) { SyntaxNode parent = node.Parent; if (parent != null) { StatementContainer container; if (StatementContainer.TryCreate(parent, out container)) { return(container); } } return(null); }
public async Task <Document> RefactorAsync( Document document, StatementContainer container, BinaryExpressionSyntax condition, ExpressionSyntax expression, CancellationToken cancellationToken = default(CancellationToken)) { var ifStatement = (IfStatementSyntax)condition.Parent; IfStatementSyntax newIfStatement = RemoveExpressionFromCondition(ifStatement, condition, expression) .WithFormatterAnnotation(); SyntaxNode newNode = AddNextIf(container, ifStatement, newIfStatement, expression); return(await document.ReplaceNodeAsync(container.Node, newNode, cancellationToken).ConfigureAwait(false)); }
public static async Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { ExpressionSyntax returnExpression = ifStatement.Condition; if (GetBooleanLiteral(ifStatement.Statement).Kind() == SyntaxKind.FalseLiteralExpression) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); returnExpression = CSharpUtility.LogicallyNegate(returnExpression, semanticModel, cancellationToken); } ReturnStatementSyntax newReturnStatement = ReturnStatement( ReturnKeyword().WithTrailingTrivia(Space), returnExpression, SemicolonToken()); if (ifStatement.Else != null) { newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(await document.ReplaceNodeAsync(ifStatement, newReturnStatement, cancellationToken).ConfigureAwait(false)); } StatementContainer container = StatementContainer.Create(ifStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(ifStatement); var returnStatement = (ReturnStatementSyntax)statements[index + 1]; newReturnStatement = newReturnStatement .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newReturnStatement); //TODO: ReplaceStatementsAsync return(await document.ReplaceNodeAsync(container.Node, container.WithStatements(newStatements).Node, cancellationToken).ConfigureAwait(false)); }
private static bool NullCheckExists(ExpressionSyntax expression, StatementSyntax statement) { if (!EmbeddedStatement.IsEmbeddedStatement(statement)) { StatementContainer container; if (StatementContainer.TryCreate(statement, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(statement); if (index < statements.Count - 1) { StatementSyntax nextStatement = statements[index + 1]; if (nextStatement.IsKind(SyntaxKind.IfStatement)) { var ifStatement = (IfStatementSyntax)nextStatement; ExpressionSyntax condition = ifStatement.Condition; if (condition?.IsKind(SyntaxKind.NotEqualsExpression) == true) { var notEqualsExpression = (BinaryExpressionSyntax)condition; ExpressionSyntax left = notEqualsExpression.Left; if (left?.IsEquivalentTo(expression, topLevel: false) == true) { ExpressionSyntax right = notEqualsExpression.Right; if (right?.IsKind(SyntaxKind.NullLiteralExpression) == true) { return(true); } } } } } } } return(false); }
public override Task <Document> RefactorAsync( Document document, CancellationToken cancellationToken = default(CancellationToken)) { IStatementContainer container = StatementContainer.Create(IfStatement); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(IfStatement); TStatement newStatement = CreateNewStatement(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index - 1, newStatement); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
public Task <Document> RefactorAsync( Document document, StatementContainer container, BinaryExpressionSyntax condition, BinaryExpressionSelection binaryExpressionSelection, CancellationToken cancellationToken) { var ifStatement = (IfStatementSyntax)condition.Parent; IfStatementSyntax newIfStatement = RemoveExpressionsFromCondition(ifStatement, condition, binaryExpressionSelection) .WithFormatterAnnotation(); ExpressionSyntax expression = SyntaxFactory.ParseExpression(binaryExpressionSelection.ToString()); SyntaxNode newNode = AddNextIf(container, ifStatement, newIfStatement, expression); return(document.ReplaceNodeAsync(container.Node, newNode, cancellationToken)); }
private static async Task <Document> RefactorAsync( Document document, StatementContainer statementContainer, StatementSyntax statement, InitializerExpressionSyntax initializer, ExpressionSyntax expression, CancellationToken cancellationToken) { ExpressionStatementSyntax[] expressions = Refactor(initializer, expression).ToArray(); expressions[expressions.Length - 1] = expressions[expressions.Length - 1] .WithTrailingTrivia(statement.GetTrailingTrivia()); SyntaxList <StatementSyntax> statements = statementContainer.Statements; int index = statements.IndexOf(statement); StatementSyntax newStatement = statement.RemoveNode(initializer, SyntaxRemoveOptions.KeepNoTrivia); SyntaxKind statementKind = statement.Kind(); if (statementKind == SyntaxKind.ExpressionStatement) { var expressionStatement = (ExpressionStatementSyntax)newStatement; newStatement = expressionStatement .WithExpression(expressionStatement.Expression.WithoutTrailingTrivia()); } else if (statementKind == SyntaxKind.LocalDeclarationStatement) { var localDeclaration = (LocalDeclarationStatementSyntax)newStatement; newStatement = localDeclaration .WithDeclaration(localDeclaration.Declaration.WithoutTrailingTrivia()); } SyntaxList <StatementSyntax> newStatements = statements.Replace(statement, newStatement); SyntaxNode newNode = statementContainer .NodeWithStatements(newStatements.InsertRange(index + 1, expressions)) .WithFormatterAnnotation(); return(await document.ReplaceNodeAsync(statementContainer.Node, newNode, cancellationToken).ConfigureAwait(false)); }