Skip to content

Commit cd7cda4

Browse files
committed
Refactoring related to tests and misc cleanup
1 parent b21c24d commit cd7cda4

File tree

6 files changed

+196
-172
lines changed

6 files changed

+196
-172
lines changed

Rubberduck.Refactorings/Common/DeclarationExtensions.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,51 @@ public static bool IsDeclaredInList(this Declaration declaration)
5656
/// <param name="content"></param>
5757
/// <param name="parameterIdentifier"></param>
5858
/// <returns></returns>
59-
public static string FieldToPropertyBlock(this Declaration variable, DeclarationType letSetGetType, string propertyIdentifier, string accessibility, string content, string parameterIdentifier = "value")
59+
public static string FieldToPropertyBlock(this Declaration variable, DeclarationType letSetGetType, string propertyIdentifier, string accessibility = null, string content = null, string parameterIdentifier = null)
6060
{
61-
var template = string.Join(Environment.NewLine, accessibility + " {0}{1} {2}{3}", $"{content}", Tokens.End + " {0}", string.Empty);
61+
//"value" is the default
62+
var propertyValueParam = parameterIdentifier ?? Resources.RubberduckUI.EncapsulateField_DefaultPropertyParameter;
63+
64+
var propertyEndStmt = $"{Tokens.End} {Tokens.Property}";
6265

6366
var asType = variable.IsArray
6467
? $"{Tokens.Variant}"
6568
: variable.IsEnumField() && variable.AsTypeDeclaration.HasPrivateAccessibility()
6669
? $"{Tokens.Long}"
6770
: $"{variable.AsTypeName}";
6871

72+
var asTypeClause = $"{Tokens.As} {asType}";
73+
6974
var paramAccessibility = variable.IsUserDefinedType() ? Tokens.ByRef : Tokens.ByVal;
7075

71-
var letSetParameter = $"({paramAccessibility} {parameterIdentifier} {Tokens.As} {asType})";
76+
var letSetParameter = $"{paramAccessibility} {propertyValueParam} {Tokens.As} {asType}";
7277

73-
if (letSetGetType.Equals(DeclarationType.PropertyGet))
78+
switch (letSetGetType)
7479
{
75-
return string.Format(template, Tokens.Property, $" {Tokens.Get}", $"{propertyIdentifier}()", $" {Tokens.As} {asType}");
76-
}
77-
78-
if (letSetGetType.Equals(DeclarationType.PropertyLet))
79-
{
80-
return string.Format(template, Tokens.Property, $" {Tokens.Let}", $"{propertyIdentifier}{letSetParameter}", string.Empty);
80+
case DeclarationType.PropertyGet:
81+
return string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {PropertyTypeStatement(letSetGetType)} {propertyIdentifier}() {asTypeClause}", content, propertyEndStmt);
82+
case DeclarationType.PropertyLet:
83+
case DeclarationType.PropertySet:
84+
return string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {PropertyTypeStatement(letSetGetType)} {propertyIdentifier}({letSetParameter})", content, propertyEndStmt);
85+
default:
86+
throw new ArgumentException();
8187
}
88+
}
8289

83-
if (letSetGetType.Equals(DeclarationType.PropertySet))
90+
private static string PropertyTypeStatement(DeclarationType declarationType)
91+
{
92+
switch (declarationType)
8493
{
85-
return string.Format(template, Tokens.Property, $" {Tokens.Set}", $"{propertyIdentifier}{letSetParameter}", string.Empty);
94+
case DeclarationType.PropertyGet:
95+
return $"{Tokens.Property} {Tokens.Get}";
96+
case DeclarationType.PropertyLet:
97+
return $"{Tokens.Property} {Tokens.Let}";
98+
case DeclarationType.PropertySet:
99+
return $"{Tokens.Property} {Tokens.Set}";
100+
default:
101+
throw new ArgumentException();
86102
}
87103

88-
return string.Empty;
89104
}
90105
}
91106
}

