Skip to content

Commit

Permalink
Preserve annotations when rewriting.
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys committed Jan 18, 2024
1 parent 3975f0b commit 086582a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
34 changes: 17 additions & 17 deletions src/Microsoft.Language.Xml/Syntax/SyntaxRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 144,13 @@ public override SyntaxNode VisitXmlDocument(XmlDocumentSyntax node)

if (anyChanges)
{
return XmlDocument(
return node.CopyAnnotationsTo(XmlDocument(
newDeclaration,
newPrecedingMisc.Node,
newRoot,
newFollowingMisc.Node,
newSkippedTokens,
newEof);
newEof));
}
else
{
Expand Down Expand Up @@ -199,13 199,13 @@ public override SyntaxNode VisitXmlDeclaration(XmlDeclarationSyntax node)

if (anyChanges)
{
return XmlDeclaration(
return node.CopyAnnotationsTo(XmlDeclaration(
newLessThanQuestionToken,
newXmlKeyword,
newVersion,
newEncoding,
newStandalone,
newQuestionGreaterThanToken);
newQuestionGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -236,7 236,7 @@ public override SyntaxNode VisitXmlDeclarationOption(XmlDeclarationOptionSyntax

if (anyChanges)
{
return XmlDeclarationOption(newName, newEquals, newValue);
return node.CopyAnnotationsTo(XmlDeclarationOption(newName, newEquals, newValue));
}
else
{
Expand Down Expand Up @@ -267,7 267,7 @@ public override SyntaxNode VisitXmlElement(XmlElementSyntax node)

if (anyChanges)
{
return XmlElement(newStartTag, newContent.Node, newEndTag);
return node.CopyAnnotationsTo(XmlElement(newStartTag, newContent.Node, newEndTag));
}
else
{
Expand All @@ -286,7 286,7 @@ public override SyntaxNode VisitXmlText(XmlTextSyntax node)

if (anyChanges)
{
return XmlText(newTextTokens.Node);
return node.CopyAnnotationsTo(XmlText(newTextTokens.Node));
}
else
{
Expand Down Expand Up @@ -323,7 323,7 @@ public override SyntaxNode VisitXmlElementStartTag(XmlElementStartTagSyntax node

if (anyChanges)
{
return XmlElementStartTag(newLessThanToken, newName, newAttributes.Node, newGreaterThanToken);
return node.CopyAnnotationsTo(XmlElementStartTag(newLessThanToken, newName, newAttributes.Node, newGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -354,7 354,7 @@ public override SyntaxNode VisitXmlElementEndTag(XmlElementEndTagSyntax node)

if (anyChanges)
{
return XmlElementEndTag(newLessThanSlashToken, newName, newGreaterThanToken);
return node.CopyAnnotationsTo(XmlElementEndTag(newLessThanSlashToken, newName, newGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -391,7 391,7 @@ public override SyntaxNode VisitXmlEmptyElement(XmlEmptyElementSyntax node)

if (anyChanges)
{
return XmlEmptyElement(newLessThanToken, newName, newAttributes.Node, newSlashGreaterThanToken);
return node.CopyAnnotationsTo(XmlEmptyElement(newLessThanToken, newName, newAttributes.Node, newSlashGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -422,7 422,7 @@ public override SyntaxNode VisitXmlAttribute(XmlAttributeSyntax node)

if (anyChanges)
{
return XmlAttribute(newName, newEqualsToken, newValue);
return node.CopyAnnotationsTo(XmlAttribute(newName, newEqualsToken, newValue));
}
else
{
Expand Down Expand Up @@ -453,7 453,7 @@ public override SyntaxNode VisitXmlString(XmlStringSyntax node)

if (anyChanges)
{
return XmlString(newStartQuoteToken, newTextTokens.Node, newEndQuoteToken);
return node.CopyAnnotationsTo(XmlString(newStartQuoteToken, newTextTokens.Node, newEndQuoteToken));
}
else
{
Expand All @@ -478,7 478,7 @@ public override SyntaxNode VisitXmlName(XmlNameSyntax node)

if (anyChanges)
{
return XmlName(newPrefix, newLocalName);
return node.CopyAnnotationsTo(XmlName(newPrefix, newLocalName));
}
else
{
Expand All @@ -503,7 503,7 @@ public override SyntaxNode VisitXmlPrefix(XmlPrefixSyntax node)

if (anyChanges)
{
return XmlPrefix(newName, newColonToken);
return node.CopyAnnotationsTo(XmlPrefix(newName, newColonToken));
}
else
{
Expand Down Expand Up @@ -534,7 534,7 @@ public override SyntaxNode VisitXmlComment(XmlCommentSyntax node)

if (anyChanges)
{
return XmlComment(newLessThanExclamationMinusMinusToken, newTextTokens.Node, newMinusMinusGreaterThanToken);
return node.CopyAnnotationsTo(XmlComment(newLessThanExclamationMinusMinusToken, newTextTokens.Node, newMinusMinusGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -571,7 571,7 @@ public override SyntaxNode VisitXmlProcessingInstruction(XmlProcessingInstructio

if (anyChanges)
{
return XmlProcessingInstruction(newLessThanQuestionToken, newName, newTextTokens.Node, newQuestionGreaterThanToken);
return node.CopyAnnotationsTo(XmlProcessingInstruction(newLessThanQuestionToken, newName, newTextTokens.Node, newQuestionGreaterThanToken));
}
else
{
Expand Down Expand Up @@ -602,7 602,7 @@ public override SyntaxNode VisitXmlCDataSection(XmlCDataSectionSyntax node)

if (anyChanges)
{
return XmlCDataSection(newBeginCDataToken, newTextTokens.Node, newEndCDataToken);
return node.CopyAnnotationsTo(XmlCDataSection(newBeginCDataToken, newTextTokens.Node, newEndCDataToken));
}
else
{
Expand Down
7 changes: 5 additions & 2 deletions src/Microsoft.Language.Xml/Syntax/XmlEmptyElementSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 236,11 @@ public XmlElementSyntax WithContent(SyntaxList<SyntaxNode> content)
var startTag = SyntaxFactory.XmlElementStartTag(this.LessThanToken, this.NameNode, this.AttributesNode, greaterThanToken);
var lessThanSlashToken = SyntaxFactory.Punctuation(SyntaxKind.LessThanSlashToken, "</", null, null);
var endTag = SyntaxFactory.XmlElementEndTag(lessThanSlashToken, this.NameNode, greaterThanToken);

return SyntaxFactory.XmlElement(startTag, content, endTag);
var newNode = SyntaxFactory.XmlElement(startTag, content, endTag);
var annotations = this.GetAnnotations();
if (annotations != null && annotations.Length > 0)
return newNode.WithAnnotations(annotations);
return newNode;
}

public XmlEmptyElementSyntax WithSlashGreaterThanToken(PunctuationSyntax slashGreaterThanToken)
Expand Down

0 comments on commit 086582a

Please sign in to comment.