private async Task <Document> DivideDeclarationsAsync(Document document, VariableDeclarationSyntax declaration, CancellationToken cancellationToken) { var newDeclarations = new SyntaxList <VariableDeclarationSyntax>(); foreach (var variable in declaration.Variables) { var newDeclaration = SyntaxFactory.VariableDeclaration(declaration.Type); var firstToken = variable.GetFirstToken(); var newVariable = variable.ReplaceToken( firstToken, firstToken.WithoutTrivia()); newDeclaration = newDeclaration.AddVariables(newVariable); newDeclarations = newDeclarations.Add(newDeclaration); } var originalField = declaration.Parent as FieldDeclarationSyntax; var modifiers = originalField.Modifiers; var leadingTrivia = originalField.GetFirstToken().LeadingTrivia; var newFields = new SyntaxList <FieldDeclarationSyntax>(); foreach (var variableDeclaration in newDeclarations) { var newField = SyntaxFactory.FieldDeclaration( new SyntaxList <AttributeListSyntax>(), modifiers, variableDeclaration) .WithTrailingTrivia(SyntaxFactory.EndOfLine(Environment.NewLine)); newFields = newFields.Add( newField .WithLeadingTrivia( newDeclarations.IndexOf(variableDeclaration) > 0 ? new SyntaxTriviaList() { SyntaxFactory.EndOfLine(Environment.NewLine), leadingTrivia.Last() } : leadingTrivia) .WithTrailingTrivia( newDeclarations.IndexOf(variableDeclaration) != newDeclarations.IndexOf(newDeclarations.Last()) ? newField.GetTrailingTrivia() .Add(SyntaxFactory.EndOfLine(Environment.NewLine)) : newField.GetTrailingTrivia())); } var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var newRoot = oldRoot.ReplaceNode(originalField, newFields); return(document.WithSyntaxRoot(newRoot)); }
public static Task <Document> MergeAsync( Document document, MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists(); var newLists = new List <AttributeListSyntax>(lists.Count - attributeLists.Length + 1); int index = lists.IndexOf(attributeLists[0]); for (int i = 0; i < index; i++) { newLists.Add(lists[i]); } newLists.Add(AttributeRefactoring.MergeAttributes(attributeLists)); for (int i = index + attributeLists.Length; i < lists.Count; i++) { newLists.Add(lists[i]); } return(document.ReplaceNodeAsync( member, member.SetAttributeLists(newLists.ToSyntaxList()), cancellationToken)); }
private static Task <Document> ConvertWhileStatementToForStatementAsync( Document document, WhileStatementSyntax whileStatement, CancellationToken cancellationToken) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(whileStatement); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(whileStatement); SingleLocalDeclarationStatementInfo localInfo = SyntaxInfo.SingleLocalDeclarationStatementInfo((LocalDeclarationStatementSyntax)statements[index - 1]); var block = (BlockSyntax)whileStatement.Statement; var expressionStatement = (ExpressionStatementSyntax)block.Statements.Last(); var postIncrementExpression = (PostfixUnaryExpressionSyntax)expressionStatement.Expression; BlockSyntax newBlock = block.WithStatements(block.Statements.Remove(expressionStatement)); ForStatementSyntax forStatement = CSharpFactory.ForStatement( declaration: localInfo.Declaration.TrimTrivia(), condition: whileStatement.Condition, incrementor: postIncrementExpression.TrimTrivia(), statement: newBlock); forStatement = forStatement .WithLeadingTrivia(localInfo.Statement.GetLeadingTrivia().AddRange(whileStatement.GetLeadingTrivia().EmptyIfWhitespace())) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements.ReplaceRange(index - 1, 2, new StatementSyntax[] { forStatement }); return(document.ReplaceStatementsAsync(statementsInfo, 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 static int IndexOfMemberToSwap( MemberDeclarationSyntax member, SyntaxList <MemberDeclarationSyntax> members, TextSpan span) { int index = members.IndexOf(member); if (span.End < member.SpanStart) { if (index > 0 && span.Start > members[index - 1].Span.End) { return(index - 1); } } else if (span.End > member.Span.End) { if (index < members.Count - 1 && span.End < members[index + 1].SpanStart) { return(index); } } return(-1); }
private static LocalDeclarationStatementSyntax GetLocalDeclaration(ReturnStatementSyntax returnStatement) { if (returnStatement.IsParentKind(SyntaxKind.Block)) { var block = (BlockSyntax)returnStatement.Parent; SyntaxList <StatementSyntax> statements = block.Statements; if (statements.Count > 1) { int index = statements.IndexOf(returnStatement); if (index > 0) { StatementSyntax statement = statements[index - 1]; if (statement.IsKind(SyntaxKind.LocalDeclarationStatement)) { return((LocalDeclarationStatementSyntax)statement); } } } } return(null); }
public static async Task <Document> MergeAsync( Document document, MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists(); var newLists = new List <AttributeListSyntax>(lists.Count - attributeLists.Length + 1); int index = lists.IndexOf(attributeLists[0]); for (int i = 0; i < index; i++) { newLists.Add(lists[i]); } newLists.Add(AttributeRefactoring.MergeAttributes(attributeLists)); for (int i = index + attributeLists.Length; i < lists.Count; i++) { newLists.Add(lists[i]); } SyntaxNode newRoot = root.ReplaceNode( member, member.SetAttributeLists(newLists.ToSyntaxList())); return(document.WithSyntaxRoot(newRoot)); }
private static Task <Document> IfReturnToReturnWithBooleanExpressionAsync( Document document, IfReturnToReturnWithBooleanExpressionAnalysis analysis, CancellationToken cancellationToken = default) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(analysis.IfStatement); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(analysis.IfStatement); ExpressionSyntax expression = GetBooleanExpression( analysis.IfStatement.Condition, analysis.Expression1, analysis.Expression2, analysis.SemanticModel, cancellationToken); StatementSyntax newStatement = ReturnStatement(expression) .WithLeadingTrivia(analysis.IfStatement.GetLeadingTrivia()) .WithTrailingTrivia(statements[index + 1].GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newStatement); return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); }
public static async Task <Document> SplitAsync( Document document, MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists(); var newLists = new List <AttributeListSyntax>(); int index = lists.IndexOf(attributeLists[0]); for (int i = 0; i < index; i++) { newLists.Add(lists[i]); } newLists.AddRange(attributeLists.SelectMany(f => AttributeRefactoring.SplitAttributes(f))); for (int i = index + attributeLists.Length; i < lists.Count; i++) { newLists.Add(lists[i]); } return(await document.ReplaceNodeAsync( member, member.SetAttributeLists(newLists.ToSyntaxList()), cancellationToken).ConfigureAwait(false)); }
public static Task <Document> RefactorAsync( Document document, SwitchSectionSyntax switchSection, int numberOfAdditionalSectionsToMerge, CancellationToken cancellationToken) { var switchStatement = (SwitchStatementSyntax)switchSection.Parent; SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections; int index = sections.IndexOf(switchSection); IEnumerable <SwitchSectionSyntax> sectionsWithoutStatements = sections .Skip(index) .Take(numberOfAdditionalSectionsToMerge + 1) .Select(section => CreateSectionWithoutStatements(section)); SyntaxList <SwitchSectionSyntax> newSections = sections.Take(index) .Concat(sectionsWithoutStatements) .Concat(sections.Skip(index + numberOfAdditionalSectionsToMerge + 1)) .ToSyntaxList(); SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections); return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, 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)); }
private static MemberDeclarationSyntax GetNextDeclaration(MemberDeclarationSyntax declaration) { MemberDeclarationListInfo info = SyntaxInfo.MemberDeclarationListInfo(declaration.Parent); if (!info.Success) { return(null); } SyntaxList <MemberDeclarationSyntax> members = info.Members; if (members.Count <= 1) { return(null); } int index = members.IndexOf(declaration); if (index == members.Count - 1) { return(null); } return(members[index + 1]); }
private static Task <Document> RefactorAsync( Document document, StatementsInfo statementsInfo, LocalDeclarationStatementSyntax[] localDeclarations, CancellationToken cancellationToken = default(CancellationToken)) { LocalDeclarationStatementSyntax localDeclaration = localDeclarations[0]; SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(localDeclaration); VariableDeclaratorSyntax[] variables = localDeclarations .Skip(1) .Select(f => f.Declaration) .SelectMany(f => f.Variables) .ToArray(); LocalDeclarationStatementSyntax newLocalDeclaration = localDeclaration .AddDeclarationVariables(variables) .WithTrailingTrivia(localDeclarations[localDeclarations.Length - 1].GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements.Replace( localDeclaration, newLocalDeclaration); for (int i = 1; i < localDeclarations.Length; i++) { newStatements = newStatements.RemoveAt(index + 1); } return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); }
private static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, ReturnStatementSyntax returnStatement, IStatementContainer 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 async Task <Document> RefactorAsync( Document document, UsingDirectiveSyntax usingDirective, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); var classSymbol = semanticModel.GetSymbol(usingDirective.Name, cancellationToken) as INamedTypeSymbol; SyntaxNode parent = usingDirective.Parent; Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), ""); SyntaxList <UsingDirectiveSyntax> usings = GetUsings(parent); int index = usings.IndexOf(usingDirective); List <SimpleNameSyntax> names = CollectNames(parent, classSymbol, semanticModel, cancellationToken); SyntaxNode newNode = parent.ReplaceNodes(names, (node, modifiedNode) => { TypeSyntax type = classSymbol.ToMinimalTypeSyntax(semanticModel, node.SpanStart); return(SimpleMemberAccessExpression(type, node).WithTriviaFrom(node)); }); newNode = RemoveUsingDirective(newNode, index); return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false)); }
private static Task <Document> IfReturnToReturnWithConditionalExpressionAsync( Document document, IfReturnToReturnWithConditionalExpressionAnalysis analysis, CancellationToken cancellationToken = default) { IfStatementSyntax ifStatement = analysis.IfStatement; StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(ifStatement); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(ifStatement); ConditionalExpressionSyntax conditionalExpression = CreateConditionalExpression(ifStatement.Condition, analysis.Expression1, analysis.Expression2); StatementSyntax newStatement = ReturnStatement(conditionalExpression) .WithLeadingTrivia(ifStatement.GetLeadingTrivia()) .WithTrailingTrivia(statements[index + 1].GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .ReplaceAt(index, newStatement); return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, StatementListSelection selectedStatements, CancellationToken cancellationToken = default(CancellationToken)) { ImmutableArray <IfStatementSyntax> ifStatements = selectedStatements.Cast <IfStatementSyntax>().ToImmutableArray(); IfStatementSyntax newIfStatement = IfStatement( BinaryExpression(SyntaxKind.LogicalOrExpression, ifStatements.Select(f => f.Condition)), Block(CreateStatements(ifStatements))); SyntaxList <StatementSyntax> statements = selectedStatements.UnderlyingList; 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.ReplaceStatementsAsync(SyntaxInfo.StatementListInfo(selectedStatements), newStatements, cancellationToken)); }
private static async Task <Document> RefactorAsync( Document document, MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists, CancellationToken cancellationToken) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken); SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists(); int index = lists.IndexOf(attributeLists[0]); for (int i = attributeLists.Length - 1; i >= 1; i--) { lists = lists.RemoveAt(index); } AttributeListSyntax list = AttributeRefactoring.MergeAttributes(attributeLists) .WithAdditionalAnnotations(Formatter.Annotation); lists = lists.Replace(lists[index], list); root = root.ReplaceNode(member, member.SetAttributeLists(lists)); return(document.WithSyntaxRoot(root)); }
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)); }
public static async Task <Document> RefactorAsync( Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { var block = (BlockSyntax)localDeclaration.Parent; SyntaxList <StatementSyntax> statements = block.Statements; int index = statements.IndexOf(localDeclaration); var returnStatement = (ReturnStatementSyntax)statements[index + 1]; ReturnStatementSyntax newReturnStatement = returnStatement .WithExpression(localDeclaration.Declaration.Variables[0].Initializer.Value.WithoutTrivia()) .WithLeadingTrivia(localDeclaration.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()) .WithFormatterAnnotation(); SyntaxList <StatementSyntax> newStatements = statements .RemoveAt(index) .RemoveAt(index) .Insert(index, newReturnStatement); BlockSyntax newBlock = block.WithStatements(newStatements); return(await document.ReplaceNodeAsync(block, newBlock, cancellationToken).ConfigureAwait(false)); }
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 static Task <Document> SplitAsync( Document document, MemberDeclarationSyntax member, AttributeListSyntax[] attributeLists, CancellationToken cancellationToken = default) { SyntaxList <AttributeListSyntax> lists = member.GetAttributeLists(); var newLists = new List <AttributeListSyntax>(); int index = lists.IndexOf(attributeLists[0]); for (int i = 0; i < index; i++) { newLists.Add(lists[i]); } newLists.AddRange(attributeLists.SelectMany(f => SyntaxRefactorings.SplitAttributeList(f)).Select(f => f.WithFormatterAnnotation())); for (int i = index + attributeLists.Length; i < lists.Count; i++) { newLists.Add(lists[i]); } return(document.ReplaceNodeAsync( member, member.WithAttributeLists(newLists.ToSyntaxList()), cancellationToken)); }
private static bool NullCheckExists(ExpressionSyntax expression, StatementSyntax statement) { StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(statement); if (!statementsInfo.Success) { return(false); } SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(statement); if (index >= statements.Count - 1) { return(false); } StatementSyntax nextStatement = statements[index + 1]; if (!(nextStatement is IfStatementSyntax ifStatement)) { return(false); } NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, NullCheckStyles.NotEqualsToNull); if (!nullCheck.Success) { return(false); } return(CSharpFactory.AreEquivalent(expression, nullCheck.Expression)); }
private static Task <Document> RefactorAsync( Document document, SwitchSectionSyntax section, SyntaxListSelection <SwitchLabelSyntax> selectedLabels, CancellationToken cancellationToken) { SyntaxList <SwitchLabelSyntax> labels = section.Labels; int lastIndex = selectedLabels.LastIndex; if (selectedLabels.Last() == labels.Last()) { lastIndex--; } var switchStatement = (SwitchStatementSyntax)section.Parent; SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections; int index = sections.IndexOf(section); SyntaxList <SwitchSectionSyntax> newSections = sections .ReplaceAt(index, section.RemoveNodes(labels.Take(lastIndex + 1), SyntaxRemoveOptions.KeepNoTrivia)) .InsertRange(index, CreateSwitchSections(section, selectedLabels, lastIndex)); SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections); return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, SwitchSectionSyntax switchSection, CancellationToken cancellationToken) { var switchStatement = (SwitchStatementSyntax)switchSection.Parent; SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections; SyntaxList <StatementSyntax> statements = MergeSwitchSectionsAnalyzer.GetStatementsOrDefault(switchSection); int index = sections.IndexOf(switchSection); int i = index + 1; while (i < sections.Count - 1 && !sections[i].SpanOrLeadingTriviaContainsDirectives() && AreEquivalent(statements, MergeSwitchSectionsAnalyzer.GetStatementsOrDefault(sections[i + 1]))) { i++; } SyntaxList <SwitchSectionSyntax> newSections = sections .ModifyRange(index, i - index, f => CreateSectionWithoutStatements(f)) .ToSyntaxList(); SwitchStatementSyntax newSwitchStatement = switchStatement.WithSections(newSections); return(document.ReplaceNodeAsync(switchStatement, newSwitchStatement, cancellationToken)); }
public static async Task <Document> RefactorAsync( Document document, ExpressionStatementSyntax statement, ReturnStatementSyntax returnStatement, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var block = (BlockSyntax)statement.Parent; SyntaxList <StatementSyntax> statements = block.Statements; int index = statements.IndexOf(statement); SyntaxList <StatementSyntax> newStatements = statements.RemoveAt(index); var assignmentExpression = (AssignmentExpressionSyntax)statement.Expression; ReturnStatementSyntax newReturnStatement = returnStatement .WithExpression(assignmentExpression.Right) .WithLeadingTrivia(assignmentExpression.GetLeadingTrivia()) .WithTrailingTrivia(returnStatement.GetTrailingTrivia()) .WithFormatterAnnotation(); newStatements = newStatements.Replace(newStatements[index], newReturnStatement); root = root.ReplaceNode(block, block.WithStatements(newStatements)); return(document.WithSyntaxRoot(root)); }
public override SyntaxNode VisitBlock(BlockSyntax node) { node = (BlockSyntax)base.VisitBlock(node); SyntaxList <StatementSyntax> statements = node.Statements; var ifStatement = statements.LastOrDefault(f => !f.IsKind(SyntaxKind.LocalFunctionStatement)) as IfStatementSyntax; if (ifStatement != null && IsFixable(ifStatement) && ((BlockSyntax)ifStatement.Parent).Statements.IsLastStatement(ifStatement, skipLocalFunction: true)) { var block = (BlockSyntax)ifStatement.Statement; ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition); IfStatementSyntax newIfStatement = ifStatement .WithCondition(newCondition) .WithStatement(block.WithStatements(SingletonList(_jumpStatement))) .WithFormatterAnnotation(); int index = statements.IndexOf(ifStatement); SyntaxList <StatementSyntax> newStatements = statements .ReplaceAt(index, newIfStatement) .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation())); node = node.WithStatements(newStatements); } return(node); }
public static Task <Document> RefactorAsync( Document document, AssignmentExpressionSyntax assignmentExpression, CancellationToken cancellationToken) { var statement = (StatementSyntax)assignmentExpression.Parent; StatementsInfo statementsInfo = SyntaxInfo.StatementsInfo(statement); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(statement); statements = statements.RemoveAt(index); var returnStatement = (ReturnStatementSyntax)statement.NextStatementOrDefault(); IEnumerable <SyntaxTrivia> trivia = statementsInfo .Node .DescendantTrivia(TextSpan.FromBounds(statement.SpanStart, returnStatement.SpanStart)) .Where(f => !f.IsWhitespaceOrEndOfLineTrivia()); trivia = statement .GetLeadingTrivia() .Concat(trivia); returnStatement = returnStatement .WithExpression(assignmentExpression.Right.WithTriviaFrom(returnStatement.Expression)) .WithLeadingTrivia(trivia); statements = statements.ReplaceAt(index, returnStatement); return(document.ReplaceStatementsAsync(statementsInfo, statements, cancellationToken)); }
private static SyntaxNode InsertStatement( SyntaxNode node, StatementSyntax statement) { var body = (BlockSyntax)GetBodyOrExpressionBody(node); SyntaxList <StatementSyntax> statements = body.Statements; StatementSyntax lastStatement = statements.LastOrDefault(f => !f.IsKind(SyntaxKind.LocalFunctionStatement, SyntaxKind.ReturnStatement)); int index = (lastStatement != null) ? statements.IndexOf(lastStatement) + 1 : 0; BlockSyntax newBody = body.WithStatements(statements.Insert(index, statement)); if (node.IsKind(SyntaxKind.MethodDeclaration)) { return(((MethodDeclarationSyntax)node).WithBody(newBody)); } else { return(((LocalFunctionStatementSyntax)node).WithBody(newBody)); } }
public static Task <Document> RefactorAsync( Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken) { ReturnStatementSyntax newReturnStatement = CreateReturnStatement(ifStatement); if (ifStatement.Else != null) { newReturnStatement = newReturnStatement.WithTriviaFrom(ifStatement); return(document.ReplaceNodeAsync(ifStatement, newReturnStatement, cancellationToken)); } else { var block = (BlockSyntax)ifStatement.Parent; SyntaxList <StatementSyntax> statements = block.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); return(document.ReplaceNodeAsync(block, block.WithStatements(newStatements), cancellationToken)); } }
static bool IsLastStatement(SyntaxList<StatementSyntax> statements, MultiLineIfBlockSyntax ifBlock) { return statements.IndexOf(ifBlock) + 1 >= statements.Count; }