1
1
using Rubberduck . Parsing ;
2
2
using Rubberduck . Parsing . Grammar ;
3
3
using Rubberduck . Parsing . Symbols ;
4
+ using Rubberduck . SmartIndenter ;
4
5
using System ;
5
6
using System . Collections . Generic ;
6
7
using System . Linq ;
@@ -35,7 +36,7 @@ string BuildMemberBlockFromPrototype(ModuleBodyElementDeclaration declaration,
35
36
/// Generates a Property Get codeblock based on the prototype declaration
36
37
/// </summary>
37
38
/// <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>
39
40
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
40
41
bool TryBuildPropertyGetCodeBlock ( Declaration prototype ,
41
42
string propertyIdentifier ,
@@ -47,7 +48,7 @@ bool TryBuildPropertyGetCodeBlock(Declaration prototype,
47
48
/// Generates a Property Let codeblock based on the prototype declaration
48
49
/// </summary>
49
50
/// <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>
51
52
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
52
53
bool TryBuildPropertyLetCodeBlock ( Declaration prototype ,
53
54
string propertyIdentifier ,
@@ -60,7 +61,7 @@ bool TryBuildPropertyLetCodeBlock(Declaration prototype,
60
61
/// Generates a Property Set codeblock based on the prototype declaration
61
62
/// </summary>
62
63
/// <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>
64
65
/// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
65
66
bool TryBuildPropertySetCodeBlock ( Declaration prototype ,
66
67
string propertyIdentifier ,
@@ -73,27 +74,36 @@ bool TryBuildPropertySetCodeBlock(Declaration prototype,
73
74
/// Generates a UserDefinedType (UDT) declaration using the prototype declarations for
74
75
/// creating the UserDefinedTypeMember declarations.
75
76
/// </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.
77
79
/// </remarks>
78
80
/// <param name="memberPrototypes">DeclarationTypes with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
79
81
bool TryBuildUserDefinedTypeDeclaration ( string udtIdentifier , IEnumerable < ( Declaration Prototype , string UDTMemberIdentifier ) > memberPrototypes , out string declaration , Accessibility accessibility = Accessibility . Private ) ;
80
82
81
83
/// <summary>
82
84
/// Generates a <c>UserDefinedTypeMember</c> declaration expression based on the prototype declaration
83
85
/// </summary>
86
+ /// <remarks>
87
+ /// No validation or conflict analysis is applied to the identifiers.
88
+ /// </remarks>
84
89
/// <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 ) ;
87
91
}
88
92
89
93
public class CodeBuilder : ICodeBuilder
90
94
{
95
+ private readonly IIndenter _indenter ;
96
+
97
+ public CodeBuilder ( IIndenter indenter )
98
+ {
99
+ _indenter = indenter ;
100
+ }
101
+
91
102
public string BuildMemberBlockFromPrototype ( ModuleBodyElementDeclaration declaration ,
92
103
string content = null ,
93
104
string accessibility = null ,
94
105
string newIdentifier = null )
95
106
{
96
-
97
107
var elements = new List < string > ( )
98
108
{
99
109
ImprovedFullMemberSignatureInternal ( declaration , accessibility , newIdentifier ) ,
@@ -117,12 +127,8 @@ public bool TryBuildPropertySetCodeBlock(Declaration prototype, string propertyI
117
127
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
118
128
{
119
129
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 ) )
126
132
{
127
133
return false ;
128
134
}
@@ -144,6 +150,9 @@ private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType let
144
150
codeBlock = letSetGetType . HasFlag ( DeclarationType . PropertyGet )
145
151
? string . Join ( Environment . NewLine , $ "{ accessibility ?? Tokens . Public } { TypeToken ( letSetGetType ) } { propertyIdentifier } () { asTypeClause } ", content , EndStatement ( letSetGetType ) )
146
152
: 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
+
147
156
return true ;
148
157
}
149
158
@@ -168,8 +177,8 @@ private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration
168
177
$ "({ ImprovedArgumentList ( declaration ) } )",
169
178
asTypeName
170
179
} ;
171
- return string . Concat ( elements ) . Trim ( ) ;
172
180
181
+ return string . Concat ( elements ) . Trim ( ) ;
173
182
}
174
183
175
184
public string ImprovedArgumentList ( ModuleBodyElementDeclaration declaration )
@@ -185,6 +194,7 @@ public string ImprovedArgumentList(ModuleBodyElementDeclaration declaration)
185
194
&& declaration . DeclarationType . HasFlag ( DeclarationType . Property )
186
195
&& ! declaration . DeclarationType . Equals ( DeclarationType . PropertyGet ) ) ) ;
187
196
}
197
+
188
198
return $ "{ string . Join ( ", " , arguments ) } ";
189
199
}
190
200
@@ -265,11 +275,12 @@ public bool TryBuildUserDefinedTypeDeclaration(string udtIdentifier, IEnumerable
265
275
266
276
blockLines . Add ( $ "{ Tokens . End } { Tokens . Type } ") ;
267
277
268
- declaration = string . Join ( Environment . NewLine , blockLines ) ;
278
+ declaration = string . Join ( Environment . NewLine , _indenter . Indent ( blockLines ) ) ;
279
+
269
280
return true ;
270
281
}
271
282
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 )
273
284
{
274
285
declaration = string . Empty ;
275
286
@@ -280,11 +291,11 @@ public bool TryBuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration
280
291
return false ;
281
292
}
282
293
283
- declaration = BuildUDTMemberDeclaration ( udtMemberIdentifier , prototype , indentation ) ;
294
+ declaration = BuildUDTMemberDeclaration ( udtMemberIdentifier , prototype ) ;
284
295
return true ;
285
296
}
286
297
287
- private static string BuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype , string indentation = null )
298
+ private static string BuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype )
288
299
{
289
300
var identifierExpression = udtMemberIdentifier ;
290
301
if ( prototype . IsArray )
@@ -294,7 +305,7 @@ private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Decl
294
305
: $ "{ udtMemberIdentifier } ()";
295
306
}
296
307
297
- return $ "{ indentation ?? " " } { identifierExpression } { Tokens . As } { prototype . AsTypeName } ";
308
+ return $ "{ identifierExpression } { Tokens . As } { prototype . AsTypeName } ";
298
309
}
299
310
300
311
private static bool IsValidPrototypeDeclarationType ( DeclarationType declarationType )
0 commit comments