Skip to content

Commit 8b59aa5

Browse files
committed
Add PropertyAttributeSetsGenerator
Extracts the PropertyAttributeSet generation responsibility from the various IEncapuslateFieldCandidate classes. Some removal of unused functions/properties and refactoring of IEncapsulateFielCandidate classes.
1 parent 0c41340 commit 8b59aa5

File tree

10 files changed

+371
-395
lines changed

10 files changed

+371
-395
lines changed

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldInsertNewCode/EncapsulateFieldCodeBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public EncapsulateFieldCodeBuilder(ICodeBuilder codeBuilder)
3030
string propertyLet = null;
3131
string propertySet = null;
3232

33-
if (propertyAttributes.GenerateLetter)
33+
if (propertyAttributes.GeneratePropertyLet)
3434
{
35-
var letterContent = $"{FourSpaces}{propertyAttributes.BackingField} = {propertyAttributes.ParameterName}";
35+
var letterContent = $"{FourSpaces}{propertyAttributes.BackingField} = {propertyAttributes.RHSParameterIdentifier}";
3636
if (!_codeBuilder.TryBuildPropertyLetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out propertyLet, content: letterContent))
3737
{
3838
throw new ArgumentException();
3939
}
4040
}
4141

42-
if (propertyAttributes.GenerateSetter)
42+
if (propertyAttributes.GeneratePropertySet)
4343
{
44-
var setterContent = $"{FourSpaces}{Tokens.Set} {propertyAttributes.BackingField} = {propertyAttributes.ParameterName}";
44+
var setterContent = $"{FourSpaces}{Tokens.Set} {propertyAttributes.BackingField} = {propertyAttributes.RHSParameterIdentifier}";
4545
if (!_codeBuilder.TryBuildPropertySetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out propertySet, content: setterContent))
4646
{
4747
throw new ArgumentException();

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldInsertNewCode/EncapsulateFieldInsertNewCodeModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public EncapsulateFieldInsertNewCodeModel(IEnumerable<IEncapsulateFieldCandidate
2020

2121
public QualifiedModuleName QualifiedModuleName { get; } = new QualifiedModuleName();
2222

23+
public bool CreateNewObjectStateUDT => !ObjectStateUDTField?.IsExistingDeclaration ?? false;
24+
25+
public IObjectStateUDT ObjectStateUDTField { set; get; }
26+
2327
public IReadOnlyCollection<IEncapsulateFieldCandidate> SelectedFieldCandidates { get; }
2428
}
2529
}

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldInsertNewCode/EncapsulateFieldInsertNewCodeRefactoringAction.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,32 @@ namespace Rubberduck.Refactorings.EncapsulateFieldInsertNewCode
1313
{
1414
public class EncapsulateFieldInsertNewCodeRefactoringAction : CodeOnlyRefactoringActionBase<EncapsulateFieldInsertNewCodeModel>
1515
{
16-
private readonly static string _doubleSpace = $"{Environment.NewLine}{Environment.NewLine}";
17-
private int? _codeSectionStartIndex;
1816
private readonly IDeclarationFinderProvider _declarationFinderProvider;
19-
private readonly IEncapsulateFieldCodeBuilderFactory _encapsulateFieldCodeBuilderFactory;
17+
private readonly IPropertyAttributeSetsGenerator _propertyAttributeSetsGenerator;
18+
private readonly IEncapsulateFieldCodeBuilder _encapsulateFieldCodeBuilder;
19+
2020
public EncapsulateFieldInsertNewCodeRefactoringAction(
2121
IDeclarationFinderProvider declarationFinderProvider,
2222
IRewritingManager rewritingManager,
23+
IPropertyAttributeSetsGenerator propertyAttributeSetsGenerator,
2324
IEncapsulateFieldCodeBuilderFactory encapsulateFieldCodeBuilderFactory)
2425
: base(rewritingManager)
2526
{
2627
_declarationFinderProvider = declarationFinderProvider;
27-
_encapsulateFieldCodeBuilderFactory = encapsulateFieldCodeBuilderFactory;
28+
_propertyAttributeSetsGenerator = propertyAttributeSetsGenerator;
29+
_encapsulateFieldCodeBuilder = encapsulateFieldCodeBuilderFactory.Create();
2830
}
2931

3032
public override void Refactor(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
3133
{
32-
_codeSectionStartIndex = _declarationFinderProvider.DeclarationFinder
33-
.Members(model.QualifiedModuleName).Where(m => m.IsMember())
34-
.OrderBy(c => c.Selection)
35-
.FirstOrDefault()?.Context.Start.TokenIndex;
34+
if (model.CreateNewObjectStateUDT)
35+
{
36+
var objectStateFieldDeclaration = _encapsulateFieldCodeBuilder.BuildObjectStateFieldDeclaration(model.ObjectStateUDTField);
37+
model.NewContentAggregator.AddNewContent(NewContentType.DeclarationBlock, objectStateFieldDeclaration);
38+
39+
var objectStateTypeDeclarationBlock = _encapsulateFieldCodeBuilder.BuildUserDefinedTypeDeclaration(model.ObjectStateUDTField, model.SelectedFieldCandidates);
40+
model.NewContentAggregator.AddNewContent(NewContentType.UserDefinedTypeDeclaration, objectStateTypeDeclarationBlock);
41+
}
3642

3743
LoadNewPropertyBlocks(model, rewriteSession);
3844

@@ -41,14 +47,16 @@ public override void Refactor(EncapsulateFieldInsertNewCodeModel model, IRewrite
4147
model.NewContentAggregator = null;
4248
}
4349

44-
public void LoadNewPropertyBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
50+
private void LoadNewPropertyBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
4551
{
46-
var builder = _encapsulateFieldCodeBuilderFactory.Create();
47-
foreach (var propertyAttributes in model.SelectedFieldCandidates.SelectMany(f => f.PropertyAttributeSets))
52+
var propAttributeSets = model.SelectedFieldCandidates
53+
.SelectMany(f => _propertyAttributeSetsGenerator.GeneratePropertyAttributeSets(f)).ToList();
54+
55+
foreach (var propertyAttributeSet in propAttributeSets)
4856
{
49-
Debug.Assert(propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.Variable) || propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.UserDefinedTypeMember));
57+
Debug.Assert(propertyAttributeSet.Declaration.DeclarationType.HasFlag(DeclarationType.Variable) || propertyAttributeSet.Declaration.DeclarationType.HasFlag(DeclarationType.UserDefinedTypeMember));
5058

51-
var (Get, Let, Set) = builder.BuildPropertyBlocks(propertyAttributes);
59+
var (Get, Let, Set) = _encapsulateFieldCodeBuilder.BuildPropertyBlocks(propertyAttributeSet);
5260

5361
var blocks = new List<string>() { Get, Let, Set };
5462
blocks.ForEach(s => model.NewContentAggregator.AddNewContent(NewContentType.CodeSectionBlock, s));
@@ -57,13 +65,16 @@ public void LoadNewPropertyBlocks(EncapsulateFieldInsertNewCodeModel model, IRew
5765

5866
private void InsertBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
5967
{
68+
6069
var newDeclarationSectionBlock = model.NewContentAggregator.RetrieveBlock(NewContentType.UserDefinedTypeDeclaration, NewContentType.DeclarationBlock, NewContentType.CodeSectionBlock);
6170
if (string.IsNullOrEmpty(newDeclarationSectionBlock))
6271
{
6372
return;
6473
}
6574

66-
var allNewContent = string.Join(_doubleSpace, new string[] { newDeclarationSectionBlock });
75+
var doubleSpace = $"{Environment.NewLine}{Environment.NewLine}";
76+
77+
var allNewContent = string.Join(doubleSpace, new string[] { newDeclarationSectionBlock });
6778

6879
var previewMarker = model.NewContentAggregator.RetrieveBlock(RubberduckUI.EncapsulateField_PreviewMarker);
6980
if (!string.IsNullOrEmpty(previewMarker))
@@ -73,22 +84,17 @@ private void InsertBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSess
7384

7485
var rewriter = rewriteSession.CheckOutModuleRewriter(model.QualifiedModuleName);
7586

76-
InsertBlock(allNewContent, _codeSectionStartIndex, rewriter);
77-
}
78-
79-
private static void InsertBlock(string content, int? insertionIndex, IModuleRewriter rewriter)
80-
{
81-
if (string.IsNullOrEmpty(content))
82-
{
83-
return;
84-
}
87+
var codeSectionStartIndex = _declarationFinderProvider.DeclarationFinder
88+
.Members(model.QualifiedModuleName).Where(m => m.IsMember())
89+
.OrderBy(c => c.Selection)
90+
.FirstOrDefault()?.Context.Start.TokenIndex;
8591

86-
if (insertionIndex.HasValue)
92+
if (codeSectionStartIndex.HasValue)
8793
{
88-
rewriter.InsertBefore(insertionIndex.Value, $"{content}{_doubleSpace}");
94+
rewriter.InsertBefore(codeSectionStartIndex.Value, $"{allNewContent}{doubleSpace}");
8995
return;
9096
}
91-
rewriter.InsertBefore(rewriter.TokenStream.Size - 1, $"{_doubleSpace}{content}");
97+
rewriter.InsertBefore(rewriter.TokenStream.Size - 1, $"{doubleSpace}{allNewContent}");
9298
}
9399
}
94100
}

Rubberduck.Refactorings/EncapsulateField/FieldCandidates/ArrayCandidate.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ public override bool TryValidateEncapsulationAttributes(out string errorMessage)
4949
public string UDTMemberDeclaration
5050
=> $"{PropertyIdentifier}({_subscripts}) {Tokens.As} {Declaration.AsTypeName}";
5151

52-
protected override string IdentifierForLocalReferences(IdentifierReference idRef)
53-
=> BackingIdentifier;
54-
5552
public bool HasExternalRedimOperation(out string errorMessage)
5653
{
5754
errorMessage = string.Empty;
Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
3-
using Rubberduck.Common;
4-
using Rubberduck.Parsing.Symbols;
1+
using Rubberduck.Parsing.Symbols;
52
using Rubberduck.VBEditor;
63

74
namespace Rubberduck.Refactorings.EncapsulateField
85
{
9-
106
public interface IEncapsulateFieldAsUDTMemberCandidate : IEncapsulateFieldCandidate
117
{
12-
string UDTMemberDeclaration { get; }
138
IObjectStateUDT ObjectStateUDT { set; get; }
9+
IEncapsulateFieldCandidate WrappedCandidate { get; }
1410
}
1511

1612
public class EncapsulateFieldAsUDTMemberCandidate : IEncapsulateFieldAsUDTMemberCandidate
@@ -27,17 +23,7 @@ public EncapsulateFieldAsUDTMemberCandidate(IEncapsulateFieldCandidate candidate
2723
_hashCode = _uniqueID.GetHashCode();
2824
}
2925

30-
public virtual string UDTMemberDeclaration
31-
{
32-
get
33-
{
34-
if (_wrapped is IArrayCandidate array)
35-
{
36-
return array.UDTMemberDeclaration;
37-
}
38-
return $"{BackingIdentifier} As {_wrapped.AsTypeName}";
39-
}
40-
}
26+
public IEncapsulateFieldCandidate WrappedCandidate => _wrapped;
4127

4228
private IObjectStateUDT _objectStateUDT;
4329
public IObjectStateUDT ObjectStateUDT
@@ -94,35 +80,12 @@ public bool IsReadOnly
9480
get => _wrapped.IsReadOnly;
9581
}
9682

97-
public string ParameterName => _wrapped.ParameterName;
98-
9983
public IEncapsulateFieldConflictFinder ConflictFinder
10084
{
10185
set => _wrapped.ConflictFinder = value;
10286
get => _wrapped.ConflictFinder;
10387
}
10488

105-
private string AccessorInProperty
106-
{
107-
get
108-
{
109-
if (_wrapped is IUserDefinedTypeMemberCandidate udtm)
110-
{
111-
return $"{ObjectStateUDT.FieldIdentifier}.{udtm.UDTField.PropertyIdentifier}.{BackingIdentifier}";
112-
}
113-
return $"{ObjectStateUDT.FieldIdentifier}.{BackingIdentifier}";
114-
}
115-
}
116-
117-
public string IdentifierForReference(IdentifierReference idRef)
118-
{
119-
if (idRef.QualifiedModuleName != QualifiedModuleName)
120-
{
121-
return PropertyIdentifier;
122-
}
123-
return BackingIdentifier;
124-
}
125-
12689
public string IdentifierName => _wrapped.IdentifierName;
12790

12891
public QualifiedModuleName QualifiedModuleName => _wrapped.QualifiedModuleName;
@@ -147,27 +110,6 @@ public bool TryValidateEncapsulationAttributes(out string errorMessage)
147110
return ConflictFinder.TryValidateEncapsulationAttributes(this, out errorMessage);
148111
}
149112

150-
public IEnumerable<PropertyAttributeSet> PropertyAttributeSets
151-
{
152-
get
153-
{
154-
var modifiedSets = new List<PropertyAttributeSet>();
155-
var sets = _wrapped.PropertyAttributeSets;
156-
for (var idx = 0; idx < sets.Count(); idx++)
157-
{
158-
var attributeSet = sets.ElementAt(idx);
159-
var fields = attributeSet.BackingField.Split(new char[] { '.' });
160-
161-
attributeSet.BackingField = fields.Count() > 1
162-
? $"{ObjectStateUDT.FieldIdentifier}.{attributeSet.BackingField.CapitalizeFirstLetter()}"
163-
: $"{ObjectStateUDT.FieldIdentifier}.{attributeSet.PropertyName.CapitalizeFirstLetter()}";
164-
165-
modifiedSets.Add(attributeSet);
166-
}
167-
return modifiedSets;
168-
}
169-
}
170-
171113
public override bool Equals(object obj)
172114
{
173115
return obj != null
@@ -179,23 +121,5 @@ public override bool Equals(object obj)
179121

180122
private static string BuildUniqueID(IEncapsulateFieldCandidate candidate, IObjectStateUDT field)
181123
=> $"{candidate.QualifiedModuleName.Name}.{field.IdentifierName}.{candidate.IdentifierName}";
182-
183-
private PropertyAttributeSet AsPropertyAttributeSet
184-
{
185-
get
186-
{
187-
return new PropertyAttributeSet()
188-
{
189-
PropertyName = PropertyIdentifier,
190-
BackingField = AccessorInProperty,
191-
AsTypeName = PropertyAsTypeName,
192-
ParameterName = ParameterName,
193-
GenerateLetter = ImplementLet,
194-
GenerateSetter = ImplementSet,
195-
UsesSetAssignment = Declaration.IsObject,
196-
IsUDTProperty = true
197-
};
198-
}
199-
}
200124
}
201125
}

0 commit comments

Comments
 (0)