88using System . Diagnostics . Contracts ;
99using System . Linq ;
1010using System . Text ;
11+ using System . Text . RegularExpressions ;
1112using Microsoft . CodeAnalysis ;
1213using Microsoft . CodeAnalysis . CSharp ;
1314using Microsoft . CodeAnalysis . CSharp . Syntax ;
@@ -43,7 +44,7 @@ public void Execute(GeneratorExecutionContext context)
4344 return ;
4445 }
4546
46- foreach ( var items in syntaxReceiver . GatheredInfo . GroupBy < IMethodSymbol , INamedTypeSymbol > ( static item => item . ContainingType , SymbolEqualityComparer . Default ) )
47+ foreach ( var items in syntaxReceiver . GatheredInfo . GroupBy < SyntaxReceiver . Item , INamedTypeSymbol > ( static item => item . MethodSymbol . ContainingType , SymbolEqualityComparer . Default ) )
4748 {
4849 if ( items . Key . DeclaringSyntaxReferences . Length > 0 &&
4950 items . Key . DeclaringSyntaxReferences . First ( ) . GetSyntax ( ) is ClassDeclarationSyntax classDeclaration )
@@ -66,12 +67,12 @@ public void Execute(GeneratorExecutionContext context)
6667 /// <param name="context">The input <see cref="GeneratorExecutionContext"/> instance to use.</param>
6768 /// <param name="classDeclaration">The <see cref="ClassDeclarationSyntax"/> node to process.</param>
6869 /// <param name="classDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for <paramref name="classDeclaration"/>.</param>
69- /// <param name="methodSymbols ">The sequence of <see cref="IMethodSymbol"/> instances to process.</param>
70+ /// <param name="items ">The sequence of <see cref="IMethodSymbol"/> instances to process.</param>
7071 private static void OnExecute (
7172 GeneratorExecutionContext context ,
7273 ClassDeclarationSyntax classDeclaration ,
7374 INamedTypeSymbol classDeclarationSymbol ,
74- IEnumerable < IMethodSymbol > methodSymbols )
75+ IEnumerable < SyntaxReceiver . Item > items )
7576 {
7677 // Create the class declaration for the user type. This will produce a tree as follows:
7778 //
@@ -82,7 +83,7 @@ private static void OnExecute(
8283 var classDeclarationSyntax =
8384 ClassDeclaration ( classDeclarationSymbol . Name )
8485 . WithModifiers ( classDeclaration . Modifiers )
85- . AddMembers ( methodSymbols . Select ( item => CreateCommandMembers ( context , default , item ) ) . SelectMany ( static g => g ) . ToArray ( ) ) ;
86+ . AddMembers ( items . Select ( item => CreateCommandMembers ( context , item . LeadingTrivia , item . MethodSymbol ) ) . SelectMany ( static g => g ) . ToArray ( ) ) ;
8687
8788 TypeDeclarationSyntax typeDeclarationSyntax = classDeclarationSyntax ;
8889
@@ -158,6 +159,27 @@ private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(Generat
158159 AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ICommandGenerator ) . FullName ) ) ) ,
159160 AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ICommandGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) ) ) ;
160161
162+ SyntaxTriviaList summaryTrivia = SyntaxTriviaList . Empty ;
163+
164+ // Parse the <summary> docs, if present
165+ foreach ( SyntaxTrivia trivia in leadingTrivia )
166+ {
167+ if ( trivia . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ||
168+ trivia . IsKind ( SyntaxKind . SingleLineDocumentationCommentTrivia ) )
169+ {
170+ string text = trivia . ToString ( ) ;
171+
172+ Match match = Regex . Match ( text , @"<summary>.*?<\/summary>" , RegexOptions . Singleline ) ;
173+
174+ if ( match . Success )
175+ {
176+ summaryTrivia = TriviaList ( Comment ( $ "/// { match . Value } ") ) ;
177+
178+ break ;
179+ }
180+ }
181+ }
182+
161183 // Construct the generated property as follows (the explicit delegate cast is needed to avoid overload resolution conflicts):
162184 //
163185 // <METHOD_SUMMARY>
@@ -175,7 +197,8 @@ private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(Generat
175197 Attribute ( IdentifierName ( "global::System.CodeDom.Compiler.GeneratedCode" ) )
176198 . AddArgumentListArguments (
177199 AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ICommandGenerator ) . FullName ) ) ) ,
178- AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ICommandGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) ) ,
200+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( ICommandGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) )
201+ . WithOpenBracketToken ( Token ( summaryTrivia , SyntaxKind . OpenBracketToken , TriviaList ( ) ) ) ,
179202 AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.DebuggerNonUserCode" ) ) ) ) ,
180203 AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" ) ) ) ) )
181204 . WithExpressionBody (
0 commit comments