public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) { var field = RemoveBackingStoreAction.GetBackingField(ctx, propertyDeclaration); if (field == null) { return; } AddIssue(new CodeIssue( propertyDeclaration.NameToken, ctx.TranslateString("Convert to auto property") ) { ActionProvider = { typeof(RemoveBackingStoreAction) } } ); }
public IEnumerable <CodeAction> GetActions(RefactoringContext context) { var property = context.GetNode <PropertyDeclaration>(); var field = RemoveBackingStoreAction.GetBackingField(context); if (field == null) { yield break; } var resolvedType = ReflectionHelper.ParseReflectionName("System.EventHandler").Resolve(context.Compilation); if (resolvedType == null) { yield break; } var type = (TypeDeclaration)property.Parent; yield return(new CodeAction(context.TranslateString("Create changed event"), script => { var eventDeclaration = CreateChangedEventDeclaration(context, property); var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator(context, type, eventDeclaration, eventDeclaration.Variables.First(), resolvedType.GetDelegateInvokeMethod(), false); var stmt = new ExpressionStatement(new InvocationExpression( new IdentifierExpression(methodDeclaration.Name), new MemberReferenceExpression(new TypeReferenceExpression(context.CreateShortType("System", "EventArgs")), "Empty") )); var task = script.InsertWithCursor( context.TranslateString("Create event invocator"), Script.InsertPosition.After, new AstNode[] { eventDeclaration, methodDeclaration } ); Action <Task> insertInvocation = delegate { script.InsertBefore(property.Setter.Body.RBraceToken, stmt); script.FormatText(stmt); }; if (task.IsCompleted) { insertInvocation(null); } else { task.ContinueWith(insertInvocation, TaskScheduler.FromCurrentSynchronizationContext()); } }, property.NameToken)); }
public override IEnumerable <CodeAction> GetActions(RefactoringContext context) { var property = context.GetNode <PropertyDeclaration>(); if (property == null || !property.NameToken.Contains(context.Location)) { yield break; } var field = RemoveBackingStoreAction.GetBackingField(context, property); if (field == null) { yield break; } var resolvedType = ReflectionHelper.ParseReflectionName("System.EventHandler").Resolve(context.Compilation); if (resolvedType == null) { yield break; } var type = (TypeDeclaration)property.Parent; yield return(new CodeAction(context.TranslateString("Create changed event"), script => { var eventDeclaration = CreateChangedEventDeclaration(context, property); var methodDeclaration = CreateEventInvocatorAction.CreateEventInvocator(context, type, eventDeclaration, eventDeclaration.Variables.First(), resolvedType.GetDelegateInvokeMethod(), false); var stmt = new ExpressionStatement(new InvocationExpression( new IdentifierExpression(methodDeclaration.Name), context.CreateShortType("System", "EventArgs").Member("Empty") )); script.InsertWithCursor( context.TranslateString("Create event invocator"), Script.InsertPosition.After, new AstNode[] { eventDeclaration, methodDeclaration } ).ContinueScript(delegate { script.InsertBefore(property.Setter.Body.RBraceToken, stmt); script.FormatText(stmt); }); }, property.NameToken)); }
static Statement BuildAccessorStatement(RefactoringContext context, PropertyDeclaration pdecl) { if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) { var field = RemoveBackingStoreAction.ScanGetter(context, pdecl); if (field != null) { return(new ExpressionStatement(new AssignmentExpression(new IdentifierExpression(field.Name), AssignmentOperatorType.Assign, new IdentifierExpression("value")))); } } if (!pdecl.Setter.IsNull && pdecl.Getter.IsNull) { var field = RemoveBackingStoreAction.ScanSetter(context, pdecl); if (field != null) { return(new ReturnStatement(new IdentifierExpression(field.Name))); } } return(new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))); }