Skip to content

Commit fe263ab

Browse files
committed
Refactor code and objects related to new content insertion
1 parent be6f32b commit fe263ab

16 files changed

+207
-282
lines changed

Rubberduck.Refactorings/EncapsulateField/ConflictDetection/EncapsulateFieldUseBackingUDTMemberConflictFinder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Rubberduck.Refactorings.EncapsulateField
88
{
99
public interface IEncapsulateFieldUseBackingUDTMemberConflictFinder : IEncapsulateFieldConflictFinder
1010
{
11-
IObjectStateUDT AssignNoConflictIdentifiers(IObjectStateUDT stateUDT, IDeclarationFinderProvider declarationFinderProvider);
11+
IObjectStateUDT AssignNoConflictIdentifiers(IObjectStateUDT stateUDT);
1212
}
1313

1414
public class EncapsulateFieldUseBackingUDTMemberConflictFinder : EncapsulateFieldConflictFinderBase, IEncapsulateFieldUseBackingUDTMemberConflictFinder
@@ -52,16 +52,16 @@ public override bool TryValidateEncapsulationAttributes(IEncapsulateFieldCandida
5252
return !ConflictsWithExistingUDTMembers(objectStateUDT, field.BackingIdentifier);
5353
}
5454

55-
public IObjectStateUDT AssignNoConflictIdentifiers(IObjectStateUDT stateUDT, IDeclarationFinderProvider declarationFinderProvider)
55+
public IObjectStateUDT AssignNoConflictIdentifiers(IObjectStateUDT stateUDT)
5656
{
57-
var members = declarationFinderProvider.DeclarationFinder.Members(stateUDT.QualifiedModuleName);
57+
var members = _declarationFinderProvider.DeclarationFinder.Members(stateUDT.QualifiedModuleName);
5858
var guard = 0;
5959
while (guard++ < 10 && members.Any(m => m.IdentifierName.IsEquivalentVBAIdentifierTo(stateUDT.FieldIdentifier)))
6060
{
6161
stateUDT.FieldIdentifier = stateUDT.FieldIdentifier.IncrementEncapsulationIdentifier();
6262
}
6363

64-
members = declarationFinderProvider.DeclarationFinder.Members(stateUDT.QualifiedModuleName)
64+
members = _declarationFinderProvider.DeclarationFinder.Members(stateUDT.QualifiedModuleName)
6565
.Where(m => !_udtTypeIdentifierNonConflictTypes.Any(nct => m.DeclarationType.HasFlag(nct)));
6666

6767
guard = 0;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Rubberduck.VBEditor;
2-
using Rubberduck.Refactorings.CodeBlockInsert;
32
using System.Collections.Generic;
43
using System.Linq;
54
using Rubberduck.Refactorings.EncapsulateField;
@@ -10,29 +9,17 @@ public class EncapsulateFieldInsertNewCodeModel : IRefactoringModel
109
{
1110
public EncapsulateFieldInsertNewCodeModel(IEnumerable<IEncapsulateFieldCandidate> selectedFieldCandidates)
1211
{
13-
_selectedCandidates = selectedFieldCandidates.ToList();
14-
if (_selectedCandidates.Any())
12+
SelectedFieldCandidates = selectedFieldCandidates.ToList();
13+
if (SelectedFieldCandidates.Any())
1514
{
16-
QualifiedModuleName = _selectedCandidates.Select(f => f.QualifiedModuleName).First();
15+
QualifiedModuleName = SelectedFieldCandidates.First().QualifiedModuleName;
1716
}
18-
19-
NewContent = new Dictionary<NewContentType, List<string>>
20-
{
21-
{ NewContentType.PostContentMessage, new List<string>() },
22-
{ NewContentType.DeclarationBlock, new List<string>() },
23-
{ NewContentType.CodeSectionBlock, new List<string>() },
24-
{ NewContentType.TypeDeclarationBlock, new List<string>() }
25-
};
2617
}
2718

28-
public bool IncludeNewContentMarker { set; get; } = false;
19+
public INewContentAggregator NewContentAggregator { set; get; }
2920

3021
public QualifiedModuleName QualifiedModuleName { get; } = new QualifiedModuleName();
3122

32-
public Dictionary<NewContentType, List<string>> NewContent { set; get; }
33-
34-
private List<IEncapsulateFieldCandidate> _selectedCandidates;
35-
public IEnumerable<IEncapsulateFieldCandidate> SelectedFieldCandidates
36-
=> _selectedCandidates;
23+
public IReadOnlyCollection<IEncapsulateFieldCandidate> SelectedFieldCandidates { get; }
3724
}
3825
}
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,94 @@
1-
using Rubberduck.Parsing.Grammar;
2-
using Rubberduck.Parsing.Rewriter;
1+
using Rubberduck.Parsing.Rewriter;
32
using Rubberduck.Parsing.Symbols;
43
using Rubberduck.Parsing.VBA;
54
using Rubberduck.Refactorings.Common;
65
using Rubberduck.Resources;
7-
using Rubberduck.Refactorings.CodeBlockInsert;
86
using System;
97
using System.Diagnostics;
108
using System.Linq;
119
using Rubberduck.Refactorings.EncapsulateField;
10+
using System.Collections.Generic;
1211

1312
namespace Rubberduck.Refactorings.EncapsulateFieldInsertNewCode
1413
{
1514
public class EncapsulateFieldInsertNewCodeRefactoringAction : CodeOnlyRefactoringActionBase<EncapsulateFieldInsertNewCodeModel>
1615
{
17-
private const string FourSpaces = " ";
18-
16+
private readonly static string _doubleSpace = $"{Environment.NewLine}{Environment.NewLine}";
17+
private int? _codeSectionStartIndex;
1918
private readonly IDeclarationFinderProvider _declarationFinderProvider;
20-
private readonly IRewritingManager _rewritingManager;
21-
private readonly ICodeBuilder _codeBuilder;
22-
private readonly ICodeOnlyRefactoringAction<CodeBlockInsertModel> _codeBlockInsertRefactoringAction;
19+
private readonly IEncapsulateFieldCodeBuilderFactory _encapsulateFieldCodeBuilderFactory;
2320
public EncapsulateFieldInsertNewCodeRefactoringAction(
24-
CodeBlockInsertRefactoringAction codeBlockInsertRefactoringAction,
2521
IDeclarationFinderProvider declarationFinderProvider,
26-
IRewritingManager rewritingManager,
27-
ICodeBuilder codeBuilder)
22+
IRewritingManager rewritingManager,
23+
IEncapsulateFieldCodeBuilderFactory encapsulateFieldCodeBuilderFactory)
2824
: base(rewritingManager)
2925
{
3026
_declarationFinderProvider = declarationFinderProvider;
31-
_rewritingManager = rewritingManager;
32-
_codeBuilder = codeBuilder;
33-
_codeBlockInsertRefactoringAction = codeBlockInsertRefactoringAction;
27+
_encapsulateFieldCodeBuilderFactory = encapsulateFieldCodeBuilderFactory;
3428
}
3529

3630
public override void Refactor(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
3731
{
38-
var codeSectionStartIndex = _declarationFinderProvider.DeclarationFinder
32+
_codeSectionStartIndex = _declarationFinderProvider.DeclarationFinder
3933
.Members(model.QualifiedModuleName).Where(m => m.IsMember())
4034
.OrderBy(c => c.Selection)
4135
.FirstOrDefault()?.Context.Start.TokenIndex;
4236

43-
var codeBlockInsertModel = new CodeBlockInsertModel()
44-
{
45-
QualifiedModuleName = model.QualifiedModuleName,
46-
SelectedFieldCandidates = model.SelectedFieldCandidates,
47-
NewContent = model.NewContent,
48-
CodeSectionStartIndex = codeSectionStartIndex,
49-
IncludeComments = model.IncludeNewContentMarker
50-
};
37+
LoadNewPropertyBlocks(model, rewriteSession);
5138

52-
LoadNewPropertyBlocks(codeBlockInsertModel, _codeBuilder, rewriteSession);
39+
InsertBlocks(model, rewriteSession);
5340

54-
_codeBlockInsertRefactoringAction.Refactor(codeBlockInsertModel, rewriteSession);
41+
model.NewContentAggregator = null;
5542
}
5643

57-
public void LoadNewPropertyBlocks(CodeBlockInsertModel model, ICodeBuilder codeBuilder, IRewriteSession rewriteSession)
44+
public void LoadNewPropertyBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
5845
{
59-
if (model.IncludeComments)
60-
{
61-
model.AddContentBlock(NewContentType.PostContentMessage, RubberduckUI.EncapsulateField_PreviewMarker);
62-
}
63-
46+
var builder = _encapsulateFieldCodeBuilderFactory.Create();
6447
foreach (var propertyAttributes in model.SelectedFieldCandidates.SelectMany(f => f.PropertyAttributeSets))
6548
{
6649
Debug.Assert(propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.Variable) || propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.UserDefinedTypeMember));
6750

68-
LoadPropertyGetCodeBlock(model, propertyAttributes, codeBuilder);
69-
70-
if (propertyAttributes.GenerateLetter)
71-
{
72-
LoadPropertyLetCodeBlock(model, propertyAttributes, codeBuilder);
73-
}
51+
var (Get, Let, Set) = builder.BuildPropertyBlocks(propertyAttributes);
7452

75-
if (propertyAttributes.GenerateSetter)
76-
{
77-
LoadPropertySetCodeBlock(model, propertyAttributes, codeBuilder);
78-
}
53+
var blocks = new List<string>() { Get, Let, Set };
54+
blocks.ForEach(s => model.NewContentAggregator.AddNewContent(NewContentType.CodeSectionBlock, s));
7955
}
8056
}
8157

82-
private static void LoadPropertyLetCodeBlock(CodeBlockInsertModel model, PropertyAttributeSet propertyAttributes, ICodeBuilder codeBuilder)
58+
private void InsertBlocks(EncapsulateFieldInsertNewCodeModel model, IRewriteSession rewriteSession)
8359
{
84-
var letterContent = $"{FourSpaces}{propertyAttributes.BackingField} = {propertyAttributes.ParameterName}";
85-
if (!codeBuilder.TryBuildPropertyLetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out var propertyLet, content: letterContent))
60+
var newDeclarationSectionBlock = model.NewContentAggregator.RetrieveBlock(NewContentType.UserDefinedTypeDeclaration, NewContentType.DeclarationBlock, NewContentType.CodeSectionBlock);
61+
if (string.IsNullOrEmpty(newDeclarationSectionBlock))
8662
{
87-
throw new ArgumentException();
63+
return;
8864
}
89-
model.AddContentBlock(NewContentType.CodeSectionBlock, propertyLet);
90-
}
9165

