Skip to content

Commit

Permalink
Fix XML file with comments formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed Mar 27, 2024
1 parent 2a85686 commit 0a76440
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PropertyGroup>
<RepositoryUrl>https://github.com/kysect/DotnetProjectSystem</RepositoryUrl>
<PackageProjectUrl>https://github.com/kysect/DotnetProjectSystem</PackageProjectUrl>
<Version>0.1.14</Version>
<Version>0.1.15</Version>
</PropertyGroup>

<!-- Code configuration -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ public void Format_XmlEmptyTagWithoutAttribute_ExpectedResult()
Validate(input, expected);
}

[Fact]
public void Format_FileWithComment_ExpectNoChanges()
{
var input = """
<Project>
<!-- First -->
<PropertyGroup>
<IncludeSymbols>true</IncludeSymbols>
</PropertyGroup>
<!-- Second -->
<PropertyGroup>
<IncludeSymbols>true</IncludeSymbols>
</PropertyGroup>
</Project>
""";

Validate(input, input);
}

private void Validate(string input, string expected)
{
XmlDocumentSyntax document = Parser.ParseText(input);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.CommonLib.Exceptions;
using Kysect.DotnetProjectSystem.Tools;
using Microsoft.Language.Xml;
using System.Text;

Expand All @@ -13,31 +14,46 @@ public XmlDocumentSyntax Format(XmlDocumentSyntax documentSyntax)
{
documentSyntax.ThrowIfNull();

IXmlElement updatedElement = UpdateElement(documentSyntax.Root, 0);
documentSyntax = documentSyntax.ReplaceNode(
documentSyntax.Root.AsSyntaxElement.AsNode,
updatedElement.AsSyntaxElement.AsNode);
List<XmlNodeSyntax> elements = documentSyntax
.Descendants()
.OfType<IXmlElement>()
.Select(x => x.AsSyntaxElement.AsNode)
.ToList();

documentSyntax =
documentSyntax
.ReplaceNodes(
elements,
(o, u) => UpdateElement(documentSyntax, (IXmlElement) o, (IXmlElement) u));

return documentSyntax;
}

private IXmlElement UpdateElement(IXmlElement currentElement, int depth)
private XmlNodeSyntax UpdateElement(XmlDocumentSyntax documentSyntax, IXmlElement oldNode, IXmlElement currentElement)
{
currentElement.ThrowIfNull();

int depth = CalculateDepth(documentSyntax, oldNode);
currentElement = FormatAttributes(currentElement);
IXmlElementSyntax modified = currentElement.AsSyntaxElement;
modified = modified.RemoveAllChild();
currentElement = AddLeadingTrivia(depth, currentElement);

return currentElement.AsSyntaxElement.AsNode;
}

private int CalculateDepth(XmlDocumentSyntax _, IXmlElement currentElement)
{
int depth = 0;

foreach (IXmlElement? element in currentElement.Elements)
while (currentElement.Parent is not null)
{
IXmlElement updatedElement = UpdateElement(element, depth + 1);
if (depth > 10)
throw new DotnetProjectSystemException("Cannot calculate XML element depth. Possible StackOverflow.");

modified = modified
.AddChild(updatedElement.AsSyntaxElement);
depth++;
currentElement = currentElement.Parent;
}

return AddLeadingTrivia(depth, modified);
return depth;
}

private IXmlElement FormatAttributes(IXmlElement currentElement)
Expand Down Expand Up @@ -78,7 +94,7 @@ private IXmlElement FormatAttributes(IXmlElement currentElement)
return currentElementSyntax.AsElement;
}

private IXmlElement AddLeadingTrivia(int depth, IXmlElementSyntax modified)
private IXmlElement AddLeadingTrivia(int depth, IXmlElement modified)
{
if (modified is XmlEmptyElementSyntax xmlEmptyElementSyntax)
return AddLeadingTrivia(depth, xmlEmptyElementSyntax);
Expand Down

0 comments on commit 0a76440

Please sign in to comment.