Rubberduck.Refactorings/Common/ModuleBodyElementDeclarationExtensions.cs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,25 @@ public static class ModuleBodyElementDeclarationExtensions
1818
/// <param name="declaration"></param>
1919
/// <returns></returns>
2020
public static string FullMemberSignature(this ModuleBodyElementDeclaration declaration,
21-
string accessibility = null,
22-
string newIdentifier = null)
21+
string accessibility = null,
22+
string newIdentifier = null)
2323
{
24-
var identifier = newIdentifier ?? declaration.IdentifierName;
25-
26-
var fullSignatureFormat = string.Empty;
27-
switch (declaration.Context)
28-
{
29-
case VBAParser.SubStmtContext _:
30-
fullSignatureFormat = $"{{0}} {Tokens.Sub} {identifier}({{1}}){{2}}";
31-
break;
32-
case VBAParser.FunctionStmtContext _:
33-
fullSignatureFormat = $"{{0}} {Tokens.Function} {identifier}({{1}}){{2}}";
34-
break;
35-
case VBAParser.PropertyGetStmtContext _:
36-
fullSignatureFormat = $"{{0}} {Tokens.Property} {Tokens.Get} {identifier}({{1}}){{2}}";
37-
break;
38-
case VBAParser.PropertyLetStmtContext _:
39-
fullSignatureFormat = $"{{0}} {Tokens.Property} {Tokens.Let} {identifier}({{1}}){{2}}";
40-
break;
41-
case VBAParser.PropertySetStmtContext _:
42-
fullSignatureFormat = $"{{0}} {Tokens.Property} {Tokens.Set} {identifier}({{1}}){{2}}";
43-
break;
44-
default:
45-
throw new ArgumentException();
46-
}
47-
4824
var accessibilityToken = declaration.Accessibility.Equals(Accessibility.Implicit)
4925
? Tokens.Public
5026
: $"{declaration.Accessibility.ToString()}";
5127

52-
accessibilityToken = accessibility ?? accessibilityToken;
53-
54-
var improvedArgList = ImprovedArgumentList(declaration);
55-
56-
var asTypeSuffix = declaration.AsTypeName == null
28+
var asTypeClause = declaration.AsTypeName == null
5729
? string.Empty
5830
: $" {Tokens.As} {declaration.AsTypeName}";
5931

60-
var fullSignature = string.Format(fullSignatureFormat, accessibilityToken, improvedArgList, asTypeSuffix);
32+
var fullSignatureFormat = RetrieveFullSignatureFormat(declaration);
33+
34+
var fullSignature = string.Format(fullSignatureFormat,
35+
accessibility ?? accessibilityToken,
36+
newIdentifier ?? declaration.IdentifierName,
37+
ImprovedArgumentList(declaration),
38+
asTypeClause);
39+
6140
return fullSignature.Trim();
6241
}
6342

@@ -155,6 +134,33 @@ private static string BuildParameterDeclaration(ParameterDeclaration parameter,
155134
return $"{FormatStandardElement(optional)}{FormatStandardElement(accessibility)}{FormatStandardElement(name)}{FormattedAsTypeName(parameter.AsTypeName)}{FormattedDefaultValue(defaultValue)}".Trim();
156135
}
157136

137+
private static string RetrieveFullSignatureFormat(Declaration declaration)
138+
{
139+
var fullSignatureFormat = $"{{0}} THE_MEMBER_TYPE {{1}}({{2}}){{3}}";
140+
141+
switch (declaration.Context)
142+
{
143+
case VBAParser.SubStmtContext _:
144+
fullSignatureFormat = fullSignatureFormat.Replace("THE_MEMBER_TYPE", Tokens.Sub);
145+
break;
146+
case VBAParser.FunctionStmtContext _:
147+
fullSignatureFormat = fullSignatureFormat.Replace("THE_MEMBER_TYPE", Tokens.Function);
148+
break;
149+
case VBAParser.PropertyGetStmtContext _:
150+
fullSignatureFormat = fullSignatureFormat.Replace("THE_MEMBER_TYPE", $"{Tokens.Property} {Tokens.Get}");
151+
break;
152+
case VBAParser.PropertyLetStmtContext _:
153+
fullSignatureFormat = fullSignatureFormat.Replace("THE_MEMBER_TYPE", $"{Tokens.Property} {Tokens.Let}");
154+
break;
155+
case VBAParser.PropertySetStmtContext _:
156+
fullSignatureFormat = fullSignatureFormat.Replace("THE_MEMBER_TYPE", $"{Tokens.Property} {Tokens.Set}");
157+
break;
158+
default:
159+
throw new ArgumentException();
160+
}
161+
return fullSignatureFormat;
162+
}
163+
158164
private static string FormatStandardElement(string element) => string.IsNullOrEmpty(element)
159165
? string.Empty
160166
: $"{element} ";