92-
private static void LoadPropertySetCodeBlock(CodeBlockInsertModel model, PropertyAttributeSet propertyAttributes, ICodeBuilder codeBuilder)
93-
{
94-
var setterContent = $"{FourSpaces}{Tokens.Set} {propertyAttributes.BackingField} = {propertyAttributes.ParameterName}";
95-
if (!codeBuilder.TryBuildPropertySetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out var propertySet, content: setterContent))
66+
var allNewContent = string.Join(_doubleSpace, new string[] { newDeclarationSectionBlock });
67+
68+
var previewMarker = model.NewContentAggregator.RetrieveBlock(RubberduckUI.EncapsulateField_PreviewMarker);
69+
if (!string.IsNullOrEmpty(previewMarker))
9670
{
97-
throw new ArgumentException();
71+
allNewContent = $"{allNewContent}{Environment.NewLine}{previewMarker}";
9872
}
99-
model.AddContentBlock(NewContentType.CodeSectionBlock, propertySet);
73+
74+
var rewriter = rewriteSession.CheckOutModuleRewriter(model.QualifiedModuleName);
75+
76+
InsertBlock(allNewContent, _codeSectionStartIndex, rewriter);
10077
}
10178

102-
private static void LoadPropertyGetCodeBlock(CodeBlockInsertModel model, PropertyAttributeSet propertyAttributes, ICodeBuilder codeBuilder)
79+
private static void InsertBlock(string content, int? insertionIndex, IModuleRewriter rewriter)
10380
{
104-
var getterContent = $"{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}";
105-
if (propertyAttributes.UsesSetAssignment)
81+
if (string.IsNullOrEmpty(content))
10682
{
107-
getterContent = $"{Tokens.Set} {getterContent}";
83+
return;
10884
}
10985

110-
if (propertyAttributes.AsTypeName.Equals(Tokens.Variant) && !propertyAttributes.Declaration.IsArray)
86+
if (insertionIndex.HasValue)
11187
{
112-
getterContent = string.Join(Environment.NewLine,
113-
$"{Tokens.If} IsObject({propertyAttributes.BackingField}) {Tokens.Then}",
114-
$"{FourSpaces}{Tokens.Set} {propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
115-
Tokens.Else,
116-
$"{FourSpaces}{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
117-
$"{Tokens.End} {Tokens.If}",
118-
Environment.NewLine);
88+
rewriter.InsertBefore(insertionIndex.Value, $"{content}{_doubleSpace}");
89+
return;
11990
}
120-
121-
if (!codeBuilder.TryBuildPropertyGetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out var propertyGet, content: $"{FourSpaces}{getterContent}"))
122-
{
123-
throw new ArgumentException();
124-
}
125-
126-
model.AddContentBlock(NewContentType.CodeSectionBlock, propertyGet);
91+
rewriter.InsertBefore(rewriter.TokenStream.Size - 1, $"{_doubleSpace}{content}");
12792
}
12893
}
12994
}

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldRefactoringAction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Rubberduck.Parsing.VBA;
2-
using Rubberduck.Refactorings.EncapsulateFieldUseBackingField;
1+
using Rubberduck.Refactorings.EncapsulateFieldUseBackingField;
32
using Rubberduck.Refactorings.EncapsulateFieldUseBackingUDTMember;
43
using System.Linq;
54

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldRefactoringActionsProvider.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using Rubberduck.Parsing.Rewriter;
2-
using Rubberduck.Parsing.VBA;
3-
using Rubberduck.Refactorings.DeclareFieldsAsUDTMembers;
1+
using Rubberduck.Refactorings.DeclareFieldsAsUDTMembers;
42
using Rubberduck.Refactorings.ReplaceDeclarationIdentifier;
53
using Rubberduck.Refactorings.ReplaceReferences;
64
using Rubberduck.Refactorings.ReplacePrivateUDTMemberReferences;
7-
using Rubberduck.Refactorings.CodeBlockInsert;
85
using Rubberduck.Refactorings.EncapsulateFieldInsertNewCode;
96

