Skip to content

Commit 361948a

Browse files
committed
Modify CodeBuilder to use Indenter settings
1 parent 4281486 commit 361948a

File tree

13 files changed

+279
-133
lines changed

13 files changed

+279
-133
lines changed

Rubberduck.Refactorings/Common/CodeBuilder.cs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Rubberduck.Parsing;
22
using Rubberduck.Parsing.Grammar;
33
using Rubberduck.Parsing.Symbols;
4+
using Rubberduck.SmartIndenter;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -35,7 +36,7 @@ string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
3536
/// Generates a Property Get codeblock based on the prototype declaration
3637
/// </summary>
3738
/// <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="content">Member body content.</param>
3940
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
4041
bool TryBuildPropertyGetCodeBlock(Declaration prototype,
4142
string propertyIdentifier,
@@ -47,7 +48,7 @@ bool TryBuildPropertyGetCodeBlock(Declaration prototype,
4748
/// Generates a Property Let codeblock based on the prototype declaration
4849
/// </summary>
4950
/// <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="content">Member body content.</param>
5152
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
5253
bool TryBuildPropertyLetCodeBlock(Declaration prototype,
5354
string propertyIdentifier,
@@ -60,7 +61,7 @@ bool TryBuildPropertyLetCodeBlock(Declaration prototype,
6061
/// Generates a Property Set codeblock based on the prototype declaration
6162
/// </summary>
6263
/// <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="content">Member body content.</param>
6465
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
6566
bool TryBuildPropertySetCodeBlock(Declaration prototype,
6667
string propertyIdentifier,
@@ -73,27 +74,36 @@ bool TryBuildPropertySetCodeBlock(Declaration prototype,
7374
/// Generates a UserDefinedType (UDT) declaration using the prototype declarations for
7475
/// creating the UserDefinedTypeMember declarations.
7576
/// </summary>
76-
/// <remarks>No validation or conflict analysis is applied to the identifiers.
77+
/// <remarks>
78+
/// No validation or conflict analysis is applied to the identifiers.
7779
/// </remarks>
7880
/// <param name="memberPrototypes">DeclarationTypes with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
7981
bool TryBuildUserDefinedTypeDeclaration(string udtIdentifier, IEnumerable<(Declaration Prototype, string UDTMemberIdentifier)> memberPrototypes, out string declaration, Accessibility accessibility = Accessibility.Private);
8082

8183
/// <summary>
8284
/// Generates a <c>UserDefinedTypeMember</c> declaration expression based on the prototype declaration
8385
/// </summary>
86+
/// <remarks>
87+
/// No validation or conflict analysis is applied to the identifiers.
88+
/// </remarks>
8489
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
85-
/// <param name="indentation">Defaults is 4 spaces</param>
86-
bool TryBuildUDTMemberDeclaration(string identifier, Declaration prototype, out string declaration, string indentation = null);
90+
bool TryBuildUDTMemberDeclaration(string identifier, Declaration prototype, out string declaration);
8791
}
8892

8993
public class CodeBuilder : ICodeBuilder
9094
{
95+
private readonly IIndenter _indenter;
96+
97+
public CodeBuilder(IIndenter indenter)
98+
{
99+
_indenter = indenter;
100+
}
101+
91102
public string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
92103
string content = null,
93104
string accessibility = null,
94105
string newIdentifier = null)
95106
{
96-
97107
var elements = new List<string>()
98108
{
99109
ImprovedFullMemberSignatureInternal(declaration, accessibility, newIdentifier),
@@ -117,12 +127,8 @@ public bool TryBuildPropertySetCodeBlock(Declaration prototype, string propertyI
117127
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
118128
{
119129
codeBlock = string.Empty;
120-
if (!letSetGetType.HasFlag(DeclarationType.Property))
121-
{
122-
throw new ArgumentException();
123-
}
124-
125-
if (!IsValidPrototypeDeclarationType(prototype.DeclarationType))
130+
if (!letSetGetType.HasFlag(DeclarationType.Property)
131+
|| !IsValidPrototypeDeclarationType(prototype.DeclarationType))
126132
{
127133
return false;
128134
}
@@ -144,6 +150,9 @@ private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType let
144150
codeBlock = letSetGetType.HasFlag(DeclarationType.PropertyGet)
145151
? string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {TypeToken(letSetGetType)} {propertyIdentifier}() {asTypeClause}", content, EndStatement(letSetGetType))
146152
: string.Join(Environment.NewLine, $"{accessibility ?? Tokens.Public} {TypeToken(letSetGetType)} {propertyIdentifier}({letSetParamExpression})", content, EndStatement(letSetGetType));
153+
154+
codeBlock = string.Join(Environment.NewLine, _indenter.Indent(codeBlock));
155+
147156
return true;
148157
}
149158

@@ -168,8 +177,8 @@ private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration
168177
$"({ImprovedArgumentList(declaration)})",
169178
asTypeName
170179
};
171-
return string.Concat(elements).Trim();
172180

181+
return string.Concat(elements).Trim();
173182
}
174183

175184
public string ImprovedArgumentList(ModuleBodyElementDeclaration declaration)
@@ -185,6 +194,7 @@ public string ImprovedArgumentList(ModuleBodyElementDeclaration declaration)
185194
&& declaration.DeclarationType.HasFlag(DeclarationType.Property)
186195
&& !declaration.DeclarationType.Equals(DeclarationType.PropertyGet)));
187196
}
197+
188198
return $"{string.Join(", ", arguments)}";
189199
}
190200

@@ -265,11 +275,12 @@ public bool TryBuildUserDefinedTypeDeclaration(string udtIdentifier, IEnumerable
265275

266276
blockLines.Add($"{Tokens.End} {Tokens.Type}");
267277

268-
declaration = string.Join(Environment.NewLine, blockLines);
278+
declaration = string.Join(Environment.NewLine, _indenter.Indent(blockLines));
279+
269280
return true;
270281
}
271282

272-
public bool TryBuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration prototype, out string declaration, string indentation = null)
283+
public bool TryBuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration prototype, out string declaration)
273284
{
274285
declaration = string.Empty;
275286

@@ -280,11 +291,11 @@ public bool TryBuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration
280291
return false;
281292
}
282293

