Skip to content

Commit 7df4028

Browse files
committed
Working on adding xml whitespace ignore.
1 parent fee0857 commit 7df4028

File tree

16 files changed

+144
-36
lines changed

16 files changed

+144
-36
lines changed

Src/CSharpier.Core/CSharp/CSharpFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
155155
LineEnding = lineEnding,
156156
IndentSize = printerOptions.IndentSize,
157157
UseTabs = printerOptions.UseTabs,
158+
XmlWhitespaceSensitivity = XmlWhitespaceSensitivity.Strict,
158159
},
159160
};
160161
var document = Node.Print(rootNode, printingContext);
@@ -181,6 +182,7 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
181182
LineEnding = lineEnding,
182183
IndentSize = printerOptions.IndentSize,
183184
UseTabs = printerOptions.UseTabs,
185+
XmlWhitespaceSensitivity = XmlWhitespaceSensitivity.Strict,
184186
},
185187
};
186188
document = Node.Print(

Src/CSharpier.Core/CSharp/SyntaxPrinter/PrintingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class PrintingContextOptions
3636
public required string LineEnding { get; init; }
3737
public required int IndentSize { get; init; }
3838
public required bool UseTabs { get; init; }
39+
public required XmlWhitespaceSensitivity XmlWhitespaceSensitivity { get; init; }
3940
}
4041

4142
public class PrintingContextState

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static Doc Print(RawNode rawNode, PrintingContext context)
4242
*/
4343
(
4444
rawNode.Nodes.Count != 0
45-
&& Tag.NeedsToBorrowParentOpeningTagEndMarker(rawNode.Nodes.First())
45+
&& Tag.NeedsToBorrowParentOpeningTagEndMarker(rawNode.Nodes.First(), context)
4646
) || doNotBreakAttributes
4747
)
4848
{

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ 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+
4152
if (prevBetweenLine is not NullDoc)
4253
{
4354
if (prevBetweenLine is HardLine)
@@ -50,7 +61,14 @@ public static Doc Print(RawNode node, PrintingContext context)
5061
}
5162
else
5263
{
53-
leadingParts.Add(Doc.IfBreak(Doc.Null, Doc.SoftLine, groupIds[x - 1]));
64+
if (groupIds.Count > 1)
65+
{
66+
leadingParts.Add(Doc.IfBreak(Doc.Null, Doc.SoftLine, groupIds[x - 1]));
67+
}
68+
else
69+
{
70+
leadingParts.Add(prevBetweenLine);
71+
}
5472
}
5573
}
5674

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal static Doc Print(RawNode node, PrintingContext context)
4040
{
4141
List<Doc> doc =
4242
[
43-
Tag.PrintOpeningTagPrefix(node),
43+
Tag.PrintOpeningTagPrefix(node, context),
4444
GetTextValue(node),
4545
Tag.PrintClosingTagSuffix(node, context),
4646
];

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static Doc PrintOpeningTag(RawNode rawNode, PrintingContext context)
1111
return Doc.Concat(
1212
PrintOpeningTagStart(rawNode, context),
1313
Attributes.Print(rawNode, context),
14-
rawNode.IsEmpty ? Doc.Null : PrintOpeningTagEnd(rawNode)
14+
rawNode.IsEmpty ? Doc.Null : PrintOpeningTagEnd(rawNode, context)
1515
);
1616
}
1717

@@ -22,23 +22,23 @@ rawNode.PreviousNode is not null
2222
&& NeedsToBorrowNextOpeningTagStartMarker(rawNode.PreviousNode)
2323
? Doc.Null
2424
: Doc.Concat(
25-
PrintOpeningTagPrefix(rawNode),
25+
PrintOpeningTagPrefix(rawNode, context),
2626
PrintOpeningTagStartMarker(rawNode, context)
2727
);
2828
}
2929

30-
private static Doc PrintOpeningTagEnd(RawNode rawNode)
30+
private static Doc PrintOpeningTagEnd(RawNode rawNode, PrintingContext context)
3131
{
3232
return
3333
rawNode.Nodes.FirstOrDefault() is { } firstNode
34-
&& NeedsToBorrowParentOpeningTagEndMarker(firstNode)
34+
&& NeedsToBorrowParentOpeningTagEndMarker(firstNode, context)
3535
? Doc.Null
3636
: ">";
3737
}
3838

39-
public static Doc PrintOpeningTagPrefix(RawNode rawNode)
39+
public static Doc PrintOpeningTagPrefix(RawNode rawNode, PrintingContext context)
4040
{
41-
return NeedsToBorrowParentOpeningTagEndMarker(rawNode) ? ">" : "";
41+
return NeedsToBorrowParentOpeningTagEndMarker(rawNode, context) ? ">" : "";
4242
}
4343

4444
public static Doc PrintClosingTag(RawNode rawNode, PrintingContext context)
@@ -53,7 +53,7 @@ public static Doc PrintClosingTagStart(RawNode rawNode, PrintingContext context)
5353
{
5454
var lastChild = rawNode.Nodes.LastOrDefault();
5555

56-
return lastChild is not null && PrintParentClosingTagStartWithContent(lastChild)
56+
return lastChild is not null && PrintParentClosingTagStartWithContent(lastChild, context)
5757
? Doc.Null
5858
: PrintClosingTagStartMarker(rawNode, context);
5959
}
@@ -78,7 +78,7 @@ public static Doc PrintClosingTagEndMarker(RawNode rawNode)
7878