Rubberduck.Refactorings/EncapsulateField/EncapsulationStrategies/ConvertFieldsToUDTMembers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ protected override void LoadNewPropertyBlocks()
8282
{
8383
getContent = $"{Tokens.Set} {getContent}";
8484
}
85-
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertyGet, set.PropertyName, Tokens.Public, getContent));
85+
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertyGet, set.PropertyName, content: $"{_defaultIndent}{getContent}"));
8686
if (converted.IsReadOnly)
8787
{
8888
continue;
8989
}
9090
if (set.GenerateLetter)
9191
{
92-
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertyLet, set.PropertyName, Tokens.Public, $"{set.BackingField} = {set.ParameterName}"));
92+
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertyLet, set.PropertyName, content: $"{_defaultIndent}{set.BackingField} = {set.ParameterName}"));
9393
}
9494
if (set.GenerateSetter)
9595
{
96-
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertySet, set.PropertyName, Tokens.Public, $"{Tokens.Set} {set.BackingField} = {set.ParameterName}"));
96+
AddContentBlock(NewContentTypes.MethodBlock, variableDeclaration.FieldToPropertyBlock(DeclarationType.PropertySet, set.PropertyName, content: $"{_defaultIndent}{Tokens.Set} {set.BackingField} = {set.ParameterName}"));
9797
}
9898
}
9999
}

Rubberduck.Refactorings/EncapsulateField/EncapsulationStrategies/EncapsulateFieldStrategyBase.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public abstract class EncapsulateFieldStrategyBase : IEncapsulateStrategy
4040
protected readonly IIndenter _indenter;
4141
protected QualifiedModuleName _targetQMN;
4242
private readonly int? _codeSectionStartIndex;
43+
protected const string _defaultIndent = " "; //4 spaces
4344

4445
protected Dictionary<IdentifierReference, (ParserRuleContext, string)> IdentifierReplacements { get; } = new Dictionary<IdentifierReference, (ParserRuleContext, string)>();
4546

