private IAccessor CreateSetter(IConstructLanguage language, string propertyName, IFieldDeclaration field) { string propertyChangedName = language.Name == LanguageNames.CSharp ? "OnPropertyChanged" : "RaisePropertyChangedEvent"; IMethodInvocation onPropertyChanged = language.MethodInvocation(language.None <IExpression>(), language.Identifier(propertyChangedName), language.None <ITypeArguments>(), language.Arguments( language.Argument(language.StringLiteral(propertyName)))); IMemberAccess fieldUsage = language.MemberAccess(language.None <IExpression>(), field.Identifier); IExpression valueUsage = language.Expression("value"); IStatement assignment = language.AssignmentStatement(fieldUsage, valueUsage); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression(fieldUsage, Operator.NotEqual, valueUsage), language.Block(assignment, language.ExpressionStatement(onPropertyChanged))); IAccessor setter = language.Setter(language.Block(ifStatement)); return(setter); }
private IConstructorDeclaration CreateConstructor(IClassDeclaration classDeclaration, IIdentifier fieldIdentifier, ITypeName typeName) { IConstructLanguage language = classDeclaration.Language; IParameter parameter = language.Parameter(typeName); NamingPolicy parameterNamingPolicy = parameter.PrimaryNamingPolicy(classDeclaration.FileModel.UserSettings); string parameterName = parameterNamingPolicy.ChangeNameAccordingToPolicy(classDeclaration.Identifier.Name, classDeclaration.SolutionModel); parameter.Identifier = language.Identifier(parameterName); Modifiers constructorModifiers = Modifiers.Public; Modifiers visibility = classDeclaration.Modifiers.Modifiers.GetVisibility(); if (visibility == Modifiers.None || visibility.IsInternal()) { constructorModifiers = Modifiers.Internal; } IConstructorDeclaration constructor = language.Constructor(language.None <IDocComment>(), language.None <IAttributes>(), language.Modifiers(constructorModifiers), language.Parameters(parameter), language.None <IConstructorInitializer>(), language.Block( language.AssignmentStatement( language.MemberAccess(language.This(), fieldIdentifier), language.VariableAccess(parameter.Identifier)))); return(constructor); }
private static IMethodDeclaration CreateVBInvocator(IEventDeclaration eventDeclaration, IModifiers modifiers, IList <IArgument> arguments, IList <IParameter> parameters, IConstructLanguage language) { IEventInvocation eventInvocation = language.New <IEventInvocation>(); eventInvocation.DelegateInvocation = language.DelegateInvocation( language.VariableAccess(eventDeclaration.Identifier), language.Arguments(arguments)); IMethodDeclaration method = language.Method( language.None <IDocComment>(), language.None <IAttributes>(), modifiers, language.TypeName(eventDeclaration.VoidTypeAtThisLocation()), language.None <IMethodTypeParameters>(), language.Parameters(parameters), language.Block( language.ExpressionStatement(eventInvocation))); NamingPolicy methodsNamingPolicy = method.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string methodName = methodsNamingPolicy.ChangeNameAccordingToPolicy("Raise" + eventDeclaration.Identifier.Name + "Event", eventDeclaration.SolutionModel); method.Identifier = language.Identifier(methodName); return(method); }
private IClassDeclaration CreateInitialViewModel(IClassDeclaration classDeclaration, IFieldDeclaration fieldDeclaration, IConstructorDeclaration viewModelConstructor) { IConstructLanguage language = classDeclaration.Language; IClassDeclaration viewModel = language.Class( language.Modifiers(Modifiers.Public), language.None <IClassTypeParameters>(), language.None <ITypeName>(), new List <ITypeName>(), new List <IDeclaration>() { fieldDeclaration, viewModelConstructor }); NamingPolicy classesPolicy = viewModel.PrimaryNamingPolicy(classDeclaration.FileModel.UserSettings); string viewModelName = classesPolicy.MakeTypeNameUniqueInNamespace(classDeclaration, classDeclaration.Identifier.Name + "ViewModel"); viewModel.Identifier = language.Identifier(viewModelName); return(viewModel); }
private static IMethodDeclaration CreateCSInvocator(IEventDeclaration eventDeclaration, IModifiers modifiers, IList<IArgument> arguments, IList<IParameter> parameters, IConstructLanguage language) { IVariableDeclaration variable = language.Variable( eventDeclaration.TypeName, language.VariableAccess(eventDeclaration.Identifier)); NamingPolicy variablesNamingPolicy = variable.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string variableName = variablesNamingPolicy.ChangeNameAccordingToPolicy("on" + eventDeclaration.Identifier.Name, eventDeclaration.SolutionModel); variable.Identifier = language.Identifier(variableName); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression( language.VariableAccess(variable.Identifier), Operator.NotEqual, language.New<INull>()), language.Block( language.ExpressionStatement( language.DelegateInvocation( language.VariableAccess(variable.Identifier), language.Arguments(arguments))))); IMethodDeclaration method = language.Method( language.None<IDocComment>(), language.None<IAttributes>(), modifiers, language.TypeName(eventDeclaration.VoidTypeAtThisLocation()), language.None<IMethodTypeParameters>(), language.Parameters(parameters), language.Block( variable, ifStatement)); NamingPolicy methodsNamingPolicy = method.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string methodName = methodsNamingPolicy.ChangeNameAccordingToPolicy("on" + eventDeclaration.Identifier.ToUpperFirstLetter().Name, eventDeclaration.SolutionModel); method.Identifier = language.Identifier(methodName); return method; }
private static IMethodDeclaration CreateCSInvocator(IEventDeclaration eventDeclaration, IModifiers modifiers, IList <IArgument> arguments, IList <IParameter> parameters, IConstructLanguage language) { IVariableDeclaration variable = language.Variable( eventDeclaration.TypeName, language.VariableAccess(eventDeclaration.Identifier)); NamingPolicy variablesNamingPolicy = variable.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string variableName = variablesNamingPolicy.ChangeNameAccordingToPolicy("on" + eventDeclaration.Identifier.Name, eventDeclaration.SolutionModel); variable.Identifier = language.Identifier(variableName); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression( language.VariableAccess(variable.Identifier), Operator.NotEqual, language.New <INull>()), language.Block( language.ExpressionStatement( language.DelegateInvocation( language.VariableAccess(variable.Identifier), language.Arguments(arguments))))); IMethodDeclaration method = language.Method( language.None <IDocComment>(), language.None <IAttributes>(), modifiers, language.TypeName(eventDeclaration.VoidTypeAtThisLocation()), language.None <IMethodTypeParameters>(), language.Parameters(parameters), language.Block( variable, ifStatement)); NamingPolicy methodsNamingPolicy = method.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string methodName = methodsNamingPolicy.ChangeNameAccordingToPolicy("on" + eventDeclaration.Identifier.ToUpperFirstLetter().Name, eventDeclaration.SolutionModel); method.Identifier = language.Identifier(methodName); return(method); }
public override void Execute(SolutionModel solutionModel, SelectionContext context) { FileModel fileModel; CodeSpan selection; if (!solutionModel.IsEditorSelection(context, out fileModel, out selection)) { return; } IConstructEnumerable <IFieldDeclaration> fields = FindFields(fileModel, selection); if (fields.Exist()) { IConstructLanguage language = fields.Language; foreach (IFieldDeclaration field in fields) { IPropertyDeclaration property = language.Property( language.None <IDocComment>(), language.None <IAttributes>(), language.Modifiers(Modifiers.Public), language.TypeName(field.TypeName.Type), language.None <IIdentifier>()); NamingPolicy propertyNamingPolicy = property.PrimaryNamingPolicy(fileModel.UserSettings); string propertyName = propertyNamingPolicy.MakeMemberNameUniqueInScope(field, field.Identifier.Name); property.Identifier = language.Identifier(propertyName); IAccessor getter = language.FieldGetter(field.Identifier); IAccessor setter = CreateSetter(language, propertyName, field); property.Accessors = language.Enumerable(new List <IAccessor>() { getter, setter }); field.EnclosingClass.Insert(property); } } }
public override void Execute(SolutionModel solutionModel, SelectionContext context) { FileModel fileModel; CodeSpan selection; if (!solutionModel.IsEditorSelection(context, out fileModel, out selection)) { return; } IPropertyDeclaration propertyDeclaration = fileModel.InnerMost <IPropertyDeclaration>(selection); if (IsFieldBackedPropertyWithSetterInsideClass(propertyDeclaration) && EnclosingClassImplementsINotifyPropertyChanged(propertyDeclaration)) { IConstructLanguage language = propertyDeclaration.Language; string methodInvocationName = language.Name == LanguageNames.CSharp ? "OnPropertyChanged" : "RaisePropertyChangedEvent"; IMethodInvocation methodInvocation = language.MethodInvocation( language.None <IExpression>(), language.Identifier(methodInvocationName), language.None <ITypeArguments>(), language.Arguments( language.Argument(language.StringLiteral(propertyDeclaration.Identifier.Name)))); IAccessor setter = propertyDeclaration.Setter(); List <IStatement> ifBlockStatements = new List <IStatement>(setter.Block.ChildStatements); ifBlockStatements.Add(language.ExpressionStatement(methodInvocation)); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression( language.MemberAccess(language.None <IExpression>(), propertyDeclaration.BackingField().Identifier), Operator.NotEqual, language.Expression("value")), language.Block(ifBlockStatements)); IBlock newBlock = language.Block(ifStatement); setter.Block = newBlock; } }
private IClassDeclaration ImplementINotifyPropertyChangedInterface(IClassDeclaration viewModel, IClassDeclaration wrappedClass) { UsingDirectiveHelper.AddUsingDirectiveIfNeeded(wrappedClass, "System", "ComponentModel"); IConstructLanguage language = viewModel.Language; IIdentifier identifier = language.Identifier(PropertyChangedSimpleTypeName); ITypeName typeName = language.SimpleTypeName(identifier, language.None <ITypeArguments>()); viewModel.IntroduceInterface(typeName); wrappedClass.Append(viewModel); string name = viewModel.Identifier.Name; viewModel = RebuildSolutionModel(wrappedClass, name); AddStubsHelper.CreateAllUnimplementedMembers(viewModel, new AddStubsOptions()); return(RebuildSolutionModel(wrappedClass, name)); }
private void InsertWrappedProperties(IClassDeclaration viewModel, List <IPropertyDeclaration> confirmedPropertiesForWrapping, IFieldDeclaration wrappedClassField, IIdentifier onPropertyChangedIdentifier) { IConstructLanguage language = viewModel.Language; foreach (IPropertyDeclaration property in confirmedPropertiesForWrapping) { IMemberAccess propertyMemberAccess = language.MemberAccess( language.MemberAccess(language.None <IExpression>(), wrappedClassField.Identifier), property.Identifier); IAccessor getterOfWrapper = language.None <IAccessor>(); IAccessor propertyGetter = property.Getter(); if (propertyGetter.Exists && IsAccessorVisibleOutsideClass(propertyGetter.Modifiers)) { getterOfWrapper = language.Getter( language.Modifiers(propertyGetter.Modifiers.Modifiers), language.Block( language.ReturnStatement(propertyMemberAccess))); } IAccessor setterOfWrapper = language.None <IAccessor>(); IAccessor propertySetter = property.Setter(); if (propertySetter.Exists && IsAccessorVisibleOutsideClass(propertySetter.Modifiers)) { IStatement assignment = language.AssignmentStatement(propertyMemberAccess, language.Expression("value")); IMethodInvocation onPropertyChangedInvocation = language.MethodInvocation( language.None <IExpression>(), onPropertyChangedIdentifier, language.None <ITypeArguments>(), language.Arguments( language.Argument(language.StringLiteral(property.Identifier.Name)))); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression(propertyMemberAccess, Operator.NotEqual, language.Expression("value")), language.Block(assignment, language.ExpressionStatement(onPropertyChangedInvocation))); setterOfWrapper = language.Setter( language.Modifiers(propertyGetter.Modifiers.Modifiers), language.Block(ifStatement)); } if (getterOfWrapper.Exists || setterOfWrapper.Exists) { IPropertyDeclaration wrapperProperty = language.Property( language.None <IDocComment>(), language.None <IAttributes>(), property.Modifiers, property.TypeName, property.Identifier, getterOfWrapper, setterOfWrapper); viewModel.Insert(wrapperProperty); } } }
private IAccessor CreateSetter(IConstructLanguage language, string propertyName, IFieldDeclaration field) { string propertyChangedName = language.Name == LanguageNames.CSharp ? "OnPropertyChanged" : "RaisePropertyChangedEvent"; IMethodInvocation onPropertyChanged = language.MethodInvocation(language.None<IExpression>(), language.Identifier(propertyChangedName), language.None<ITypeArguments>(), language.Arguments( language.Argument(language.StringLiteral(propertyName)))); IMemberAccess fieldUsage = language.MemberAccess(language.None<IExpression>(), field.Identifier); IExpression valueUsage = language.Expression("value"); IStatement assignment = language.AssignmentStatement(fieldUsage, valueUsage); IIfStatement ifStatement = language.IfStatement( language.BinaryExpression(fieldUsage, Operator.NotEqual, valueUsage), language.Block(assignment, language.ExpressionStatement(onPropertyChanged))); IAccessor setter = language.Setter(language.Block(ifStatement)); return setter; }
private static IMethodDeclaration CreateVBInvocator(IEventDeclaration eventDeclaration, IModifiers modifiers, IList<IArgument> arguments, IList<IParameter> parameters, IConstructLanguage language) { IEventInvocation eventInvocation = language.New<IEventInvocation>(); eventInvocation.DelegateInvocation = language.DelegateInvocation( language.VariableAccess(eventDeclaration.Identifier), language.Arguments(arguments)); IMethodDeclaration method = language.Method( language.None<IDocComment>(), language.None<IAttributes>(), modifiers, language.TypeName(eventDeclaration.VoidTypeAtThisLocation()), language.None<IMethodTypeParameters>(), language.Parameters(parameters), language.Block( language.ExpressionStatement(eventInvocation))); NamingPolicy methodsNamingPolicy = method.PrimaryNamingPolicy(eventDeclaration.FileModel.UserSettings); string methodName = methodsNamingPolicy.ChangeNameAccordingToPolicy("Raise" + eventDeclaration.Identifier.Name + "Event", eventDeclaration.SolutionModel); method.Identifier = language.Identifier(methodName); return method; }