283-
declaration = BuildUDTMemberDeclaration(udtMemberIdentifier, prototype, indentation);
294+
declaration = BuildUDTMemberDeclaration(udtMemberIdentifier, prototype);
284295
return true;
285296
}
286297

287-
private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration prototype, string indentation = null)
298+
private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration prototype)
288299
{
289300
var identifierExpression = udtMemberIdentifier;
290301
if (prototype.IsArray)
@@ -294,7 +305,7 @@ private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Decl
294305
: $"{udtMemberIdentifier}()";
295306
}
296307

297-
return $"{indentation ?? " "}{identifierExpression} {Tokens.As} {prototype.AsTypeName}";
308+
return $"{identifierExpression} {Tokens.As} {prototype.AsTypeName}";
298309
}
299310

300311
private static bool IsValidPrototypeDeclarationType(DeclarationType declarationType)

Rubberduck.Refactorings/CreateUDTMember/CreateUDTMemberRefactoringAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ private IEnumerable<string> GenerateUserDefinedMemberDeclarations(IEnumerable<(D
7070
var declarations = new List<string>();
7171
foreach (var (Prototype, UDTMemberIdentifier) in newMemberPairs)
7272
{
73-
if (_codeBuilder.TryBuildUDTMemberDeclaration(UDTMemberIdentifier, Prototype, out var declaration, indentation))
73+
if (_codeBuilder.TryBuildUDTMemberDeclaration(UDTMemberIdentifier, Prototype, out var declaration))
7474
{
75-
declarations.Add(declaration);
75+
declarations.Add($"{indentation}{declaration}");
7676
}
7777
}
7878
return declarations;

Rubberduck.Refactorings/EncapsulateField/EncapsulateFieldInsertNewCode/EncapsulateFieldCodeBuilder.cs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public interface IEncapsulateFieldCodeBuilder
2020
/// </summary>
2121
public class EncapsulateFieldCodeBuilder : IEncapsulateFieldCodeBuilder
2222
{
23-
private const string FourSpaces = " ";
24-
private static string _doubleSpace = $"{Environment.NewLine}{Environment.NewLine}";
25-
2623
private readonly ICodeBuilder _codeBuilder;
2724

2825
public EncapsulateFieldCodeBuilder(ICodeBuilder codeBuilder)
@@ -32,68 +29,60 @@ public EncapsulateFieldCodeBuilder(ICodeBuilder codeBuilder)
3229

3330
public (string Get, string Let, string Set) BuildPropertyBlocks(PropertyAttributeSet propertyAttributes)
3431
{
35-
string propertyLet = null;
36-
string propertySet = null;
32+
if (!(propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.Variable)
33+
|| propertyAttributes.Declaration.DeclarationType.HasFlag(DeclarationType.UserDefinedTypeMember)))
34+
{
35+
throw new ArgumentException("Invalid prototype DeclarationType", nameof(propertyAttributes));
36+
}
37+
38+
(string Get, string Let, string Set) blocks = (string.Empty, string.Empty, string.Empty);
39+
40+
var mutatorBody = $"{propertyAttributes.BackingField} = {propertyAttributes.RHSParameterIdentifier}";
3741

3842
if (propertyAttributes.GeneratePropertyLet)
3943
{
40-
var letterContent = $"{FourSpaces}{propertyAttributes.BackingField} = {propertyAttributes.RHSParameterIdentifier}";
41-
if (!_codeBuilder.TryBuildPropertyLetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out propertyLet, content: letterContent))
42-
{
43-
throw new ArgumentException();
44-
}
44+
_codeBuilder.TryBuildPropertyLetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out blocks.Let, content: mutatorBody);
4545
}
4646

4747
if (propertyAttributes.GeneratePropertySet)
4848
{
49-
var setterContent = $"{FourSpaces}{Tokens.Set} {propertyAttributes.BackingField} = {propertyAttributes.RHSParameterIdentifier}";
50-
if (!_codeBuilder.TryBuildPropertySetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out propertySet, content: setterContent))
51-
{
52-
throw new ArgumentException();
53-
}
49+
_codeBuilder.TryBuildPropertySetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out blocks.Set, content: $"{Tokens.Set} {mutatorBody}");
5450
}
5551

