-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for xml documentation to class, constructot, fields and properties. * Added support for single line comments to statements.
- Loading branch information
Showing
17 changed files
with
277 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/Testura.Code/Extensions/CsharpSyntaxNodeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Testura.Code.Models; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace Testura.Code.Generators.Special | ||
{ | ||
/// <summary> | ||
/// Extension methods to CsharpSyntaxNode. | ||
/// </summary> | ||
public static class CsharpSyntaxNodeExtensions | ||
{ | ||
/// <summary> | ||
/// Create summary to syntax node. | ||
/// </summary> | ||
/// <typeparam name="T">Syntax type.</typeparam> | ||
/// <param name="syntax">The syntax.</param> | ||
/// <param name="summary">Summary text.</param> | ||
/// <param name="parameters">Parameters in the summary</param> | ||
/// <returns>Return syntax node with summary.</returns> | ||
public static T WithSummary<T>(this T syntax, string summary, IEnumerable<Parameter> parameters = null) | ||
where T : CSharpSyntaxNode | ||
{ | ||
parameters = parameters ?? new List<Parameter>(); | ||
|
||
if (string.IsNullOrEmpty(summary)) | ||
{ | ||
return syntax; | ||
} | ||
|
||
var content = List<XmlNodeSyntax>(); | ||
|
||
content = CreateSummaryDocumentation(content, summary); | ||
|
||
foreach (var parameter in parameters.Where(p => p.XmlDocumentation != null)) | ||
{ | ||
content = CreateParameterDocumentation(content, parameter); | ||
} | ||
|
||
content = content.Add(XmlText().WithTextTokens(TokenList(XmlTextNewLine(TriviaList(), "\n", "\n", TriviaList())))); | ||
|
||
var trivia = Trivia( | ||
DocumentationCommentTrivia( | ||
SyntaxKind.SingleLineDocumentationCommentTrivia, | ||
content)); | ||
return syntax.WithLeadingTrivia(trivia); | ||
} | ||
|
||
private static SyntaxList<XmlNodeSyntax> CreateSummaryDocumentation(SyntaxList<XmlNodeSyntax> content, string text) | ||
{ | ||
var summary = new List<SyntaxToken>(); | ||
summary.Add(XmlTextNewLine(TriviaList(), "\n", "\n", TriviaList())); | ||
var commentLines = text.Split(new[] { "\n" }, StringSplitOptions.None); | ||
for (int n = 0; n < commentLines.Length; n++) | ||
{ | ||
var fixedCommentLine = $" {commentLines[n]}"; | ||
if (n != commentLines.Length - 1) | ||
{ | ||
fixedCommentLine += "\n"; | ||
} | ||
|
||
summary.Add(XmlTextLiteral(TriviaList(DocumentationCommentExterior("///")), fixedCommentLine, fixedCommentLine, TriviaList())); | ||
} | ||
|
||
summary.Add(XmlTextNewLine(TriviaList(), "\n", "\n", TriviaList())); | ||
summary.Add(XmlTextLiteral(TriviaList(DocumentationCommentExterior("///")), " ", " ", TriviaList())); | ||
|
||
return content.AddRange(new List<XmlNodeSyntax> | ||
{ | ||
XmlText().WithTextTokens(TokenList(XmlTextLiteral(TriviaList(DocumentationCommentExterior("///")), " ", " ", TriviaList()))), | ||
XmlElement(XmlElementStartTag(XmlName(Identifier("summary"))), XmlElementEndTag(XmlName(Identifier("summary")))) | ||
.WithContent(SingletonList<XmlNodeSyntax>(XmlText().WithTextTokens(TokenList(summary)))), | ||
}); | ||
} | ||
|
||
private static SyntaxList<XmlNodeSyntax> CreateParameterDocumentation(SyntaxList<XmlNodeSyntax> content, Parameter parameter) | ||
{ | ||
return content.AddRange(new List<XmlNodeSyntax> { | ||
XmlText().WithTextTokens( | ||
TokenList( | ||
new[] | ||
{ | ||
XmlTextNewLine( | ||
TriviaList(), | ||
"\n", | ||
"\n", | ||
TriviaList()), | ||
XmlTextLiteral( | ||
TriviaList( | ||
DocumentationCommentExterior("///")), | ||
" ", | ||
" ", | ||
TriviaList()) | ||
})), | ||
XmlExampleElement(SingletonList<XmlNodeSyntax>( | ||
XmlText().WithTextTokens( | ||
TokenList( | ||
XmlTextLiteral( | ||
TriviaList(), | ||
parameter.XmlDocumentation, | ||
parameter.XmlDocumentation, | ||
TriviaList()))))) | ||
.WithStartTag(XmlElementStartTag( | ||
XmlName( | ||
Identifier("param"))) | ||
.WithAttributes( | ||
SingletonList<XmlAttributeSyntax>( | ||
XmlNameAttribute( | ||
XmlName( | ||
Identifier(" name")), | ||
Token(SyntaxKind.DoubleQuoteToken), | ||
IdentifierName(parameter.Name), | ||
Token(SyntaxKind.DoubleQuoteToken))))) | ||
.WithEndTag( | ||
XmlElementEndTag( | ||
XmlName( | ||
Identifier("param")))) }); | ||
} | ||
} | ||
} |
Oops, something went wrong.