Skip to content

Commit 948b733

Browse files
committed
Merge branch 'refs/heads/EF_UseAccessibilityEnum' into EF_CodeBuilderChanges
2 parents 4281486 + 3d2ed90 commit 948b733

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

Rubberduck.Refactorings/Common/CodeBuilder.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface ICodeBuilder
2121
/// <param name="content">Main body content/logic of the member</param>
2222
string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
2323
string content = null,
24-
string accessibility = null,
24+
Accessibility accessibility = Accessibility.Public,
2525
string newIdentifier = null);
2626

2727
/// <summary>
@@ -35,37 +35,36 @@ string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
3535
/// Generates a Property Get codeblock based on the prototype declaration
3636
/// </summary>
3737
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
38-
/// <param name="content">Member body content. Formatting is the responsibility of the caller</param>
39-
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
38+
/// <param name="content">Member body content. Null results in an empty body. Formatting is the responsibility of the caller</param>
4039
bool TryBuildPropertyGetCodeBlock(Declaration prototype,
4140
string propertyIdentifier,
4241
out string codeBlock,
43-
string accessibility = null,
42+
Accessibility accessibility = Accessibility.Public,
4443
string content = null);
4544

4645
/// <summary>
4746
/// Generates a Property Let codeblock based on the prototype declaration
4847
/// </summary>
4948
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
50-
/// <param name="content">Member body content. Formatting is the responsibility of the caller</param>
51-
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
49+
/// <param name="content">Member body content. Null results in an empty body. Formatting is the responsibility of the caller</param>
50+
/// <param name="parameterIdentifier">Defaults to 'RHS' unless otherwise specified</param>
5251
bool TryBuildPropertyLetCodeBlock(Declaration prototype,
5352
string propertyIdentifier,
5453
out string codeBlock,
55-
string accessibility = null,
54+
Accessibility accessibility = Accessibility.Public,
5655
string content = null,
5756
string parameterIdentifier = null);
5857

5958
/// <summary>
6059
/// Generates a Property Set codeblock based on the prototype declaration
6160
/// </summary>
6261
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
63-
/// <param name="content">Member body content. Formatting is the responsibility of the caller</param>
64-
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
62+
/// <param name="content">Member body content. Null results in an empty body. Formatting is the responsibility of the caller</param>
63+
/// <param name="parameterIdentifier">Defaults to 'RHS' unless otherwise specified</param>
6564
bool TryBuildPropertySetCodeBlock(Declaration prototype,
6665
string propertyIdentifier,
6766
out string codeBlock,
68-
string accessibility = null,
67+
Accessibility accessibility = Accessibility.Public,
6968
string content = null,
7069
string parameterIdentifier = null);
7170

@@ -82,15 +81,15 @@ bool TryBuildPropertySetCodeBlock(Declaration prototype,
8281
/// Generates a <c>UserDefinedTypeMember</c> declaration expression based on the prototype declaration
8382
/// </summary>
8483
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
85-
/// <param name="indentation">Defaults is 4 spaces</param>
84+
/// <param name="indentation">If left null, 4 spaces of indentation are applied</param>
8685
bool TryBuildUDTMemberDeclaration(string identifier, Declaration prototype, out string declaration, string indentation = null);
8786
}
8887

8988
public class CodeBuilder : ICodeBuilder
9089
{
9190
public string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
9291
string content = null,
93-
string accessibility = null,
92+
Accessibility accessibility = Accessibility.Public,
9493
string newIdentifier = null)
9594
{
9695

@@ -105,16 +104,16 @@ public string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declara
105104
return string.Concat(elements);
106105
}
107106

108-
public bool TryBuildPropertyGetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, string accessibility = null, string content = null)
107+
public bool TryBuildPropertyGetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, Accessibility accessibility = Accessibility.Public, string content = null)
109108
=> TryBuildPropertyBlockFromTarget(prototype, DeclarationType.PropertyGet, propertyIdentifier, out codeBlock, accessibility, content);
110109

111-
public bool TryBuildPropertyLetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, string accessibility = null, string content = null, string parameterIdentifier = null)
110+
public bool TryBuildPropertyLetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, Accessibility accessibility = Accessibility.Public, string content = null, string parameterIdentifier = null)
112111
=> TryBuildPropertyBlockFromTarget(prototype, DeclarationType.PropertyLet, propertyIdentifier, out codeBlock, accessibility, content, parameterIdentifier);
113112

114-
public bool TryBuildPropertySetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, string accessibility = null, string content = null, string parameterIdentifier = null)
113+
public bool TryBuildPropertySetCodeBlock(Declaration prototype, string propertyIdentifier, out string codeBlock, Accessibility accessibility = Accessibility.Public, string content = null, string parameterIdentifier = null)
115114
=> TryBuildPropertyBlockFromTarget(prototype, DeclarationType.PropertySet, propertyIdentifier, out codeBlock, accessibility, content, parameterIdentifier);
116115

