Skip to content

Commit 3b63eb9

Browse files
committed
Update extract public interface
A private class can have a (public | private) interface extracted. The default option is public. When an implementing class is public the newly created interface must also too be public. As such the private option for the extracted interface is disabled. Need to get the Public.Checked = true on private implementing classes. Once that's done I can merge the following commits together and edit this commit.
1 parent 38d4399 commit 3b63eb9

File tree

7 files changed

+34
-34
lines changed

7 files changed

+34
-34
lines changed

Rubberduck.Core/UI/Converters/ClassInstancingToBooleanConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class ClassInstancingToBooleanConverter : IValueConverter
99
{
1010
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1111
{
12-
return (ClassInstancing)value == ClassInstancing.Private;
12+
return (ClassInstancing)value == ClassInstancing.PublicNotCreatable;
1313
}
1414

1515
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
1616
{
1717
return (bool)value
18-
? ClassInstancing.Private
19-
: ClassInstancing.PublicNotCreatable;
18+
? ClassInstancing.PublicNotCreatable
19+
: ClassInstancing.Private;
2020
}
2121
}
2222
}

Rubberduck.Core/UI/Refactorings/ExtractInterface/ExtractInterfaceView.xaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<UserControl.Resources>
99
<converters:BoolToHiddenVisibilityConverter x:Key="BoolToHiddenVisibility" />
1010
<converters:ClassInstancingToBooleanConverter x:Key="ClassInstancingToBool" />
11+
<converters:InvertBoolValueConverter x:Key="InvertBool" />
1112

1213
<LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0">
1314
<GradientStop Color="#FFD9F4FF" Offset="0"/>
@@ -93,12 +94,12 @@
9394

9495
<GroupBox Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=ExtractInterface_InstancingGroupBox}"
9596
Grid.Row="1" Margin="5" Padding="5">
96-
<StackPanel Orientation="Horizontal">
97-
<RadioButton x:Name="IsPrivateInstancing"
98-
Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=ExtractInterface_PrivateRadioButton}"
99-
IsChecked="{Binding Converter={StaticResource ClassInstancingToBool}, Mode=TwoWay, Path=ClassInstancing}" />
97+
<StackPanel Orientation="Vertical">
98+
<RadioButton Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=ExtractInterface_PrivateRadioButton}"
99+
IsEnabled="{Binding IsPrivateInterfaceEnabled}"
100+
IsChecked="{Binding IsPublicInterfaceChecked, Converter={StaticResource InvertBool}}" />
100101
<RadioButton Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=ExtractInterface_PublicNotCreatableRadioButton}"
101-
IsEnabled="{Binding IsInterfacePublicNotCreateableEnabled}" />
102+
IsChecked="{Binding IsPublicInterfaceChecked, Mode=TwoWay}" />
102103
</StackPanel>
103104
</GroupBox>
104105

Rubberduck.Core/UI/Refactorings/ExtractInterface/ExtractInterfaceViewModel.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Rubberduck.Parsing.Symbols;
88
using Rubberduck.Refactorings.ExtractInterface;
99
using Rubberduck.UI.Command;
10+
using System.Windows.Data;
1011

1112
namespace Rubberduck.UI.Refactorings
1213
{
@@ -61,38 +62,33 @@ public bool IsValidInterfaceName
6162
}
6263
}
6364

64-
public bool IsInterfacePublicNotCreateableEnabled
65+
public bool IsPrivateInterfaceEnabled
6566
{
6667
get
6768
{
68-
try
69-
{
70-
return Convert.ToBoolean(Model.TargetDeclaration.Attributes.ExposedAttribute.Values.First());
71-
}
72-
catch (FormatException)
73-
{
74-
return false;
75-
}
69+
return Model.ImplementingClassInstancing != ClassInstancing.PublicNotCreatable;
7670
}
7771
}
7872

79-
private ClassInstancing classInstancing = ClassInstancing.Private;
80-
public ClassInstancing ClassInstancing
73+
private readonly IValueConverter classInstancingConverter = new Converters.ClassInstancingToBooleanConverter();
74+
75+
private bool isPublicInterfaceChecked = true;
76+
public bool IsPublicInterfaceChecked
8177
{
82-
get => classInstancing;
78+
get => isPublicInterfaceChecked;
8379
set
8480
{
85-
if (value == classInstancing)
81+
if (value == isPublicInterfaceChecked)
8682
{
8783
return;
8884
}
8985

90-
classInstancing = value;
91-
OnPropertyChanged();
86+
Model.ImplementingClassInstancing = (ClassInstancing)classInstancingConverter.ConvertBack(value, null, null, null);
87+
isPublicInterfaceChecked = value;
88+
OnPropertyChanged();
9289
}
9390
}
9491

