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,8 @@ 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. Null results in an empty body. Formatting is the responsibility of the caller</param>
39
+ /// <param name="content">Member body content.</param>
40
+ /// <param name="parameterIdentifier">Defaults to '<paramref name="propertyIdentifier"/>Value' unless otherwise specified</param>
39
41
bool TryBuildPropertyGetCodeBlock ( Declaration prototype ,
40
42
string propertyIdentifier ,
41
43
out string codeBlock ,
@@ -46,7 +48,7 @@ bool TryBuildPropertyGetCodeBlock(Declaration prototype,
46
48
/// Generates a Property Let codeblock based on the prototype declaration
47
49
/// </summary>
48
50
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
49
- /// <param name="content">Member body content. Null results in an empty body. Formatting is the responsibility of the caller </param>
51
+ /// <param name="content">Member body content.</param>
50
52
/// <param name="parameterIdentifier">Defaults to 'RHS' unless otherwise specified</param>
51
53
bool TryBuildPropertyLetCodeBlock ( Declaration prototype ,
52
54
string propertyIdentifier ,
@@ -59,7 +61,7 @@ bool TryBuildPropertyLetCodeBlock(Declaration prototype,
59
61
/// Generates a Property Set codeblock based on the prototype declaration
60
62
/// </summary>
61
63
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
62
- /// <param name="content">Member body content. Null results in an empty body. Formatting is the responsibility of the caller </param>
64
+ /// <param name="content">Member body content.</param>
63
65
/// <param name="parameterIdentifier">Defaults to 'RHS' unless otherwise specified</param>
64
66
bool TryBuildPropertySetCodeBlock ( Declaration prototype ,
65
67
string propertyIdentifier ,
@@ -72,27 +74,36 @@ bool TryBuildPropertySetCodeBlock(Declaration prototype,
72
74
/// Generates a UserDefinedType (UDT) declaration using the prototype declarations for
73
75
/// creating the UserDefinedTypeMember declarations.
74
76
/// </summary>
75
- /// <remarks>No validation or conflict analysis is applied to the identifiers.
77
+ /// <remarks>
78
+ /// No validation or conflict analysis is applied to the identifiers.
76
79
/// </remarks>
77
80
/// <param name="memberPrototypes">DeclarationTypes with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
78
81
bool TryBuildUserDefinedTypeDeclaration ( string udtIdentifier , IEnumerable < ( Declaration Prototype , string UDTMemberIdentifier ) > memberPrototypes , out string declaration , Accessibility accessibility = Accessibility . Private ) ;
79
82
80
83
/// <summary>
81
84
/// Generates a <c>UserDefinedTypeMember</c> declaration expression based on the prototype declaration
82
85
/// </summary>
86
+ /// <remarks>
87
+ /// No validation or conflict analysis is applied to the identifiers.
88
+ /// </remarks>
83
89
/// <param name="prototype">DeclarationType with flags: Variable, Constant, UserDefinedTypeMember, or Function</param>
84
- /// <param name="indentation">If left null, 4 spaces of indentation are applied</param>
85
- bool TryBuildUDTMemberDeclaration ( string identifier , Declaration prototype , out string declaration , string indentation = null ) ;
90
+ bool TryBuildUDTMemberDeclaration ( string identifier , Declaration prototype , out string declaration ) ;
86
91
}
87
92
88
93
public class CodeBuilder : ICodeBuilder
89
94
{
95
+ private readonly IIndenter _indenter ;
96
+
97
+ public CodeBuilder ( IIndenter indenter )
98
+ {
99
+ _indenter = indenter ;
100
+ }
101
+
90
102
public string BuildMemberBlockFromPrototype ( ModuleBodyElementDeclaration declaration ,
91
103
string content = null ,
92
104
Accessibility accessibility = Accessibility . Public ,
93
105
string newIdentifier = null )
94
106
{
95
-
96
107
var elements = new List < string > ( )
97
108
{
98
109
ImprovedFullMemberSignatureInternal ( declaration , accessibility , newIdentifier ) ,
@@ -116,12 +127,8 @@ public bool TryBuildPropertySetCodeBlock(Declaration prototype, string propertyI
116
127
private bool TryBuildPropertyBlockFromTarget < T > ( T prototype , DeclarationType letSetGetType , string propertyIdentifier , out string codeBlock , Accessibility accessibility , string content = null , string parameterIdentifier = null ) where T : Declaration
117
128
{
118
129
codeBlock = string . Empty ;
119
- if ( ! letSetGetType . HasFlag ( DeclarationType . Property ) )
120
- {
121
- throw new ArgumentException ( ) ;
122
- }
123
-
124
- if ( ! IsValidPrototypeDeclarationType ( prototype . DeclarationType ) )
130
+ if ( ! letSetGetType . HasFlag ( DeclarationType . Property )
131
+ || ! IsValidPrototypeDeclarationType ( prototype . DeclarationType ) )
125
132
{
126
133
return false ;
127
134
}
@@ -143,6 +150,8 @@ private bool TryBuildPropertyBlockFromTarget<T>(T prototype, DeclarationType let
143
150
codeBlock = letSetGetType . HasFlag ( DeclarationType . PropertyGet )
144
151
? string . Join ( Environment . NewLine , $ "{ AccessibilityToken ( accessibility ) } { TypeToken ( letSetGetType ) } { propertyIdentifier } () { asTypeClause } ", content , EndStatement ( letSetGetType ) )
145
152
: string . Join ( Environment . NewLine , $ "{ AccessibilityToken ( accessibility ) } { TypeToken ( letSetGetType ) } { propertyIdentifier } ({ letSetParamExpression } )", content , EndStatement ( letSetGetType ) ) ;
153
+
154
+ codeBlock = string . Join ( Environment . NewLine , _indenter . Indent ( codeBlock ) ) ;
146
155
return true ;
147
156
}
148
157
@@ -163,8 +172,8 @@ private string ImprovedFullMemberSignatureInternal(ModuleBodyElementDeclaration
163
172
$ "({ ImprovedArgumentList ( declaration ) } )",
164
173
asTypeName
165
174
} ;
166
- return string . Concat ( elements ) . Trim ( ) ;
167
175
176
+ return string . Concat ( elements ) . Trim ( ) ;
168
177
}
169
178
170
179
public string ImprovedArgumentList ( ModuleBodyElementDeclaration declaration )
@@ -180,6 +189,7 @@ public string ImprovedArgumentList(ModuleBodyElementDeclaration declaration)
180
189
&& declaration . DeclarationType . HasFlag ( DeclarationType . Property )
181
190
&& ! declaration . DeclarationType . Equals ( DeclarationType . PropertyGet ) ) ) ;
182
191
}
192
+
183
193
return $ "{ string . Join ( ", " , arguments ) } ";
184
194
}
185
195
@@ -260,11 +270,12 @@ public bool TryBuildUserDefinedTypeDeclaration(string udtIdentifier, IEnumerable
260
270
261
271
blockLines . Add ( $ "{ Tokens . End } { Tokens . Type } ") ;
262
272
263
- declaration = string . Join ( Environment . NewLine , blockLines ) ;
273
+ declaration = string . Join ( Environment . NewLine , _indenter . Indent ( blockLines ) ) ;
274
+
264
275
return true ;
265
276
}
266
277
267
- public bool TryBuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype , out string declaration , string indentation = null )
278
+ public bool TryBuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype , out string declaration )
268
279
{
269
280
declaration = string . Empty ;
270
281
@@ -275,11 +286,11 @@ public bool TryBuildUDTMemberDeclaration(string udtMemberIdentifier, Declaration
275
286
return false ;
276
287
}
277
288
278
- declaration = BuildUDTMemberDeclaration ( udtMemberIdentifier , prototype , indentation ) ;
289
+ declaration = BuildUDTMemberDeclaration ( udtMemberIdentifier , prototype ) ;
279
290
return true ;
280
291
}
281
292
282
- private static string BuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype , string indentation = null )
293
+ private static string BuildUDTMemberDeclaration ( string udtMemberIdentifier , Declaration prototype )
283
294
{
284
295
var identifierExpression = udtMemberIdentifier ;
285
296
if ( prototype . IsArray )
@@ -289,7 +300,7 @@ private static string BuildUDTMemberDeclaration(string udtMemberIdentifier, Decl
289
300
: $ "{ udtMemberIdentifier } ()";
290
301
}
291
302
292
- return $ "{ indentation ?? " " } { identifierExpression } { Tokens . As } { prototype . AsTypeName } ";
303
+ return $ "{ identifierExpression } { Tokens . As } { prototype . AsTypeName } ";
293
304
}
294
305
295
306
private static string AccessibilityToken ( Accessibility accessibility )
0 commit comments