Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossanlop committed Sep 21, 2024
1 parent e402e46 commit 6ce8501
Showing 1 changed file with 104 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static System.Net.Mime.MediaTypeNames;

namespace ApiDocsSync.PortToTripleSlash.Roslyn;

Expand All @@ -33,7 +34,7 @@ public DocumentationUpdater(Configuration config, IDocsAPI api, SyntaxTrivia? in

public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax> originalDocumentation)
{
SyntaxList<XmlNodeSyntax> docsNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> docsNodes = [];

// Preserve the order in which each API element is looked for below

Expand All @@ -43,7 +44,7 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>
}
else if (TryGet("summary") is XmlNodeSyntax existingSummary)
{
docsNodes.Add(existingSummary);
docsNodes.AddRange(RegeneraGetFullTripleSlashXmlSyntaxNodeteExistingNode(existingSummary));

Check failure on line 47 in src/PortToTripleSlash/src/libraries/RoslynTripleSlash/DocumentationUpdater.cs

View workflow job for this annotation

GitHub Actions / build

The name 'RegeneraGetFullTripleSlashXmlSyntaxNodeteExistingNode' does not exist in the current context

Check failure on line 47 in src/PortToTripleSlash/src/libraries/RoslynTripleSlash/DocumentationUpdater.cs

View workflow job for this annotation

GitHub Actions / build

The name 'RegeneraGetFullTripleSlashXmlSyntaxNodeteExistingNode' does not exist in the current context
}

foreach (DocsTypeParam typeParam in _api.TypeParams)
Expand All @@ -54,7 +55,7 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>
}
else if (TryGet("typeparam", "name", typeParam.Value) is XmlNodeSyntax existingTypeParam)
{
docsNodes.Add(existingTypeParam);
docsNodes.AddRange(GetFullTripleSlashXmlSyntaxNode(existingTypeParam));

}
}
Expand All @@ -67,7 +68,7 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>
}
else if (TryGet("param", "name", param.Value) is XmlNodeSyntax existingParam)
{
docsNodes.Add(existingParam);
docsNodes.AddRange(GetFullTripleSlashXmlSyntaxNode(existingParam));

}
}
Expand All @@ -78,16 +79,19 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>
}
else if (TryGet("returns") is XmlNodeSyntax existingReturns)
{
docsNodes.Add(existingReturns);
docsNodes.AddRange(GetFullTripleSlashXmlSyntaxNode(existingReturns));
}

if (!_config.SkipRemarks && !_api.Remarks.IsDocsEmpty())
if (!_config.SkipRemarks)
{
docsNodes.AddRange(GetRemarksNodes());
}
else if (TryGet("remarks") is XmlNodeSyntax existingRemarks)
{
docsNodes.Add(existingRemarks);
if (!_api.Remarks.IsDocsEmpty())
{
docsNodes.AddRange(GetRemarksNodes());
}
else if (TryGet("remarks") is XmlNodeSyntax existingRemarks)
{
docsNodes.AddRange(GetFullTripleSlashXmlSyntaxNode(existingRemarks));
}
}

foreach (DocsException exception in _api.Exceptions)
Expand All @@ -98,14 +102,14 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>
}
else if (TryGet("exception", "cref", exception.Value) is XmlNodeSyntax existingException)
{
docsNodes.Add(existingException);
docsNodes.AddRange(GetFullTripleSlashXmlSyntaxNode(existingException));

}
}

return SyntaxFactory.DocumentationCommentTrivia(
SyntaxKind.SingleLineDocumentationCommentTrivia,
docsNodes);
SyntaxFactory.List(docsNodes));