107
namespace Rubberduck.Refactorings.EncapsulateField
@@ -14,7 +11,6 @@ public interface IEncapsulateFieldRefactoringActionsProvider
1411
ICodeOnlyRefactoringAction<ReplaceReferencesModel> ReplaceReferences { get; }
1512
ICodeOnlyRefactoringAction<ReplacePrivateUDTMemberReferencesModel> ReplaceUDTMemberReferences { get; }
1613
ICodeOnlyRefactoringAction<ReplaceDeclarationIdentifierModel> ReplaceDeclarationIdentifiers { get; }
17-
ICodeOnlyRefactoringAction<CodeBlockInsertModel> CodeBlockInsert { get; }
1814
ICodeOnlyRefactoringAction<DeclareFieldsAsUDTMembersModel> DeclareFieldsAsUDTMembers { get; }
1915
ICodeOnlyRefactoringAction<EncapsulateFieldInsertNewCodeModel> EncapsulateFieldInsertNewCode { get; }
2016
}
@@ -23,7 +19,6 @@ public class EncapsulateFieldRefactoringActionsProvider : IEncapsulateFieldRefac
2319
{
2420
private readonly ReplaceReferencesRefactoringAction _replaceReferences;
2521
private readonly ReplaceDeclarationIdentifierRefactoringAction _replaceDeclarationIdentifiers;
26-
private readonly CodeBlockInsertRefactoringAction _codeBlockInsertRefactoringAction;
2722
private readonly ReplacePrivateUDTMemberReferencesRefactoringAction _replaceUDTMemberReferencesRefactoringAction;
2823
private readonly DeclareFieldsAsUDTMembersRefactoringAction _declareFieldsAsUDTMembersRefactoringAction;
2924
private readonly EncapsulateFieldInsertNewCodeRefactoringAction _encapsulateFieldInsertNewCodeRefactoringAction;
@@ -33,14 +28,12 @@ public EncapsulateFieldRefactoringActionsProvider(
3328
ReplacePrivateUDTMemberReferencesRefactoringAction replaceUDTMemberReferencesRefactoringAction,
3429
ReplaceDeclarationIdentifierRefactoringAction replaceDeclarationIdentifierRefactoringAction,
3530
DeclareFieldsAsUDTMembersRefactoringAction declareFieldsAsUDTMembersRefactoringAction,
36-
EncapsulateFieldInsertNewCodeRefactoringAction encapsulateFieldInsertNewCodeRefactoringAction,
37-
CodeBlockInsertRefactoringAction codeBlockInsertRefactoringAction)
31+
EncapsulateFieldInsertNewCodeRefactoringAction encapsulateFieldInsertNewCodeRefactoringAction)
3832
{
3933
_replaceReferences = replaceReferencesRefactoringAction;
4034
_replaceUDTMemberReferencesRefactoringAction = replaceUDTMemberReferencesRefactoringAction;
4135
_replaceDeclarationIdentifiers = replaceDeclarationIdentifierRefactoringAction;
4236
_declareFieldsAsUDTMembersRefactoringAction = declareFieldsAsUDTMembersRefactoringAction;
43-
_codeBlockInsertRefactoringAction = codeBlockInsertRefactoringAction;
4437
_encapsulateFieldInsertNewCodeRefactoringAction = encapsulateFieldInsertNewCodeRefactoringAction;
4538
}
4639

