2
2
using System . CommandLine . Invocation ;
3
3
using Microsoft . Build . Locator ;
4
4
using Microsoft . CodeAnalysis ;
5
+ using Microsoft . CodeAnalysis . CSharp ;
5
6
using Microsoft . CodeAnalysis . CSharp . Syntax ;
6
7
using Microsoft . CodeAnalysis . MSBuild ;
7
8
using TypealizR . CLI . Abstractions ;
@@ -144,22 +145,29 @@ private static async Task ExportAsync(IndentableWriter writer, string baseDirect
144
145
}
145
146
}
146
147
147
- private static IEnumerable < BaseNamespaceDeclarationSyntax > FindNamespaces ( Compilation compilation , CancellationToken cancellationToken ) => compilation . SyntaxTrees
148
- . Where ( x => x . GetRoot ( ) is CompilationUnitSyntax )
149
- . Select ( x => ( CompilationUnitSyntax ) x . GetRoot ( cancellationToken ) )
150
- . SelectMany ( x => x . Members . OfType < BaseNamespaceDeclarationSyntax > ( ) )
148
+ private static IEnumerable < BaseNamespaceDeclarationSyntax > FindNamespaces ( Compilation compilation , CancellationToken cancellationToken )
149
+ => compilation . SyntaxTrees
150
+ . Where ( x => x . GetRoot ( ) is CompilationUnitSyntax )
151
+ . Select ( x => ( CompilationUnitSyntax ) x . GetRoot ( cancellationToken ) )
152
+ . SelectMany ( x => x . Members . OfType < BaseNamespaceDeclarationSyntax > ( ) )
151
153
;
152
154
153
- private static IEnumerable < InterfaceInfo > FindInterfaces ( Compilation compilation , IEnumerable < BaseNamespaceDeclarationSyntax > allNamespaces , CancellationToken cancellationToken ) => allNamespaces
154
- . SelectMany ( x => GetAllInterfaceDeclarations ( x . Members ) )
155
- . Select ( x => new { Declaration = x , Model = compilation . GetSemanticModel ( x . SyntaxTree ) } )
156
- . Select ( x => new { x . Declaration , Symbol = x . Model . GetDeclaredSymbol ( x . Declaration , cancellationToken ) } )
157
- . Where ( x => x . Symbol is not null )
158
- . Select ( x => new InterfaceInfo ( x . Declaration , x . Symbol ! ) )
159
- . Where ( x => x . Symbol
160
- . GetAttributes ( )
161
- . Any ( x => x . AttributeClass is not null && x . AttributeClass ! . Name . StartsWith ( MarkerAttributeName , StringComparison . Ordinal ) )
162
- )
155
+ private static IEnumerable < InterfaceInfo > FindInterfaces
156
+ (
157
+ Compilation compilation ,
158
+ IEnumerable < BaseNamespaceDeclarationSyntax > allNamespaces ,
159
+ CancellationToken cancellationToken
160
+ )
161
+ => allNamespaces
162
+ . SelectMany ( x => GetAllInterfaceDeclarations ( x . Members ) )
163
+ . Select ( x => new { Declaration = x , Model = compilation . GetSemanticModel ( x . SyntaxTree ) } )
164
+ . Select ( x => new { x . Declaration , Symbol = x . Model . GetDeclaredSymbol ( x . Declaration , cancellationToken ) } )
165
+ . Where ( x => x . Symbol is not null )
166
+ . Select ( x => new InterfaceInfo ( x . Declaration , x . Symbol ! ) )
167
+ . Where ( x => x . Symbol
168
+ . GetAttributes ( )
169
+ . Any ( x => x . AttributeClass is not null && x . AttributeClass ! . Name . StartsWith ( MarkerAttributeName , StringComparison . Ordinal ) )
170
+ )
163
171
;
164
172
165
173
private static IEnumerable < InterfaceDeclarationSyntax > GetAllInterfaceDeclarations ( SyntaxList < MemberDeclarationSyntax > members )
@@ -180,7 +188,12 @@ private static IEnumerable<InterfaceDeclarationSyntax> GetAllInterfaceDeclaratio
180
188
}
181
189
}
182
190
183
- private static IEnumerable < TypeInfo > FindClasses ( Compilation compilation , IEnumerable < BaseNamespaceDeclarationSyntax > allNamespaces , IEnumerable < InterfaceInfo > markedInterfacesIdentifier , CancellationToken cancellationToken )
191
+ private static IEnumerable < TypeInfo > FindClasses (
192
+ Compilation compilation ,
193
+ IEnumerable < BaseNamespaceDeclarationSyntax > allNamespaces ,
194
+ IEnumerable < InterfaceInfo > markedInterfacesIdentifier ,
195
+ CancellationToken cancellationToken
196
+ )
184
197
{
185
198
var interfaces = markedInterfacesIdentifier . ToDictionary ( x => x . Symbol , SymbolEqualityComparer . Default ) ;
186
199
@@ -219,30 +232,35 @@ private static IEnumerable<ClassDeclarationSyntax> GetAllClassDeclarations(Synta
219
232
}
220
233
}
221
234
222
- private static VariableDeclaratorSyntax ? FindKeyOf ( TypeInfo type , PropertyDeclarationSyntax propertySyntax ) => type . Declaration . Members
223
- . OfType < FieldDeclarationSyntax > ( )
224
- . Where ( x => x . Modifiers . Any ( y => y . Text == "const" ) )
225
- . Select ( x => x . Declaration . Variables . SingleOrDefault ( ) )
226
- . Where ( x => x is not null ) . Select ( x => x ! )
227
- . FirstOrDefault ( x => x . Identifier . Text == $ "{ propertySyntax . Identifier . Text } { TypealizR . _ . FallBackKeySuffix } ")
228
- ;
229
-
230
- private static VariableDeclaratorSyntax ? FindKeyOf ( TypeInfo type , MethodDeclarationSyntax methodSyntax ) => type . Declaration . Members
231
- . OfType < FieldDeclarationSyntax > ( )
232
- . Where ( x => x . Modifiers . Any ( y => y . Text == "const" ) )
233
- . Select ( x => x . Declaration . Variables . SingleOrDefault ( ) )
234
- . Where ( x => x is not null ) . Select ( x => x ! )
235
- . FirstOrDefault ( x => x . Identifier . Text == $ "{ methodSyntax . Identifier . Text } { TypealizR . _ . FallBackKeySuffix } ")
236
- ;
235
+ private static VariableDeclaratorSyntax ? FindKeyOf ( TypeInfo type , PropertyDeclarationSyntax propertySyntax )
236
+ => type . Declaration . Members
237
+ . OfType < FieldDeclarationSyntax > ( )
238
+ . Where ( x => x . Modifiers . Any ( y => y . Text == "const" ) )
239
+ . Select ( x => x . Declaration . Variables . SingleOrDefault ( ) )
240
+ . Where ( x => x is not null ) . Select ( x => x ! )
241
+ . FirstOrDefault ( x => x . Identifier . Text == $ "{ propertySyntax . Identifier . Text } { TypealizR . _ . FallBackKeySuffix } ")
242
+ ;
243
+
244
+ private static VariableDeclaratorSyntax ? FindKeyOf ( TypeInfo type , MethodDeclarationSyntax methodSyntax )
245
+ => type . Declaration . Members
246
+ . OfType < FieldDeclarationSyntax > ( )
247
+ . Where ( x => x . Modifiers . Any ( y => y . Text == "const" ) )
248
+ . Select ( x => x . Declaration . Variables . SingleOrDefault ( ) )
249
+ . Where ( x => x is not null ) . Select ( x => x ! )
250
+ . FirstOrDefault ( x => x . Identifier . Text == $ "{ methodSyntax . Identifier . Text } { TypealizR . _ . FallBackKeySuffix } ")
251
+ ;
237
252
238
253
private static void AddProperty ( IndentableWriter writer , TypeInfo type , ResxBuilder builder , PropertyDeclarationSyntax property )
239
254
{
240
255
var key = FindKeyOf ( type , property ) ;
241
256
var sanitizedValue = key ? . Initializer ? . Value ? . ToResourceKey ( ) ?? "" ;
242
-
257
+
243
258
if ( key is not null && ! string . IsNullOrEmpty ( sanitizedValue ) )
244
259
{
245
- builder . Add ( property . Identifier . Text , sanitizedValue ) ;
260
+ var commentTrivia = property . Identifier . GetAllTrivia ( ) . FirstOrDefault ( t => t . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ) ;
261
+ var commentText = commentTrivia . ToFullString ( ) ? . Replace ( "//" , "" , StringComparison . InvariantCulture ) ;
262
+
263
+ builder . Add ( property . Identifier . Text , sanitizedValue , commentText ) ;
246
264
writer . WriteLine ( $ "✔️ { property . Identifier . Text } ") ;
247
265
}
248
266
else
@@ -255,9 +273,13 @@ private static void AddMethod(IndentableWriter writer, TypeInfo type, ResxBuilde
255
273
{
256
274
var key = FindKeyOf ( type , method ) ;
257
275
var sanitizedValue = key ? . Initializer ? . Value ? . ToResourceKey ( ) ?? "" ;
276
+
258
277
if ( key is not null && ! string . IsNullOrEmpty ( sanitizedValue ) )
259
278
{
260
- builder . Add ( method . Identifier . Text , sanitizedValue ) ;
279
+ var commentTrivia = method . DescendantTrivia ( null , true ) . FirstOrDefault ( t => t . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ) ;
280
+ var commentText = commentTrivia . ToFullString ( ) ? . Replace ( "//" , "" , StringComparison . InvariantCulture ) ;
281
+
282
+ builder . Add ( method . Identifier . Text , sanitizedValue , commentText ) ;
261
283
writer . WriteLine ( $ "✔️ { method . Identifier . Text } ()") ;
262
284
}
263
285
else
0 commit comments