public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var catchClause = context.GetNode <CatchClause>(); if (catchClause == null) { yield break; } if (!catchClause.Type.IsNull) { yield break; } yield return(new CodeAction(context.TranslateString("Add type specifier"), script => { var newType = context.CreateShortType("System", "Exception"); var namingHelper = new NamingHelper(context); var newIdentifier = Identifier.Create(namingHelper.GenerateVariableName(newType, "e")); script.Replace(catchClause, new CatchClause { Type = newType, VariableNameToken = newIdentifier, Body = catchClause.Body.Clone() as BlockStatement }); script.Select(newType); }, catchClause)); }
static ForeachStatement MakeForeach(Expression node, IType type, RefactoringContext context) { var namingHelper = new NamingHelper(context); return(new ForeachStatement { VariableType = new SimpleType("var"), VariableName = namingHelper.GenerateVariableName(type), InExpression = node.Clone(), EmbeddedStatement = new BlockStatement() }); }
public override Expression VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, Expression data) { var creationType = objectCreateExpression.Type.Clone(); var parameters = objectCreateExpression.Arguments.Select(arg => arg.Clone()); var newCreation = new ObjectCreateExpression(creationType, parameters); if (objectCreateExpression.Initializer.IsNull) { return(newCreation); } else { AstType declarationType = GetDeclarationType(objectCreateExpression.Type); var name = namingHelper.GenerateVariableName(objectCreateExpression.Type); var variableInitializer = new VariableDeclarationStatement(declarationType, name, newCreation); statements.Add(variableInitializer); var identifier = new IdentifierExpression(name); base.VisitObjectCreateExpression(objectCreateExpression, identifier); return(identifier); } }