Skip to content

Commit cb72be7

Browse files
committed
Getting the basics of ignore whitespace in xml working.
1 parent 7df4028 commit cb72be7

32 files changed

+65
-30
lines changed

Src/CSharpier.Cli/Options/OptionsProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ await EditorConfigLocator.FindForDirectoryNameAsync(
117117
CancellationToken cancellationToken
118118
)
119119
{
120+
// TODO #1794 the xml whitespace should default based on the file
121+
// possible defaults
122+
// xaml files = xaml
123+
// msbuild ones = ignore
124+
// everything else = strict
120125
if (this.specifiedConfigFile is not null)
121126
{
122127
return this.specifiedConfigFile.ConvertToPrinterOptions(filePath);

Src/CSharpier.Core/PrinterOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public int IndentSize
2626
public EndOfLine EndOfLine { get; set; } = EndOfLine.Auto;
2727
public bool TrimInitialLines { get; init; } = true;
2828
public bool IncludeGenerated { get; set; }
29-
public Formatter Formatter { get; set; } = formatter;
29+
public Formatter Formatter { get; } = formatter;
3030
public XmlWhitespaceSensitivity XmlWhitespaceSensitivity { get; set; } =
3131
XmlWhitespaceSensitivity.Strict;
3232

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ Doc PrintLineBeforeChildren()
3434
}
3535

3636
if (
37-
rawNode.Nodes.FirstOrDefault() is
38-
{ NodeType: XmlNodeType.Text, Value: ['\n', ..] or ['\r', ..] }
37+
context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
38+
&& rawNode.Nodes.FirstOrDefault()
39+
is { NodeType: XmlNodeType.Text, Value: ['\n', ..] or ['\r', ..] }
3940
)
4041
{
4142
return Doc.LiteralLine;
4243
}
4344

44-
if (rawNode.Attributes.Length == 0 && rawNode.Nodes is [{ NodeType: XmlNodeType.Text }])
45+
if (
46+
rawNode.Attributes.Length == 0
47+
&& rawNode.Nodes is [{ NodeType: XmlNodeType.Text }]
48+
&& context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
49+
)
4550
{
4651
return Doc.Null;
4752
}
4853

49-
if (rawNode.Nodes.Any(o => o.NodeType is XmlNodeType.Text && o.Value.Contains('\n')))
54+
if (
55+
context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
56+
&& rawNode.Nodes.Any(o => o.NodeType is XmlNodeType.Text && o.Value.Contains('\n'))
57+
)
5058
{
5159
return Doc.HardLine;
5260
}
@@ -62,7 +70,11 @@ Doc PrintLineAfterChildren()
6270
return Doc.IfBreak(Doc.SoftLine, "", attrGroupId);
6371
}
6472

65-
if (rawNode.Attributes.Length == 0 && rawNode.Nodes is [{ NodeType: XmlNodeType.Text }])
73+
if (
74+
rawNode.Attributes.Length == 0
75+
&& rawNode.Nodes is [{ NodeType: XmlNodeType.Text }]
76+
&& context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
77+
)
6678
{
6779
return Doc.Null;
6880
}

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

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public static Doc Print(RawNode node, PrintingContext context)
1212
var groupIds = new List<string>();
1313
foreach (var _ in node.Nodes)
1414
{
15-
groupIds.Add(context.GroupFor("symbol"));
15+
groupIds.Add(context.GroupFor("children group"));
1616
}
1717

18-
var result = new DocListBuilder(node.Nodes.Count * 5);
18+
var result = new List<Doc>();
1919
var x = 0;
2020
foreach (var childNode in node.Nodes)
2121
{
@@ -25,10 +25,10 @@ public static Doc Print(RawNode node, PrintingContext context)
2525
continue;
2626
}
2727

28-
var prevParts = new DocListBuilder(2);
29-
var leadingParts = new DocListBuilder(2);
30-
var trailingParts = new DocListBuilder(2);
31-
var nextParts = new DocListBuilder(2);
28+
var prevParts = new List<Doc>();
29+
var leadingParts = new List<Doc>();
30+
var trailingParts = new List<Doc>();
31+
var nextParts = new List<Doc>();
3232

3333
var prevBetweenLine = childNode.PreviousNode is not null
3434
? PrintBetweenLine(childNode.PreviousNode, childNode)
@@ -38,17 +38,6 @@ public static Doc Print(RawNode node, PrintingContext context)
3838
? PrintBetweenLine(childNode, childNode.NextNode)
3939
: Doc.Null;
4040