117-
private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType letSetGetType, string propertyIdentifier, out string codeBlock, string accessibility = null, string content = null, string parameterIdentifier = null) where T : Declaration
116+
private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType letSetGetType, string propertyIdentifier, out string codeBlock, Accessibility accessibility, string content = null, string parameterIdentifier = null) where T : Declaration
118117
{
119118
codeBlock = string.Empty;
120119
if (!letSetGetType.HasFlag(DeclarationType.Property))
@@ -142,27 +141,23 @@ private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType let
142141
var letSetParamExpression = $"{paramMechanism} {propertyValueParam} {asTypeClause}";
143142

144143
codeBlock = letSetGetType.HasFlag(DeclarationType.PropertyGet)
145-
? string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {TypeToken(letSetGetType)} {propertyIdentifier}() {asTypeClause}", content, EndStatement(letSetGetType))
146-
: string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {TypeToken(letSetGetType)} {propertyIdentifier}({letSetParamExpression})", content, EndStatement(letSetGetType));
144+
? string.Join(Environment.NewLine, $"{AccessibilityToken(accessibility)} {TypeToken(letSetGetType)} {propertyIdentifier}() {asTypeClause}", content, EndStatement(letSetGetType))
145+
: string.Join(Environment.NewLine, $"{AccessibilityToken(accessibility)} {TypeToken(letSetGetType)} {propertyIdentifier}({letSetParamExpression})", content, EndStatement(letSetGetType));
147146
return true;
148147
}
149148

150149
public string ImprovedFullMemberSignature(ModuleBodyElementDeclaration declaration)
151-
=> ImprovedFullMemberSignatureInternal(declaration);
150+
=> ImprovedFullMemberSignatureInternal(declaration, declaration.Accessibility);
152151

153-
private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration declaration, string accessibility = null, string newIdentifier = null)
152+
private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration declaration, Accessibility accessibility, string newIdentifier = null)
154153
{
155-
var accessibilityToken = declaration.Accessibility.Equals(Accessibility.Implicit)
156-
? Tokens.Public
157-
: $"{declaration.Accessibility.ToString()}";
158-
159154
var asTypeName = string.IsNullOrEmpty(declaration.AsTypeName)
160155
? string.Empty
161156
: $" {Tokens.As} {declaration.AsTypeName}";
162157

163158
var elements = new List<string>()
164159
{
165-
accessibility ?? accessibilityToken,
160+
AccessibilityToken(accessibility),
166161
$" {TypeToken(declaration.DeclarationType)} ",
167162
newIdentifier ?? declaration.IdentifierName,
168163
$"({ImprovedArgumentList(declaration)})",
@@ -297,6 +292,11 @@ private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Decl
297292
return $"{indentation ?? " "}{identifierExpression} {Tokens.As} {prototype.AsTypeName}";
298293
}
299294

295+
private static string AccessibilityToken(Accessibility accessibility)
296+
=> accessibility.Equals(Accessibility.Implicit)
297+
? Tokens.Public
298+
: $"{accessibility.ToString()}";
299+
300300
private static bool IsValidPrototypeDeclarationType(DeclarationType declarationType)
301301
{
302302
return declarationType.HasFlag(DeclarationType.Variable)

Rubberduck.Refactorings/ImplementInterface/AddInterfaceImplementations/AddInterfaceImplementationsRefactoringAction.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ private string GetInterfaceMember(Declaration member, string interfaceName)
3838
{
3939
if (member is ModuleBodyElementDeclaration mbed)
4040
{
41-
return _codeBuilder.BuildMemberBlockFromPrototype(mbed, accessibility: Tokens.Private, newIdentifier: $"{interfaceName}_{member.IdentifierName}", content: _memberBody);
41+
return _codeBuilder.BuildMemberBlockFromPrototype(mbed, accessibility: Accessibility.Private, newIdentifier: $"{interfaceName}_{member.IdentifierName}", content: _memberBody);
4242
}
4343

4444
if (member is VariableDeclaration variable)
4545
{
46-
if (!_codeBuilder.TryBuildPropertyGetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertyGet, Tokens.Private, _memberBody))
46+
if (!_codeBuilder.TryBuildPropertyGetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertyGet, Accessibility.Private, _memberBody))
4747
{
4848
throw new InvalidOperationException();
4949
}
@@ -52,7 +52,7 @@ private string GetInterfaceMember(Declaration member, string interfaceName)
5252

5353
if (variable.AsTypeName.Equals(Tokens.Variant) || !variable.IsObject)
5454
{
55-
if (!_codeBuilder.TryBuildPropertyLetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertyLet, Tokens.Private, _memberBody))
55+
if (!_codeBuilder.TryBuildPropertyLetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertyLet, Accessibility.Private, _memberBody))
5656
{
5757
throw new InvalidOperationException();
5858
}
@@ -61,7 +61,7 @@ private string GetInterfaceMember(Declaration member, string interfaceName)
6161

6262
if (variable.AsTypeName.Equals(Tokens.Variant) || variable.IsObject)
6363
{
64-
if (!_codeBuilder.TryBuildPropertySetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertySet, Tokens.Private, _memberBody))
64+
if (!_codeBuilder.TryBuildPropertySetCodeBlock(variable, $"{interfaceName}_{variable.IdentifierName}", out var propertySet, Accessibility.Private, _memberBody))
6565
{
6666
throw new InvalidOperationException();
6767
}

RubberduckTests/CodeBuilderTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ Private fuzz As ETestType2
5656
StringAssert.Contains($"Property Get {testParams.Identifier}() As {typeName}", result);
5757
}
5858

