Skip to content

Commit 553f830

Browse files
committed
Working on keeping the closing tag of an element clean.
1 parent 0fa83b7 commit 553f830

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

Src/CSharpier.Core/Utilities/ValueListBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Buffers;
22
using System.Diagnostics;
33
using System.Runtime.CompilerServices;
4+
using System.Text.Json;
45

56
namespace CSharpier.Core.Utilities;
67

@@ -176,4 +177,10 @@ private void Grow(int additionalCapacityRequired = 1)
176177
ArrayPool<T>.Shared.Return(toReturn);
177178
}
178179
}
180+
181+
public override string ToString()
182+
{
183+
// TODO get this to actually show something useful
184+
return JsonSerializer.Serialize(this.ToArray());
185+
}
179186
}

Src/CSharpier.Core/Xml/XNodePrinters/Element.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ Doc PrintLineAfterChildren()
6666
{
6767
return Doc.Null;
6868
}
69+
70+
if (
71+
rawNode.Nodes is [{ NodeType: XmlNodeType.Text }]
72+
&& rawNode.Nodes[0].Value.Contains('\n')
73+
)
74+
{
75+
return Doc.Null;
76+
}
77+
6978
return Doc.SoftLine;
7079
}
7180

Src/CSharpier.Core/Xml/XNodePrinters/Tag.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,31 @@ private static bool NeedsToBorrowParentClosingTagStartMarker(RawNode rawNode)
126126
* ^^^
127127
* >
128128
*/
129+
// TODO #1789 we really want this last condition only if the indentation of the last line of the text value matches
130+
// the indentation of the start element. Bleh.
131+
/*
132+
may have to handle one of these vs the second
133+
<Root>
134+
<Element Attribute="TheSign">
135+
Life is demanding.
136+
</Element>
137+
</Root>
138+
<Root>
139+
<Element Attribute="TheSign">
140+
Life is demanding.
141+
</Element>
142+
</Root>
143+
there is also this case
144+
<Root>
145+
<Element >
146+
Life is demanding.
147+
</Element>
148+
</Root>
149+
*/
129150
return rawNode.NextNode is null
130151
&& rawNode.IsTextLike()
131-
&& rawNode.GetLastDescendant().NodeType is XmlNodeType.Text;
152+
&& rawNode.GetLastDescendant() is { NodeType: XmlNodeType.Text } textNode
153+
&& !textNode.Value.Contains('\n');
132154
}
133155

134156
public static bool NeedsToBorrowParentOpeningTagEndMarker(RawNode rawNode)

Src/CSharpier.Tests/FormattingTests/TestFiles/xml/Attributes.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetCore.Test'
1212
AND '$(MSBuildProjectName)' != 'System.Net.Http.Formatting.NetStandard.Test' "
1313
/>
14+
<Element Attribute="TheSign">
15+
Life is demanding.
16+
</Element>
1417
</Root>

Src/CSharpier.Tests/FormattingTests/TestFiles/xml/StrictWhitespace.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
Because whitespace is strict
55
The indentation of the closing element can't change
6-
</Element
7-
>
6+
</Element>
87
<Element>
98
Shorter Text with indentation that can't change
109
</Element>

0 commit comments

Comments
 (0)