@@ -50,9 +43,6 @@ public ICodeOnlyRefactoringAction<ReplaceReferencesModel> ReplaceReferences
5043
public ICodeOnlyRefactoringAction<ReplaceDeclarationIdentifierModel> ReplaceDeclarationIdentifiers
5144
=> _replaceDeclarationIdentifiers;
5245

53-
public ICodeOnlyRefactoringAction<CodeBlockInsertModel> CodeBlockInsert
54-
=> _codeBlockInsertRefactoringAction;
55-
5646
public ICodeOnlyRefactoringAction<ReplacePrivateUDTMemberReferencesModel> ReplaceUDTMemberReferences
5747
=> _replaceUDTMemberReferencesRefactoringAction;
5848

Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using Rubberduck.Parsing.VBA;
43
using Rubberduck.VBEditor;
5-
using Rubberduck.Refactorings.CodeBlockInsert;
64
using Rubberduck.Refactorings.EncapsulateField;
75

86
namespace Rubberduck.Refactorings.EncapsulateFieldUseBackingField
97
{
108
public class EncapsulateFieldUseBackingFieldModel : IRefactoringModel
119
{
12-
private readonly IDeclarationFinderProvider _declarationFinderProvider;
13-
14-
public EncapsulateFieldUseBackingFieldModel(IEnumerable<IEncapsulateFieldCandidate> candidates,
15-
IDeclarationFinderProvider declarationFinderProvider)
10+
public EncapsulateFieldUseBackingFieldModel(IEnumerable<IEncapsulateFieldCandidate> candidates)
1611
{
17-
_declarationFinderProvider = declarationFinderProvider;
18-
1912
EncapsulationCandidates = candidates.ToList();
20-
21-
ResetNewContent();
22-
}
23-
24-
public void ResetNewContent()
25-
{
26-
NewContent = new Dictionary<NewContentType, List<string>>
13+
if (EncapsulationCandidates.Any())
2714
{
28-
{ NewContentType.PostContentMessage, new List<string>() },
29-
{ NewContentType.DeclarationBlock, new List<string>() },
30-
{ NewContentType.CodeSectionBlock, new List<string>() },
31-
{ NewContentType.TypeDeclarationBlock, new List<string>() }
32-
};
15+
QualifiedModuleName = EncapsulationCandidates.First().QualifiedModuleName;
16+
}
3317
}
3418

35-
public IEncapsulateFieldConflictFinder ConflictFinder { set; get; }
36-
37-
public bool IncludeNewContentMarker { set; get; } = false;
19+
public INewContentAggregator NewContentAggregator { set; get; }
3820

39-
public List<IEncapsulateFieldCandidate> EncapsulationCandidates { get; }
40-
41-
public IEnumerable<IEncapsulateFieldCandidate> SelectedFieldCandidates
42-
=> EncapsulationCandidates.Where(v => v.EncapsulateFlag);
21+
public IEncapsulateFieldConflictFinder ConflictFinder { set; get; }
4322

44-
public void AddContentBlock(NewContentType contentType, string block)
45-
=> NewContent[contentType].Add(block);
23+
public IReadOnlyCollection<IEncapsulateFieldCandidate> EncapsulationCandidates { get; }
4624

47-
public Dictionary<NewContentType, List<string>> NewContent { set; get; }
25+
public IReadOnlyCollection<IEncapsulateFieldCandidate> SelectedFieldCandidates
26+
=> EncapsulationCandidates.Where(c => c.EncapsulateFlag).ToList();
4827

49-
public QualifiedModuleName QualifiedModuleName => EncapsulationCandidates.First().QualifiedModuleName;
28+
public QualifiedModuleName QualifiedModuleName { get; } = new QualifiedModuleName();
5029
}
5130
}

0 commit comments

Comments
 (0)