public CodeAction GetActionForVariableInitializer(RefactoringContext context) { var variableInitializer = context.GetNode <VariableInitializer>(); if (variableInitializer == null) { return(null); } var declaration = variableInitializer.Parent as VariableDeclarationStatement; if (declaration == null) { return(null); } if (variableInitializer.Initializer.IsNull) { return(null); } var objectCreateExpression = variableInitializer.Initializer as ObjectCreateExpression; if (objectCreateExpression == null) { return(null); } var converter = new InitializerConversionVisitor(context); Expression finalExpression; var statements = converter.ConvertInitializer(objectCreateExpression, out finalExpression); if (statements.Count > 0) { return(new CodeAction(context.TranslateString("Convert to explicit initializers"), script => { foreach (var statement in statements) { script.InsertBefore(declaration, statement); } script.Replace(variableInitializer.Initializer, finalExpression); })); } return(null); }
public CodeAction GetActionForAssignmentExpression(RefactoringContext context) { var assignmentExpression = context.GetNode <AssignmentExpression>(); if (assignmentExpression == null) { return(null); } var expressionStatement = assignmentExpression.Parent as ExpressionStatement; if (expressionStatement == null) { return(null); } var objectCreateExpression = assignmentExpression.Right as ObjectCreateExpression; if (objectCreateExpression == null) { return(null); } var converter = new InitializerConversionVisitor(context); Expression finalExpression; var statements = converter.ConvertInitializer(objectCreateExpression, out finalExpression); if (statements.Count > 0) { return(new CodeAction(context.TranslateString("Convert to explicit initializers"), script => { foreach (var statement in statements) { script.InsertBefore(expressionStatement, statement); } script.Replace(assignmentExpression.Right, finalExpression); })); } return(null); }