95-
9692
private void ToggleSelection(bool value)
9793
{
9894
foreach (var item in Members)

Rubberduck.Refactorings/ExtractInterface/ExtractInterfaceModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ExtractInterfaceModel : IRefactoringModel
2020
public string InterfaceName { get; set; }
2121
public ObservableCollection<InterfaceMember> Members { get; set; } = new ObservableCollection<InterfaceMember>();
2222
public IEnumerable<InterfaceMember> SelectedMembers => Members.Where(m => m.IsSelected);
23-
public ClassInstancing ClassInstancing { get; set; }
23+
public ClassInstancing ImplementingClassInstancing { get; set; }
2424

2525
public static readonly DeclarationType[] MemberTypes =
2626
{
@@ -35,6 +35,9 @@ public ExtractInterfaceModel(IDeclarationFinderProvider declarationFinderProvide
3535
{
3636
TargetDeclaration = target;
3737
DeclarationFinderProvider = declarationFinderProvider;
38+
ImplementingClassInstancing = System.Convert.ToBoolean(target.Attributes.ExposedAttribute.Values.First())
39+
? ClassInstancing.PublicNotCreatable
40+
: ClassInstancing.Private;
3841

3942
if (TargetDeclaration == null)
4043
{

Rubberduck.Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void AddInterface(ExtractInterfaceModel model)
9898
return; //The target project is not available.
9999
}
100100

101-
AddInterfaceClass(model.TargetDeclaration, model.InterfaceName, GetInterfaceModuleBody(model));
101+
AddInterfaceClass(model.TargetDeclaration, model.InterfaceName, GetInterfaceModuleBody(model), model.ImplementingClassInstancing);
102102

103103
var rewriteSession = RewritingManager.CheckOutCodePaneSession();
104104
var rewriter = rewriteSession.CheckOutModuleRewriter(model.TargetDeclaration.QualifiedModuleName);
@@ -116,7 +116,7 @@ private void AddInterface(ExtractInterfaceModel model)
116116
}
117117
}
118118

119-
private void AddInterfaceClass(Declaration implementingClass, string interfaceName, string interfaceBody)
119+
private void AddInterfaceClass(Declaration implementingClass, string interfaceName, string interfaceBody, ClassInstancing interfaceInstancing)
120120
{
121121
var targetProject = implementingClass.Project;
122122
using (var components = targetProject.VBComponents)
@@ -128,16 +128,15 @@ private void AddInterfaceClass(Declaration implementingClass, string interfaceNa
128128
interfaceComponent.Name = interfaceName;
129129

130130
var optionPresent = interfaceModule.CountOfLines > 1;
131-
var optionExplicit = $"{Tokens.Option} {Tokens.Explicit}{Environment.NewLine}";
132131
if (!optionPresent)
133132
{
134-
interfaceModule.InsertLines(1, optionExplicit);
133+
interfaceModule.InsertLines(1, "Option Explicit" + Environment.NewLine);
135134
}
136135

137-
interfaceModule.InsertLines(3, interfaceBody);
136+
interfaceModule.InsertLines(1, "'@Interface");
137+
interfaceModule.InsertLines(4, interfaceBody);
138138

139-
var classIsExposed = Convert.ToBoolean(implementingClass.Attributes.ExposedAttribute.Values.First());
140-
if (classIsExposed)
139+
if (interfaceInstancing == ClassInstancing.PublicNotCreatable)
141140
{
142141
AddExposedAttribute(components, interfaceComponent);
143142
}
@@ -155,6 +154,7 @@ private void AddExposedAttribute(IVBComponents components, IVBComponent interfac
155154

156155
var text = System.IO.File.ReadAllText(tempFile);
157156
var sb = new System.Text.StringBuilder(text);
157+
sb.Insert(text.IndexOf("Option Explicit"), "'@Exposed" + Environment.NewLine);
158158
sb.Replace("Attribute VB_Exposed = False", "Attribute VB_Exposed = True");
159159
System.IO.File.WriteAllText(tempFile, sb.ToString());
160160

Rubberduck.Resources/RubberduckUI.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/RubberduckUI.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,6 @@ Import aborted.</value>
17611761
<value>Private</value>
17621762
</data>
17631763
<data name="ExtractInterface_PublicNotCreatableRadioButton" xml:space="preserve">
1764-
<value>Public Not Creatable</value>
1764+
<value>Public (Not Creatable)</value>
17651765
</data>
17661766
</root>

0 commit comments

Comments
 (0)