7979
public static Doc PrintClosingTagSuffix(RawNode rawNode, PrintingContext context)
8080
{
81-
return PrintParentClosingTagStartWithContent(rawNode)
81+
return PrintParentClosingTagStartWithContent(rawNode, context)
8282
? PrintClosingTagStartMarker(rawNode.Parent!, context)
8383
: NeedsToBorrowNextOpeningTagStartMarker(rawNode)
8484
? PrintOpeningTagStartMarker(rawNode.NextNode!, context)
@@ -113,7 +113,10 @@ private static bool NeedsToBorrowNextOpeningTagStartMarker(RawNode rawNode)
113113
;
114114
}
115115

116-
private static bool PrintParentClosingTagStartWithContent(RawNode rawNode)
116+
private static bool PrintParentClosingTagStartWithContent(
117+
RawNode rawNode,
118+
PrintingContext context
119+
)
117120
{
118121
/*
119122
* <p>
@@ -147,7 +150,8 @@ Life is demanding.
147150
</Element>
148151
</Root>
149152
*/
150-
return rawNode.NextNode is null
153+
return context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
154+
&& rawNode.NextNode is null
151155
&& rawNode.IsTextLike()
152156
&& rawNode.GetLastDescendant() is { NodeType: XmlNodeType.Text } textNode
153157
&& (
@@ -157,7 +161,10 @@ Life is demanding.
157161
);
158162
}
159163

160-
public static bool NeedsToBorrowParentOpeningTagEndMarker(RawNode rawNode)
164+
public static bool NeedsToBorrowParentOpeningTagEndMarker(
165+
RawNode rawNode,
166+
PrintingContext context
167+
)
161168
{
162169
/*
163170
* <p
@@ -168,7 +175,8 @@ public static bool NeedsToBorrowParentOpeningTagEndMarker(RawNode rawNode)
168175
* ><a
169176
* ^
170177
*/
171-
return rawNode.PreviousNode is null
178+
return context.Options.XmlWhitespaceSensitivity is XmlWhitespaceSensitivity.Strict
179+
&& rawNode.PreviousNode is null
172180
&& rawNode.NodeType is XmlNodeType.Text
173181
&& rawNode.Value![0] is not ('\r' or '\n');
174182
}

Src/CSharpier.Core/Xml/XmlFormatter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ PrinterOptions printerOptions
3535
LineEnding = lineEnding,
3636
IndentSize = printerOptions.IndentSize,
3737
UseTabs = printerOptions.UseTabs,
38+
XmlWhitespaceSensitivity = printerOptions.XmlWhitespaceSensitivity,
3839
},
3940
};
4041
var doc = Node.Print(rootNode, printingContext);

Src/CSharpier.Playground/ClientApp/src/AppContext.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AppState {
3737
showAst = getBoolean("showAst", false);
3838
showDoc = getBoolean("showDoc", false);
3939
hideNull = getBoolean("hideNull", false);
40+
xmlWhitespaceSensitivity = getString("xmlWhitespaceSensitivity", "Strict");
4041
doc = "";
4142
isLoading = false;
4243
hasErrors = false;
@@ -53,6 +54,11 @@ class AppState {
5354
this.formatter = value;
5455
};
5556

57+
setXmlWhitespaceSensitivity = (value: string) => {
58+
window.sessionStorage.setItem("xmlWhitespaceSensitivity", value);
59+
this.xmlWhitespaceSensitivity = value;
60+
};
61+
5662
setIndentSize = (value: number) => {
5763
window.sessionStorage.setItem("indentSize", value.toString(10));
5864
this.indentSize = value;
@@ -119,6 +125,7 @@ class AppState {
119125
this.indentSize,
120126
this.useTabs,
121127
this.formatter,
128+
this.xmlWhitespaceSensitivity,
122129
);
123130

124131
runInAction(() => {

Src/CSharpier.Playground/ClientApp/src/Controls.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const Controls = observer(() => {
1313
setUseTabs,
1414
formatter,
1515
setFormatter,
16+
xmlWhitespaceSensitivity,
17+
setXmlWhitespaceSensitivity,
1618
showDoc,
1719
setShowDoc,
1820
hideNull,
@@ -47,6 +49,19 @@ export const Controls = observer(() => {
4749
<option value="CSharpScript">C# Script</option>
4850
<option value="XML">XML</option>
4951
</select>
52+
{formatter === "XML" && (
53+
<>
54+
<label>Whitespace Sensitivity</label>
55+
<select
56+
value={xmlWhitespaceSensitivity}
57+
onChange={e => setXmlWhitespaceSensitivity(e.target.value)}
58+
>
59+
<option value="Strict">Strict</option>
60+
<option value="Xaml">Xaml</option>
61+
<option value="Ignore">Ignore</option>
62+
</select>
63+
</>
64+
)}
5065
</div>
5166
<div>
5267
<h3>Debug</h3>

Src/CSharpier.Playground/ClientApp/src/FormatCode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export const formatCode = async (
88
indentSize: number,
99
useTabs: boolean,
1010
formatter: string,
11+
xmlWhitespaceSensitivity: string,
1112
) => {
1213
const makeRequest = async () => {
1314
const response = await fetch("/Format", {
1415
method: "POST",
15-
body: JSON.stringify({ code, printWidth, indentSize, useTabs, formatter }),
16+
body: JSON.stringify({ code, printWidth, indentSize, useTabs, formatter, xmlWhitespaceSensitivity }),
1617
headers: {
1718
"Content-Type": "application/json",
1819
"cache-control": "no-cache",

0 commit comments

Comments
 (0)