41-
if (
42-
context.Options.XmlWhitespaceSensitivity is not XmlWhitespaceSensitivity.Strict
43-
&& childNode.PreviousNode is null
44-
&& childNode.NextNode is null
45-
&& childNode.IsTextLike()
46-
)
47-
{
48-
prevBetweenLine = Doc.SoftLine;
49-
nextBetweenLine = Doc.SoftLine;
50-
}
51-
5241
if (prevBetweenLine is not NullDoc)
5342
{
5443
if (prevBetweenLine is HardLine)
@@ -87,22 +76,22 @@ context.Options.XmlWhitespaceSensitivity is not XmlWhitespaceSensitivity.Strict
8776
}
8877
}
8978

90-
result.Add(prevParts.AsSpan());
79+
result.AddRange(prevParts);
9180
result.Add(
9281
Doc.Group(
93-
Doc.Concat(ref leadingParts),
82+
Doc.Concat(leadingParts),
9483
Doc.GroupWithId(
9584
groupIds[x],
9685
PrintChild(childNode, context),
97-
Doc.Concat(ref trailingParts)
86+
Doc.Concat(trailingParts)
9887
)
9988
)
10089
);
101-
result.Add(nextParts.AsSpan());
90+
result.AddRange(nextParts);
10291
x++;
10392
}
10493

105-
return Doc.Concat(ref result);
94+
return Doc.Concat(result);
10695
}
10796

10897
public static Doc PrintChild(RawNode child, PrintingContext context)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static Doc Print(RawNode node, PrintingContext context)
4141
List<Doc> doc =
4242
[
4343
Tag.PrintOpeningTagPrefix(node, context),
44-
GetTextValue(node),
44+
GetTextValue(node, context),
4545
Tag.PrintClosingTagSuffix(node, context),
4646
];
4747

@@ -77,7 +77,7 @@ or XmlNodeType.CDATA
7777
throw new Exception("Need to handle + " + node.NodeType);
7878
}
7979

80-
private static Doc GetTextValue(RawNode rawNode)
80+
private static Doc GetTextValue(RawNode rawNode, PrintingContext context)
8181
{
8282
var textValue = rawNode.Value;
8383

@@ -86,6 +86,11 @@ private static Doc GetTextValue(RawNode rawNode)
8686
return Doc.Null;
8787
}
8888

89+
if (context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Ignore)
90+
{
91+
textValue = textValue.Trim();
92+
}
93+
8994
if (rawNode.Parent?.Nodes.First() == rawNode)
9095
{
9196
if (textValue[0] is '\r')

Src/CSharpier.Core/XmlWhitespaceSensitivity.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ namespace CSharpier.Core;
33
internal enum XmlWhitespaceSensitivity
44
{
55
Strict,
6+
7+
// TODO #1794 figure out what this actually means code wise, should it be named something besides xaml?
68
Xaml,
9+
10+
// TODO #1794 do a lot more testing with this, can review the repos once the file extension stuff is figured out.
711
Ignore,
812
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Root>
2+
<LongElement____________________________________________>
3+
TextValue
4+
</LongElement____________________________________________>
5+
<LongElementWithAttribute Attribute="AttributeValue_________________">
6+
TextValue
7+
</LongElementWithAttribute>
8+
<ShortElement>TextValue</ShortElement>
9+
<ShortElementWithAttribute Attribute="1">TextValue</ShortElementWithAttribute>
10+
</Root>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Root>
2+
<LongElement____________________________________________>
3+
TextValue
4+
</LongElement____________________________________________>
5+
<LongElementWithAttribute Attribute="AttributeValue_________________">
6+
TextValue
7+
</LongElementWithAttribute>
8+
<ShortElement>TextValue</ShortElement>
9+
<ShortElementWithAttribute Attribute="1">TextValue</ShortElementWithAttribute>
10+
</Root>

Src/CSharpier.Tests/FormattingTests/TestFiles/xml/Attributes.test renamed to Src/CSharpier.Tests/FormattingTests/TestFiles/xml_strict/Attributes.test

File renamed without changes.

Src/CSharpier.Tests/FormattingTests/TestFiles/xml/BasicProject.test renamed to Src/CSharpier.Tests/FormattingTests/TestFiles/xml_strict/BasicProject.test

File renamed without changes.

0 commit comments

Comments
 (0)