59-
[TestCase("fizz", DeclarationType.Variable, "Integer", "Public")]
60-
[TestCase("FirstValue", DeclarationType.UserDefinedTypeMember, "Long", "Public")]
61-
[TestCase("fazz", DeclarationType.Variable, "Long", "Public")]
62-
[TestCase("fuzz", DeclarationType.Variable, "ETestType2", "Private")]
59+
[TestCase("fizz", DeclarationType.Variable, "Integer", Accessibility.Public)]
60+
[TestCase("FirstValue", DeclarationType.UserDefinedTypeMember, "Long", Accessibility.Public)]
61+
[TestCase("fazz", DeclarationType.Variable, "Long", Accessibility.Public)]
62+
[TestCase("fuzz", DeclarationType.Variable, "ETestType2", Accessibility.Private)]
6363
[Category(nameof(CodeBuilder))]
64-
public void PropertyBlockFromPrototype_PropertyGetAccessibility(string targetIdentifier, DeclarationType declarationType, string typeName, string accessibility)
64+
public void PropertyBlockFromPrototype_PropertyGetAccessibility(string targetIdentifier, DeclarationType declarationType, string typeName, Accessibility accessibility)
6565
{
6666
var testParams = new PropertyBlockFromPrototypeParams("Bazz", DeclarationType.PropertyGet, accessibility);
6767
string inputCode =
@@ -643,7 +643,7 @@ private static string ImprovedArgumentListTest(ModuleBodyElementDeclaration mbed
643643
=> new CodeBuilder().ImprovedArgumentList(mbed);
644644

645645
private static string MemberBlockFromPrototypeTest(ModuleBodyElementDeclaration mbed, MemberBlockFromPrototypeTestParams testParams)
646-
=> new CodeBuilder().BuildMemberBlockFromPrototype(mbed, testParams.Accessibility, testParams.Content, testParams.NewIdentifier);
646+
=> new CodeBuilder().BuildMemberBlockFromPrototype(mbed, testParams.Content, testParams.Accessibility, testParams.NewIdentifier);
647647

648648
private (string procType, string endStmt) ProcedureTypeIdentifier(DeclarationType declarationType)
649649
{
@@ -666,7 +666,7 @@ private static string MemberBlockFromPrototypeTest(ModuleBodyElementDeclaration
666666

667667
private struct PropertyBlockFromPrototypeParams
668668
{
669-
public PropertyBlockFromPrototypeParams(string identifier, DeclarationType propertyType, string accessibility = null, string content = null, string paramIdentifier = null)
669+
public PropertyBlockFromPrototypeParams(string identifier, DeclarationType propertyType, Accessibility accessibility = Accessibility.Public, string content = null, string paramIdentifier = null)
670670
{
671671
Identifier = identifier;
672672
DeclarationType = propertyType;
@@ -676,21 +676,21 @@ public PropertyBlockFromPrototypeParams(string identifier, DeclarationType prope
676676
}
677677
public DeclarationType DeclarationType { get; }
678678
public string Identifier { get; }
679-
public string Accessibility {get; }
679+
public Accessibility Accessibility {get; }
680680
public string Content { get; }
681681
public string WriteParam { get; }
682682
}
683683

684684
private struct MemberBlockFromPrototypeTestParams
685685
{
686-
public MemberBlockFromPrototypeTestParams(ModuleBodyElementDeclaration mbed, string accessibility = null, string content = null, string newIdentifier = null)
686+
public MemberBlockFromPrototypeTestParams(ModuleBodyElementDeclaration mbed, Accessibility accessibility = Accessibility.Public, string content = null, string newIdentifier = null)
687687
{
688688
Accessibility = accessibility;
689689
Content = content;
690690
NewIdentifier = newIdentifier;
691691
}
692692

693-
public string Accessibility { get; }
693+
public Accessibility Accessibility { get; }
694694
public string Content { get; }
695695
public string NewIdentifier { get; }
696696
}

0 commit comments

Comments
 (0)