@@ -139,35 +140,35 @@ private void InsertNewContent(IEncapsulateFieldRewriteSession refactorRewriteSes
139140

140141
protected virtual void LoadNewPropertyBlocks()
141142
{
142-
foreach (var set in SelectedFields.SelectMany(f => f.PropertyAttributeSets))
143+
foreach (var attributeSet in SelectedFields.SelectMany(f => f.PropertyAttributeSets))
143144
{
144-
if (set.Declaration is VariableDeclaration || set.Declaration.DeclarationType.Equals(DeclarationType.UserDefinedTypeMember))
145+
if (attributeSet.Declaration is VariableDeclaration || attributeSet.Declaration.DeclarationType.Equals(DeclarationType.UserDefinedTypeMember))
145146
{
146-
var getContent = $"{set.PropertyName} = {set.BackingField}";
147-
if (set.UsesSetAssignment)
147+
var getContent = $"{attributeSet.PropertyName} = {attributeSet.BackingField}";
148+
if (attributeSet.UsesSetAssignment)
148149
{
149150
getContent = $"{Tokens.Set} {getContent}";
150151
}
151-
if (set.AsTypeName.Equals(Tokens.Variant) && !set.Declaration.IsArray)
152+
if (attributeSet.AsTypeName.Equals(Tokens.Variant) && !attributeSet.Declaration.IsArray)
152153
{
153154
getContent = string.Join(Environment.NewLine,
154-
$"If IsObject({set.BackingField}) Then",
155-
$" Set {set.PropertyName} = {set.BackingField}",
156-
"Else",
157-
$" {set.PropertyName} = {set.BackingField}",
158-
"End If",
155+
$"{Tokens.If} IsObject({attributeSet.BackingField}) {Tokens.Then}",
156+
$"{_defaultIndent}{Tokens.Set} {attributeSet.PropertyName} = {attributeSet.BackingField}",
157+
Tokens.Else,
158+
$"{_defaultIndent}{attributeSet.PropertyName} = {attributeSet.BackingField}",
159+
$"{Tokens.End} {Tokens.If}",
159160
Environment.NewLine);
160161
}
161162

162-
AddContentBlock(NewContentTypes.MethodBlock, set.Declaration.FieldToPropertyBlock(DeclarationType.PropertyGet, set.PropertyName, Tokens.Public, $" {getContent}"));
163+
AddContentBlock(NewContentTypes.MethodBlock, attributeSet.Declaration.FieldToPropertyBlock(DeclarationType.PropertyGet, attributeSet.PropertyName, content: $"{_defaultIndent}{getContent}"));
163164

164-
if (set.GenerateLetter)
165+
if (attributeSet.GenerateLetter)
165166
{
166-
AddContentBlock(NewContentTypes.MethodBlock, set.Declaration.FieldToPropertyBlock(DeclarationType.PropertyLet, set.PropertyName, Tokens.Public, $" {set.BackingField} = {set.ParameterName}"));
167+
AddContentBlock(NewContentTypes.MethodBlock, attributeSet.Declaration.FieldToPropertyBlock(DeclarationType.PropertyLet, attributeSet.PropertyName, content: $"{_defaultIndent}{attributeSet.BackingField} = {attributeSet.ParameterName}"));
167168
}
168-
if (set.GenerateSetter)
169+
if (attributeSet.GenerateSetter)
169170
{
170-
AddContentBlock(NewContentTypes.MethodBlock, set.Declaration.FieldToPropertyBlock(DeclarationType.PropertySet, set.PropertyName, Tokens.Public, $" {Tokens.Set} {set.BackingField} = {set.ParameterName}"));
171+
AddContentBlock(NewContentTypes.MethodBlock, attributeSet.Declaration.FieldToPropertyBlock(DeclarationType.PropertySet, attributeSet.PropertyName, content: $"{_defaultIndent}{Tokens.Set} {attributeSet.BackingField} = {attributeSet.ParameterName}"));
171172
}
172173
}
173174
}

Rubberduck.Refactorings/ImplementInterface/AddInterfaceImplementations/AddInterfaceImplementationsRefactoringAction.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ namespace Rubberduck.Refactorings.AddInterfaceImplementations
1111
{
1212
public class AddInterfaceImplementationsRefactoringAction : CodeOnlyRefactoringActionBase<AddInterfaceImplementationsModel>
1313
{
14-
private const string MemberBody = " Err.Raise 5 'TODO implement interface member";
14+
private readonly string _memberBody;
1515

1616
public AddInterfaceImplementationsRefactoringAction(IRewritingManager rewritingManager)
1717
: base(rewritingManager)
18-
{}
18+
{
19+
_memberBody = $" {Tokens.Err}.Raise 5 {Resources.RubberduckUI.ImplementInterface_TODO}";
20+
}
1921

2022
public override void Refactor(AddInterfaceImplementationsModel model, IRewriteSession rewriteSession)
2123
{
@@ -35,27 +37,27 @@ private string GetInterfaceMember(Declaration member, string interfaceName)
3537
{
3638
if (member is ModuleBodyElementDeclaration mbed)
3739
{
38-
return mbed.AsCodeBlock(accessibility: Tokens.Private, newIdentifier: $"{interfaceName}_{member.IdentifierName}", content: MemberBody );
40+
return mbed.AsCodeBlock(accessibility: Tokens.Private, newIdentifier: $"{interfaceName}_{member.IdentifierName}", content: _memberBody);
3941
}
4042

4143
if (member.DeclarationType.Equals(DeclarationType.Variable))
4244
{
43-
var propertyGet = member.FieldToPropertyBlock(DeclarationType.PropertyGet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, MemberBody, "rhs");
45+
var propertyGet = member.FieldToPropertyBlock(DeclarationType.PropertyGet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, _memberBody);
4446
var members = new List<string> { propertyGet };
4547

4648
if (member.AsTypeName.Equals(Tokens.Variant) || !member.IsObject)
4749
{
48-
var propertyLet = member.FieldToPropertyBlock(DeclarationType.PropertyLet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, MemberBody, "rhs");
50+
var propertyLet = member.FieldToPropertyBlock(DeclarationType.PropertyLet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, _memberBody);
4951
members.Add(propertyLet);
5052
}
5153

5254
if (member.AsTypeName.Equals(Tokens.Variant) || member.IsObject)
5355
{
54-
var propertySet = member.FieldToPropertyBlock(DeclarationType.PropertySet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, MemberBody, "rhs");
56+
var propertySet = member.FieldToPropertyBlock(DeclarationType.PropertySet, $"{interfaceName}_{member.IdentifierName}", Tokens.Private, _memberBody);
5557
members.Add(propertySet);
5658
}
5759

58-
return string.Join(Environment.NewLine, members);
60+
return string.Join($"{Environment.NewLine}{Environment.NewLine}", members);
5961
}
6062

6163
return string.Empty;

0 commit comments

Comments
 (0)