XmlNodeSyntax? TryGet(string tagName, string? attributeName = null, string? attributeValue = null)
{
Expand All @@ -115,19 +119,20 @@ public DocumentationCommentTriviaSyntax GetUpdatedDocs(SyntaxList<XmlNodeSyntax>

public DocumentationCommentTriviaSyntax GetNewDocs()
{
SyntaxList<XmlNodeSyntax> docsNodes = SyntaxFactory.List<XmlNodeSyntax>();

// Preserve the order
docsNodes.AddRange(GetSummaryNodes());
docsNodes.AddRange(GetTypeParamNodes());
docsNodes.AddRange(GetParamNodes());
docsNodes.AddRange(GetReturnsNodes());
docsNodes.AddRange(GetRemarksNodes());
docsNodes.AddRange(GetExceptionNodes());
List<XmlNodeSyntax> docsNodes =
[
// Preserve the order
.. GetSummaryNodes(),
.. GetTypeParamNodes(),
.. GetParamNodes(),
.. GetReturnsNodes(),
.. GetRemarksNodes(),
.. GetExceptionNodes(),
];

return SyntaxFactory.DocumentationCommentTrivia(
SyntaxKind.SingleLineDocumentationCommentTrivia,
docsNodes);
SyntaxFactory.List(docsNodes));
}

private XmlNodeSyntax[] GetSummaryNodes()
Expand All @@ -137,16 +142,16 @@ private XmlNodeSyntax[] GetSummaryNodes()
return [];
}

SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> internalTextNodes = new();
foreach (string line in _api.Summary.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
GetTripleSlashTextSyntaxNode(),
SyntaxFactory.XmlSummaryElement(internalTextNodes),
GetNewLineTextSyntaxNode(),
SyntaxFactory.XmlSummaryElement(internalTextNodes.ToArray()).WithEndTag(endTag), // TODO

Check failure on line 153 in src/PortToTripleSlash/src/libraries/RoslynTripleSlash/DocumentationUpdater.cs

View workflow job for this annotation

GitHub Actions / build

The name 'endTag' does not exist in the current context

Check failure on line 153 in src/PortToTripleSlash/src/libraries/RoslynTripleSlash/DocumentationUpdater.cs

View workflow job for this annotation

GitHub Actions / build

The name 'endTag' does not exist in the current context
GetNewLineTextSyntaxNode()
];
}

Expand All @@ -157,7 +162,7 @@ private XmlNodeSyntax[] GetTypeParamNodes()
return [];
}

SyntaxList<XmlNodeSyntax> typeParamNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> typeParamNodes = new();
foreach (DocsTypeParam typeParam in _api.TypeParams)
{
typeParamNodes.AddRange(GetTypeParamNode(typeParam));
Expand All @@ -168,17 +173,14 @@ private XmlNodeSyntax[] GetTypeParamNodes()

private XmlNodeSyntax[] GetTypeParamNode(DocsTypeParam typeParam)
{
SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> internalTextNodes = new();
foreach (string line in typeParam.Value.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
GetTripleSlashTextSyntaxNode(),
GetXmlAttributedElement(internalTextNodes, "typeparam", "name", typeParam.Name),
GetNewLineTextSyntaxNode()
];
// TODO
return GetFullTripleSlashXmlSyntaxNode(GetXmlAttributedElement(internalTextNodes, "typeparam", "name", typeParam.Name));
}

private XmlNodeSyntax[] GetParamNodes()
Expand All @@ -188,7 +190,7 @@ private XmlNodeSyntax[] GetParamNodes()
return [];
}

SyntaxList<XmlNodeSyntax> paramNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> paramNodes = new();
foreach (DocsParam param in _api.Params)
{
paramNodes.AddRange(GetParamNode(param));
Expand All @@ -202,7 +204,7 @@ private XmlNodeSyntax[] GetParamNode(DocsParam param)
SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
foreach (string line in param.Value.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
Expand All @@ -219,15 +221,15 @@ private XmlNodeSyntax[] GetReturnsNodes()
return [];
}

SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> internalTextNodes = new();
foreach (string line in _api.Returns.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
GetTripleSlashTextSyntaxNode(),
SyntaxFactory.XmlReturnsElement(internalTextNodes),
SyntaxFactory.XmlReturnsElement(internalTextNodes.ToArray()),
GetNewLineTextSyntaxNode(),
];
}
Expand All @@ -239,15 +241,15 @@ private XmlNodeSyntax[] GetRemarksNodes()
return [];
}

SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> internalTextNodes = new();
foreach (string line in _api.Remarks.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
GetTripleSlashTextSyntaxNode(),
SyntaxFactory.XmlRemarksElement(internalTextNodes),
SyntaxFactory.XmlRemarksElement(internalTextNodes.ToArray()),
GetNewLineTextSyntaxNode(),
];
}
Expand All @@ -259,7 +261,7 @@ private XmlNodeSyntax[] GetExceptionNodes()
return [];
}

