Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Src/CSharpier.Core/Xml/RawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,29 @@ public RawNode GetLastDescendant()
{
return this.NodeType is XmlNodeType.Element ? this.Nodes.LastOrDefault() ?? this : this;
}

public override string? ToString()
{
if (this.IsTextLike())
{
return this.Value;
}

if (this.NodeType is XmlNodeType.Element)
{
if (this.IsEmpty)
{
return "<" + this.Name + " />";
}

return "<" + this.Name;
}

if (this.NodeType is XmlNodeType.EndElement)
{
return "</" + this.Name + ">";
}

return base.ToString();
}
}
9 changes: 9 additions & 0 deletions Src/CSharpier.Core/Xml/XNodePrinters/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ Doc PrintLineAfterChildren()
{
return Doc.Null;
}

if (
rawNode.Nodes is [{ NodeType: XmlNodeType.Text }]
&& rawNode.Nodes[0].Value.TrimEnd(' ')[^1] is 'r' or '\n'
)
{
return Doc.Null;
}

return Doc.SoftLine;
}

Expand Down
34 changes: 30 additions & 4 deletions Src/CSharpier.Core/Xml/XNodePrinters/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static Doc PrintClosingTagStart(RawNode rawNode, PrintingContext context)
{
var lastChild = rawNode.Nodes.LastOrDefault();

return lastChild is not null && NeedsToBorrowParentClosingTagStartMarker(lastChild)
return lastChild is not null && PrintParentClosingTagStartWithContent(lastChild)
? Doc.Null
: PrintClosingTagStartMarker(rawNode, context);
}
Expand All @@ -78,7 +78,7 @@ public static Doc PrintClosingTagEndMarker(RawNode rawNode)

public static Doc PrintClosingTagSuffix(RawNode rawNode, PrintingContext context)
{
return NeedsToBorrowParentClosingTagStartMarker(rawNode)
return PrintParentClosingTagStartWithContent(rawNode)
? PrintClosingTagStartMarker(rawNode.Parent!, context)
: NeedsToBorrowNextOpeningTagStartMarker(rawNode)
? PrintOpeningTagStartMarker(rawNode.NextNode!, context)
Expand Down Expand Up @@ -113,7 +113,7 @@ private static bool NeedsToBorrowNextOpeningTagStartMarker(RawNode rawNode)
;
}

private static bool NeedsToBorrowParentClosingTagStartMarker(RawNode rawNode)
private static bool PrintParentClosingTagStartWithContent(RawNode rawNode)
{
/*
* <p>
Expand All @@ -126,9 +126,35 @@ private static bool NeedsToBorrowParentClosingTagStartMarker(RawNode rawNode)
* ^^^
* >
*/
// TODO #1790 we really want this last condition only if the indentation of the last line of the text value matches
// the indentation of the start element. Bleh.
/*
may have to handle one of these vs the second
<Root>
<Element Attribute="TheSign">
Life is demanding.
</Element>
</Root>
<Root>
<Element Attribute="TheSign">
Life is demanding.
</Element>
</Root>
there is also this case
<Root>
<Element >
Life is demanding.
</Element>
</Root>
*/
return rawNode.NextNode is null
&& rawNode.IsTextLike()
&& rawNode.GetLastDescendant().NodeType is XmlNodeType.Text;
&& rawNode.GetLastDescendant() is { NodeType: XmlNodeType.Text } textNode
&& (
textNode.Value[^1] is not (' ' or '\r' or '\n')
|| !textNode.Value.Contains('\n')
|| rawNode.Parent!.Nodes.Any(o => !o.IsTextLike())
);
}

public static bool NeedsToBorrowParentOpeningTagEndMarker(RawNode rawNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetCore.Test'
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetStandard.Test' "
/>
<Element Attribute="TheSign">
Life is demanding.
</Element>
</Root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,34 @@

Because whitespace is strict
The indentation of the closing element can't change
</Element
>
</Element>
<Element>
Shorter Text with indentation that can't change
</Element>
<returns>
<para
>A <see cref="T:System.Int32" /> containing a value that reflects the sort order of
<paramref name="x" /> as compared to <paramref name="y" />. The following table defines the conditions
under which the returned value is a negative number, zero, or a positive
number.</para
>
</returns>
<para>Some text that ends with a space. </para>
<para
>Some loooooooooooooooooooooooooooooooooong text with this <br /> and a space after this. </para
>
<exception cref="T:System.InvalidOperationException"
>The current instance is read-only and a set operation was attempted. </exception
>
<example>
<code>public void MethodName() {
}
</code>
</example>
<Root>
<Element />
<!--
SomeText
-->
</Root>
</Root>