56-
var getterContent = $"{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}";
57-
if (propertyAttributes.UsesSetAssignment)
58-
{
59-
getterContent = $"{Tokens.Set} {getterContent}";
60-
}
52+
var propertyGetBody = propertyAttributes.UsesSetAssignment
53+
? $"{Tokens.Set} {propertyAttributes.PropertyName} = {propertyAttributes.BackingField}"
54+
: $"{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}";
6155

6256
if (propertyAttributes.AsTypeName.Equals(Tokens.Variant) && !propertyAttributes.Declaration.IsArray)
6357
{
64-
getterContent = string.Join(Environment.NewLine,
58+
propertyGetBody = string.Join(
6559
$"{Tokens.If} IsObject({propertyAttributes.BackingField}) {Tokens.Then}",
66-
$"{FourSpaces}{Tokens.Set} {propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
60+
$"{Tokens.Set} {propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
6761
Tokens.Else,
68-
$"{FourSpaces}{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
69-
$"{Tokens.End} {Tokens.If}",
70-
Environment.NewLine);
62+
$"{propertyAttributes.PropertyName} = {propertyAttributes.BackingField}",
63+
$"{Tokens.End} {Tokens.If}");
7164
}
7265

73-
if (!_codeBuilder.TryBuildPropertyGetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out var propertyGet, content: $"{FourSpaces}{getterContent}"))
74-
{
75-
throw new ArgumentException();
76-
}
66+
_codeBuilder.TryBuildPropertyGetCodeBlock(propertyAttributes.Declaration, propertyAttributes.PropertyName, out blocks.Get, content: propertyGetBody);
7767

78-
return (propertyGet, propertyLet, propertySet);
68+
return (blocks.Get, blocks.Let, blocks.Set);
7969
}
8070

8171
public string BuildUserDefinedTypeDeclaration(IObjectStateUDT objectStateUDT, IEnumerable<IEncapsulateFieldCandidate> candidates)
8272
{
83-
var selected = candidates.Where(c => c.EncapsulateFlag);
84-
85-
var newUDTMembers = selected
73+
var newUDTMembers = candidates.Where(c => c.EncapsulateFlag)
8674
.Select(m => (m.Declaration, m.BackingIdentifier));
8775

88-
_codeBuilder.TryBuildUserDefinedTypeDeclaration(objectStateUDT.AsTypeName, newUDTMembers, out var declaration);
76+
if (_codeBuilder.TryBuildUserDefinedTypeDeclaration(objectStateUDT.AsTypeName, newUDTMembers, out var declaration))
77+
{
78+
return declaration;
79+
}
8980

90-
return declaration ?? string.Empty;
81+
return string.Empty;
9182
}
9283

93-
public string BuildObjectStateFieldDeclaration(IObjectStateUDT objectStateUDT)
94-
{
95-
return $"{Accessibility.Private} {objectStateUDT.IdentifierName} {Tokens.As} {objectStateUDT.AsTypeName}";
96-
}
84+
public string BuildObjectStateFieldDeclaration(IObjectStateUDT objectStateUDT)
85+
=> $"{Accessibility.Private} {objectStateUDT.IdentifierName} {Tokens.As} {objectStateUDT.AsTypeName}";
9786

9887
public string BuildFieldDeclaration(Declaration target, string identifier)
9988
{

0 commit comments

Comments
 (0)