public override void Execute(SolutionModel solutionModel, SelectionContext context) { FileModel fileModel; CodeSpan selection; if (!solutionModel.IsEditorSelection(context, out fileModel, out selection)) { return; } propertiesSelectedByUser = false; IIdentifier identifier = fileModel.InnerMost <IIdentifier>(selection); IClassDeclaration classDeclaration = identifier.ParentConstruct.As <IClassDeclaration>(); if (classDeclaration.ExistsTextuallyInFile && classDeclaration.IsInUserCode() && !classDeclaration.IsPrivate()) { IConstructEnumerable <IMemberDeclaration> propertiesForWrapping = GetPropertiesForWrapping(classDeclaration); if (!propertiesForWrapping.Any()) { CreateViewModelWithoutUserSelectedProperties(classDeclaration).NavigateTo(); } else { ConfirmOccurencesDialog confirmOccurencesDialog = fileModel.UIProcess.Get <ConfirmOccurencesDialog>(); confirmOccurencesDialog.ShowIfOccurencesToConfirmIn("Select properties for wrapping", "Select which properties to be wrapped", () => ConfirmPropertyDeclarationsToBeWrappedAndContinueWrapping(classDeclaration, propertiesForWrapping)); } } }
public override bool CanExecute(SolutionModel solutionModel, SelectionContext context) { FileModel fileModel; CodeSpan selection; if (!solutionModel.IsEditorSelection(context, out fileModel, out selection)) { return(false); } IConstructEnumerable <IFieldDeclaration> fields = FindFields(fileModel, selection); return(fields.Exist() && ImplementsINotifyPropertyChanged(fields.First().EnclosingClass)); }
private List <IPropertyDeclaration> GetPropertiesForWrappingByName(IClassDeclaration classDeclaration, List <string> confirmedPropertiesNamesForWrapping) { List <IPropertyDeclaration> properties = new List <IPropertyDeclaration>(); IConstructEnumerable <IDeclaration> containedDeclarations = classDeclaration.ContainedDeclarations; foreach (string propertyName in confirmedPropertiesNamesForWrapping) { IPropertyDeclaration property = containedDeclarations.First(d => string.Equals(propertyName, d.Identifier.Name, StringComparison.Ordinal)) .As <IPropertyDeclaration>(); if (property.Exists) { properties.Add(property); } } return(properties); }
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); } } }
private void ConfirmPropertyDeclarationsToBeWrappedAndContinueWrapping(IClassDeclaration classDeclaration, IConstructEnumerable <IMemberDeclaration> propertiesForWrapping) { List <string> confirmedPropertiesNamesForWrapping = new List <string>(); foreach (IMemberDeclaration propertyDeclaration in propertiesForWrapping) { if (classDeclaration.FileModel.UIProcess.Get <ConfirmOccurencesDialog>().Confirm(propertyDeclaration, true)) { confirmedPropertiesNamesForWrapping.Add(propertyDeclaration.Identifier.Name); } } if (propertiesSelectedByUser) { string classDeclarationFullName = classDeclaration.FullName; IClassDeclaration viewModel = CreateViewModelWithoutUserSelectedProperties(classDeclaration); viewModel = RebuildSolutionModel(classDeclaration, viewModel.Identifier.Name); IFieldDeclaration wrappedField = viewModel.ContainedDeclarations.First(d => d.Is <IFieldDeclaration>()).As <IFieldDeclaration>(); IMethodDeclaration onPropertyChangedMethod = viewModel.ContainedDeclarations.First(d => d.Is <IMethodDeclaration>()).As <IMethodDeclaration>(); classDeclaration = viewModel.FileModel.All <IClassDeclaration>().First(c => string.Equals(c.FullName, classDeclarationFullName, System.StringComparison.Ordinal)); List <IPropertyDeclaration> confirmedPropertiesForWrapping = GetPropertiesForWrappingByName(classDeclaration, confirmedPropertiesNamesForWrapping); InsertWrappedProperties(viewModel, confirmedPropertiesForWrapping, wrappedField, onPropertyChangedMethod.Identifier); viewModel.NavigateTo(); } else { propertiesSelectedByUser = true; } }
private void ConfirmPropertyDeclarationsToBeWrappedAndContinueWrapping(IClassDeclaration classDeclaration, IConstructEnumerable<IMemberDeclaration> propertiesForWrapping) { List<string> confirmedPropertiesNamesForWrapping = new List<string>(); foreach (IMemberDeclaration propertyDeclaration in propertiesForWrapping) { if (classDeclaration.FileModel.UIProcess.Get<ConfirmOccurencesDialog>().Confirm(propertyDeclaration, true)) { confirmedPropertiesNamesForWrapping.Add(propertyDeclaration.Identifier.Name); } } if (propertiesSelectedByUser) { string classDeclarationFullName = classDeclaration.FullName; IClassDeclaration viewModel = CreateViewModelWithoutUserSelectedProperties(classDeclaration); viewModel = RebuildSolutionModel(classDeclaration, viewModel.Identifier.Name); IFieldDeclaration wrappedField = viewModel.ContainedDeclarations.First(d => d.Is<IFieldDeclaration>()).As<IFieldDeclaration>(); IMethodDeclaration onPropertyChangedMethod = viewModel.ContainedDeclarations.First(d => d.Is<IMethodDeclaration>()).As<IMethodDeclaration>(); classDeclaration = viewModel.FileModel.All<IClassDeclaration>().First(c => string.Equals(c.FullName, classDeclarationFullName, System.StringComparison.Ordinal)); List<IPropertyDeclaration> confirmedPropertiesForWrapping = GetPropertiesForWrappingByName(classDeclaration, confirmedPropertiesNamesForWrapping); InsertWrappedProperties(viewModel, confirmedPropertiesForWrapping, wrappedField, onPropertyChangedMethod.Identifier); viewModel.NavigateTo(); } else { propertiesSelectedByUser = true; } }