SyntaxList<XmlNodeSyntax> exceptionNodes = SyntaxFactory.List<XmlNodeSyntax>();
List<XmlNodeSyntax> exceptionNodes = new();
foreach (DocsException exception in _api.Exceptions)
{
exceptionNodes.AddRange(GetExceptionNode(exception));
Expand All @@ -273,7 +275,7 @@ private XmlNodeSyntax[] GetExceptionNode(DocsException exception)
SyntaxList<XmlNodeSyntax> internalTextNodes = SyntaxFactory.List<XmlNodeSyntax>();
foreach (string line in exception.Value.Split(_NewLineSeparators, _NewLineSplitOptions))
{
internalTextNodes.Add(GetTripleSlashSingleLineXmlTextSyntaxNode(_indentationTrivia, line));
internalTextNodes.Add(GetFullTripleSlashSingleLineXmlTextSyntaxNode(line));
}

return [
Expand All @@ -289,8 +291,6 @@ private XmlElementSyntax GetXmlAttributedElement(IEnumerable<XmlNodeSyntax> cont
Debug.Assert(!string.IsNullOrWhiteSpace(attributeName));
Debug.Assert(!string.IsNullOrWhiteSpace(attributeValue));

XmlElementStartTagSyntax startTag = SyntaxFactory.XmlElementStartTag(SyntaxFactory.XmlName(SyntaxFactory.Identifier(tagName)));

SyntaxToken xmlAttributeName = SyntaxFactory.Identifier(
leading: SyntaxFactory.TriviaList(SyntaxFactory.Space),
text: attributeName,
Expand All @@ -302,69 +302,96 @@ private XmlElementSyntax GetXmlAttributedElement(IEnumerable<XmlNodeSyntax> cont
identifier: SyntaxFactory.IdentifierName(attributeValue),
endQuoteToken: SyntaxFactory.Token(SyntaxKind.DoubleQuoteToken));

SyntaxList<XmlAttributeSyntax> startTagAttributes = SyntaxFactory.SingletonList<XmlAttributeSyntax>(xmlAttribute);

startTag = startTag.WithAttributes(startTagAttributes);
XmlElementStartTagSyntax startTag = SyntaxFactory
.XmlElementStartTag(SyntaxFactory.XmlName(SyntaxFactory.Identifier(tagName)))
.WithAttributes(SyntaxFactory.List<XmlAttributeSyntax>([xmlAttribute]));

XmlElementEndTagSyntax endTag = SyntaxFactory.XmlElementEndTag(SyntaxFactory.XmlName(SyntaxFactory.Identifier(tagName)));

return SyntaxFactory.XmlElement(startTag, SyntaxFactory.List(content), endTag);
}

private XmlNodeSyntax[] GetFullTripleSlashXmlSyntaxNode(XmlNodeSyntax existingNode)
{
GetIndentationSyntaxToken(out SyntaxToken indentationSyntaxToken);
GetTripleSlashSyntaxToken(out SyntaxToken tripleSlashSyntaxToken);
GetNewLineSyntaxToken(out SyntaxToken newLineSyntaxToken);

return [
SyntaxFactory.XmlText(new SyntaxToken[] { indentationSyntaxToken, tripleSlashSyntaxToken }),
existingNode,
SyntaxFactory.XmlText(SyntaxFactory.TokenList(newLineSyntaxToken)),
];
}

// Returns a single line of triple slash comments plus the optional line of text that may follow it.
// [indentation][tripleslash][textline][newline]
// Example: ->->->/// text\n
private XmlTextSyntax GetTripleSlashSingleLineXmlTextSyntaxNode(SyntaxTrivia? indentationTrivia, string? text = null)
private XmlTextSyntax GetFullTripleSlashSingleLineXmlTextSyntaxNode(string text)
{
SyntaxTokenList syntaxTokenList = SyntaxFactory.TokenList();

syntaxTokenList.Add(GetIndentationSyntaxToken(indentationTrivia));
syntaxTokenList.Add(GetTripleSlashSyntaxToken());
if (text != null)
{
syntaxTokenList.Add(SyntaxFactory.XmlTextNewLine(
GetIndentationSyntaxToken(out SyntaxToken indentationSyntaxToken);
GetTripleSlashSyntaxToken(out SyntaxToken tripleSlashSyntaxToken);
GetNewLineSyntaxToken(out SyntaxToken newLineSyntaxToken);

SyntaxToken[] syntaxTokenList = [
indentationSyntaxToken,
tripleSlashSyntaxToken,
SyntaxFactory.XmlTextNewLine(
leading: SyntaxFactory.TriviaList(),
text: text,
value: text,
trailing: SyntaxFactory.TriviaList()));
}
syntaxTokenList.Add(GetNewLineSyntaxToken());
trailing: SyntaxFactory.TriviaList()),
newLineSyntaxToken
];

return SyntaxFactory.XmlText().WithTextTokens(syntaxTokenList);
return SyntaxFactory.XmlText(SyntaxFactory.TokenList(syntaxTokenList));
}

// Returns a syntax node containing the "/// " text literal syntax token.
private XmlTextSyntax GetTripleSlashTextSyntaxNode() =>
SyntaxFactory.XmlText().WithTextTokens(SyntaxFactory.TokenList(GetTripleSlashSyntaxToken()));
private XmlTextSyntax GetTripleSlashTextSyntaxNode()
{
GetTripleSlashSyntaxToken(out SyntaxToken tripleSlashSyntaxToken);
return SyntaxFactory.XmlText().WithTextTokens(SyntaxFactory.TokenList(tripleSlashSyntaxToken));
}

// Returns a syntax node containing the "\n" text literal syntax token.
private XmlTextSyntax GetNewLineTextSyntaxNode() =>
SyntaxFactory.XmlText().WithTextTokens(SyntaxFactory.TokenList(GetNewLineSyntaxToken()));
private XmlTextSyntax GetNewLineTextSyntaxNode()
{
GetNewLineSyntaxToken(out SyntaxToken newLineSyntaxToken);
return SyntaxFactory.XmlText().WithTextTokens(SyntaxFactory.TokenList(newLineSyntaxToken));
}

// Returns a syntax token with the "" text literal preceded by the specified indentation trivia.
private SyntaxToken GetIndentationSyntaxToken(SyntaxTrivia? indentationTrivia) =>
SyntaxFactory.XmlTextLiteral(
leading: indentationTrivia != null ? SyntaxFactory.TriviaList(indentationTrivia.Value) : SyntaxFactory.TriviaList(),
text: string.Empty,
value: string.Empty,
trailing: SyntaxFactory.TriviaList());
// Returns a syntax node containing the specified indentation text literal syntax token.
private XmlTextSyntax GetIndentationTextSyntaxNode()
{
GetIndentationSyntaxToken(out SyntaxToken indentationSyntaxToken);
return SyntaxFactory.XmlText().WithTextTokens(SyntaxFactory.TokenList(indentationSyntaxToken));
}

// Returns a syntax token containing the "/// " text literal.
private SyntaxToken GetTripleSlashSyntaxToken() =>
SyntaxFactory.XmlTextLiteral(
private void GetTripleSlashSyntaxToken(out SyntaxToken tripleSlashSyntaxToken) =>
tripleSlashSyntaxToken = SyntaxFactory.XmlTextLiteral(
leading: SyntaxFactory.TriviaList(SyntaxFactory.DocumentationCommentExterior(TripleSlash)),
text: Space,
value: Space,
trailing: SyntaxFactory.TriviaList());

// Returns a syntax token containing the "\n" text literal.
private SyntaxToken GetNewLineSyntaxToken() =>
SyntaxFactory.XmlTextNewLine(
private void GetNewLineSyntaxToken(out SyntaxToken newLineSyntaxToken) =>
newLineSyntaxToken = SyntaxFactory.XmlTextNewLine(
leading: SyntaxFactory.TriviaList(),
text: NewLine,
value: NewLine,
trailing: SyntaxFactory.TriviaList());

// Returns a syntax token with the "" text literal preceded by the specified indentation trivia.
private void GetIndentationSyntaxToken(out SyntaxToken indentationSyntaxToken) =>
indentationSyntaxToken = SyntaxFactory.XmlTextLiteral(
leading: SyntaxFactory.TriviaList(_indentationTrivia),
text: string.Empty,
value: string.Empty,
trailing: SyntaxFactory.TriviaList());

private static bool DoesNodeHaveTag(SyntaxNode xmlNode, string tagName, string? attributeName = null, string? attributeValue = null)
{
if (xmlNode.Kind() is SyntaxKind.XmlElement && xmlNode is XmlElementSyntax xmlElement)
Expand Down

0 comments on commit 6ce8501

Please sign in to comment.