private static void ReplaceExpression(SemanticModel model, SyntaxNode node, DocumentEditor editor, ClassDeclarationSyntax oldClass, string fieldName, ExpressionSyntax expressionSyntax) { var typeInfo = model.GetTypeInfo(node); var type = typeInfo.Type?.Name ?? Constants.IDisposable; editor.AddUninitializedFieldNamed(oldClass, fieldName, type); var assignment = SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(fieldName), expressionSyntax ) ); editor.ReplaceNode(node.Parent, assignment); }
private static void ReplaceArgument(SemanticModel model, ArgumentSyntax argumentSyntax, DocumentEditor editor, ClassDeclarationSyntax oldClass, string fieldName, SyntaxNode node) { var typeInfo = model.GetTypeInfo(argumentSyntax.Expression); var type = typeInfo.Type?.Name ?? Constants.IDisposable; editor.AddUninitializedFieldNamed(oldClass, fieldName, type); if (!node.TryFindContainingBlock(out var block)) { return; } var assignmentExpression = SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(fieldName), argumentSyntax.Expression ) ); var precedingStatements = block.Statements.TakeWhile(ss => ss.DescendantNodes <ArgumentSyntax>().All(@as => @as != argumentSyntax)); var variable = SyntaxFactory.Argument(SyntaxFactory.IdentifierName(fieldName)); var currentStatement = block.Statements .SkipWhile(ss => ss.DescendantNodes <ArgumentSyntax>().All(@as => @as != argumentSyntax)) .FirstOrDefault() .ReplaceNode(node, variable); var trailingStatements = block.Statements .SkipWhile(ss => ss.DescendantNodes <ArgumentSyntax>().All(@as => @as != argumentSyntax)) .Skip(1); var newBlock = SyntaxFactory.Block(precedingStatements .Concat(assignmentExpression) .Concat(currentStatement) .Concat(trailingStatements)); editor.ReplaceNode(block, newBlock); }