Skip to content

Commit dbeba87

Browse files
committed
getting this really close to working
1 parent 45145f3 commit dbeba87

File tree

15 files changed

+614
-571
lines changed

15 files changed

+614
-571
lines changed

Src/CSharpier.Core/Xml/RawAttributeReader.cs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,45 @@ namespace CSharpier.Core.Xml;
44

55
internal class RawAttributeReader(string originalXml, string endOfLine, XmlReader xmlReader)
66
{
7+
private readonly IXmlLineInfo xmlLineInfo = (xmlReader as IXmlLineInfo)!;
8+
79
private readonly string[] lines = originalXml.Split(["\r\n", "\n"], StringSplitOptions.None);
810

9-
// TODO 1679 don't need to pass xmlLineInfo here
10-
public string? GetRawAttribute(IXmlLineInfo xmlLineInfo, string attributeName)
11+
public RawAttribute[] GetAttributes()
12+
{
13+
xmlReader.MoveToFirstAttribute();
14+
15+
var result = new RawAttribute[xmlReader.AttributeCount];
16+
17+
for (var x = 0; x < xmlReader.AttributeCount; x++)
18+
{
19+
result[x] = new RawAttribute
20+
{
21+
Name = xmlReader.Name,
22+
Value = this.GetRawAttribute(xmlReader.Name).Replace("\"", "&quot;"),
23+
};
24+
25+
xmlReader.MoveToNextAttribute();
26+
}
27+
28+
xmlReader.MoveToElement();
29+
30+
return result;
31+
}
32+
33+
private string GetRawAttribute(string attributeName)
1134
{
12-
var lineNumber = xmlLineInfo.LineNumber - 1;
35+
var lineNumber = this.xmlLineInfo.LineNumber - 1;
1336
var line = this.lines[lineNumber];
1437

1538
var index = line.IndexOf(
1639
attributeName,
17-
xmlLineInfo.LinePosition - 1,
40+
this.xmlLineInfo.LinePosition - 1,
1841
StringComparison.Ordinal
1942
);
2043
if (index < 0)
2144
{
22-
return null;
45+
return string.Empty;
2346
}
2447

2548
var firstQuote = line.IndexOfAny(['"', '\''], index);
@@ -48,28 +71,4 @@ internal class RawAttributeReader(string originalXml, string endOfLine, XmlReade
4871

4972
return result;
5073
}
51-
52-
public RawAttribute[] GetAttributes()
53-
{
54-
xmlReader.MoveToFirstAttribute();
55-
56-
var result = new RawAttribute[xmlReader.AttributeCount];
57-
58-
var xmlLineInfo = (xmlReader as IXmlLineInfo)!;
59-
60-
for (var x = 0; x < xmlReader.AttributeCount; x++)
61-
{
62-
result[x] = new RawAttribute
63-
{
64-
Name = xmlReader.Name,
65-
Value = this.GetRawAttribute(xmlLineInfo, xmlReader.Name),
66-
};
67-
68-
xmlReader.MoveToNextAttribute();
69-
}
70-
71-
xmlReader.MoveToElement();
72-
73-
return result;
74-
}
7574
}

Src/CSharpier.Core/Xml/RawElement.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

Src/CSharpier.Core/Xml/RawElementReader.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

Src/CSharpier.Core/Xml/RawNode.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Xml;
2+
3+
namespace CSharpier.Core.Xml;
4+
5+
// TODO 1679 call this RawNode instead?
6+
internal class RawNode
7+
{
8+
public RawNode? Parent { get; set; }
9+
public RawNode? PreviousNode { get; set; }
10+
public RawNode? NextNode { get; set; }
11+
public required string? Name { get; set; }
12+
public required XmlNodeType NodeType { get; set; }
13+
public required bool IsEmpty { get; set; }
14+
public required RawAttribute[] Attributes { get; set; }
15+
public List<RawNode> Nodes { get; set; } = new();
16+
public string? Value { get; set; }
17+
18+
public bool IsTextLike()
19+
{
20+
return this.NodeType is XmlNodeType.Text or XmlNodeType.Comment;
21+
}
22+
23+
public RawNode GetLastDescendant()
24+
{
25+
return this.NodeType is XmlNodeType.Element ? this.Nodes.LastOrDefault() ?? this : this;
26+
}
27+
}

Src/CSharpier.Core/Xml/XNodePrinters/Attributes.cs renamed to Src/CSharpier.Core/Xml/RawNodePrinters/Attributes.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44
using CSharpier.Core.CSharp.SyntaxPrinter;
55
using CSharpier.Core.DocTypes;
66

7-
namespace CSharpier.Core.Xml.XNodePrinters;
7+
namespace CSharpier.Core.Xml.RawNodePrinters;
88

99
internal static class Attributes
1010
{
11-
public static Doc Print(RawElement rawElement, PrintingContext context)
11+
public static Doc Print(RawNode rawNode, PrintingContext context)
1212
{
13-
if (rawElement.Attributes.Length == 0)
13+
if (rawNode.Attributes.Length == 0)
1414
{
15-
return rawElement.IsEmpty ? " " : Doc.Null;
15+
return rawNode.IsEmpty ? " " : Doc.Null;
1616
}
1717

1818
var printedAttributes = new List<Doc>();
19-
foreach (var attribute in rawElement.Attributes)
19+
foreach (var attribute in rawNode.Attributes)
2020
{
2121
printedAttributes.Add(PrintAttribute(attribute));
2222
}
2323

2424
var doNotBreakAttributes =
25-
rawElement.Attributes.Length == 1
26-
&& !rawElement.Attributes[0].Value.Contains('\n')
27-
&& (rawElement.Nodes.Any(o => o.NodeType is XmlNodeType.Element) || rawElement.IsEmpty);
25+
rawNode.Attributes.Length == 1
26+
&& !rawNode.Attributes[0].Value.Contains('\n')
27+
&& (rawNode.Nodes.Any(o => o.NodeType is XmlNodeType.Element) || rawNode.IsEmpty);
2828
var attributeLine = Doc.Line;
2929

3030
var parts = new List<Doc>
@@ -43,16 +43,16 @@ public static Doc Print(RawElement rawElement, PrintingContext context)
4343
* >456
4444
*/
4545
(
46-
rawElement.Nodes.Count != 0
47-
&& Tag.NeedsToBorrowParentOpeningTagEndMarker(rawElement.Nodes.First())
46+
rawNode.Nodes.Count != 0
47+
&& Tag.NeedsToBorrowParentOpeningTagEndMarker(rawNode.Nodes.First())
4848
) || doNotBreakAttributes
4949
)
5050
{
51-
parts.Add(rawElement.IsEmpty ? " " : "");
51+
parts.Add(rawNode.IsEmpty ? " " : "");
5252
}
5353
else
5454
{
55-
parts.Add(rawElement.IsEmpty ? Doc.Line : Doc.SoftLine);
55+
parts.Add(rawNode.IsEmpty ? Doc.Line : Doc.SoftLine);
5656
}
5757

5858
return Doc.Concat(parts);

Src/CSharpier.Core/Xml/XNodePrinters/Element.cs renamed to Src/CSharpier.Core/Xml/RawNodePrinters/Element.cs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
using CSharpier.Core.CSharp.SyntaxPrinter;
44
using CSharpier.Core.DocTypes;
55

6-
namespace CSharpier.Core.Xml.XNodePrinters;
6+
namespace CSharpier.Core.Xml.RawNodePrinters;
77

88
internal static class Element
99
{
10-
internal static Doc Print(RawElement rawElement, XmlPrintingContext context)
10+
internal static Doc Print(RawNode rawNode, XmlPrintingContext context)
1111
{
1212
var shouldHugContent = false;
1313
var attrGroupId = context.GroupFor("element-attr-group-id");
1414

15-
// TODO 1679 somewhere in here we should return the list of RawElements
16-
// TODO 1679 or we should just convert the reader into RawElements + attributes + etc
17-
1815
Doc PrintChildrenDoc()
1916
{
20-
var childContent = ElementChildren.Print(rawElement, context);
17+
var childContent = ElementChildren.Print(rawNode, context);
2118

2219
if (shouldHugContent)
2320
{
@@ -37,18 +34,15 @@ Doc PrintLineBeforeChildren()
3734
return Doc.IfBreak(Doc.SoftLine, "", attrGroupId);
3835
}
3936

40-
// if (
41-
// node.Nodes().FirstOrDefault()
42-
// is not XCData
43-
// and XText { Value: ['\n', ..] or ['\r', ..] }
44-
// )
45-
// {
46-
// return Doc.LiteralLine;
47-
// }
48-
49-
if (rawElement.Attributes.Length == 0
50-
// TODO 1679 && node.Nodes().ToList() is [XText] and not [XCData]
37+
if (
38+
rawNode.Nodes.FirstOrDefault() is
39+
{ NodeType: XmlNodeType.Text, Value: ['\n', ..] or ['\r', ..] }
5140
)
41+
{
42+
return Doc.LiteralLine;
43+
}
44+
45+
if (rawNode.Attributes.Length == 0 && rawNode.Nodes is [{ NodeType: XmlNodeType.Text }])
5246
{
5347
return Doc.Null;
5448
}
@@ -64,9 +58,7 @@ Doc PrintLineAfterChildren()
6458
return Doc.IfBreak(Doc.SoftLine, "", attrGroupId);
6559
}
6660

67-
if (rawElement.Attributes.Length == 0
68-
// TODO 1679 && node.Nodes().ToList() is [XText] and not [XCData]
69-
)
61+
if (rawNode.Attributes.Length == 0 && rawNode.Nodes is [{ NodeType: XmlNodeType.Text }])
7062
{
7163
return Doc.Null;
7264
}
@@ -75,10 +67,10 @@ Doc PrintLineAfterChildren()
7567

7668
Doc PrintElementContent()
7769
{
78-
var elementContent = rawElement.IsEmpty
70+
var elementContent = rawNode.IsEmpty
7971
? Doc.Null
8072
: Doc.Concat(
81-
ForceBreakContent(rawElement) ? Doc.BreakParent : "",
73+
ForceBreakContent(rawNode) ? Doc.BreakParent : "",
8274
PrintChildrenDoc(),
8375
PrintLineAfterChildren()
8476
);
@@ -87,15 +79,15 @@ Doc PrintElementContent()
8779
}
8880

8981
return Doc.Group(
90-
Doc.GroupWithId(attrGroupId, Tag.PrintOpeningTag(rawElement, context)),
82+
Doc.GroupWithId(attrGroupId, Tag.PrintOpeningTag(rawNode, context)),
9183
PrintElementContent(),
92-
Tag.PrintClosingTag(rawElement, context)
84+
Tag.PrintClosingTag(rawNode, context)
9385
);
9486
}
9587

96-
private static bool ForceBreakContent(RawElement node)
88+
private static bool ForceBreakContent(RawNode rawNode)
9789
{
98-
var childNode = node.Nodes.Count == 1 ? node.Nodes.First() : null;
90+
var childNode = rawNode.Nodes.Count == 1 ? rawNode.Nodes.First() : null;
9991

10092
return childNode is not null
10193
&& childNode.NodeType is XmlNodeType.CDATA or not XmlNodeType.Text;

Src/CSharpier.Core/Xml/XNodePrinters/ElementChildren.cs renamed to Src/CSharpier.Core/Xml/RawNodePrinters/ElementChildren.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
using CSharpier.Core.DocTypes;
33
using CSharpier.Core.Utilities;
44

5-
namespace CSharpier.Core.Xml.XNodePrinters;
5+
namespace CSharpier.Core.Xml.RawNodePrinters;
66

77
internal static class ElementChildren
88
{
9-
public static Doc Print(RawElement element, XmlPrintingContext context)
9+
public static Doc Print(RawNode node, XmlPrintingContext context)
1010
{
1111
var groupIds = new List<string>();
12-
foreach (var _ in element.Nodes)
12+
foreach (var _ in node.Nodes)
1313
{
1414
groupIds.Add(context.GroupFor("symbol"));
1515
}
1616

17-
var result = new ValueListBuilder<Doc>(element.Nodes.Count * 5);
17+
var result = new ValueListBuilder<Doc>(node.Nodes.Count * 5);
1818
var x = 0;
19-
foreach (var childNode in element.Nodes)
19+
foreach (var childNode in node.Nodes)
2020
{
2121
var prevParts = new ValueListBuilder<Doc>([null, null]);
2222
var leadingParts = new ValueListBuilder<Doc>([null, null]);
@@ -80,7 +80,7 @@ public static Doc Print(RawElement element, XmlPrintingContext context)
8080
return Doc.Concat(ref result);
8181
}
8282

83-
public static Doc PrintChild(RawElement child, XmlPrintingContext context)
83+
public static Doc PrintChild(RawNode child, XmlPrintingContext context)
8484
{
8585
// should we try to support csharpier-ignore some day?
8686
// if (HasPrettierIgnore(child))
@@ -103,7 +103,7 @@ public static Doc PrintChild(RawElement child, XmlPrintingContext context)
103103
return Node.Print(child, context);
104104
}
105105

106-
public static Doc PrintBetweenLine(RawElement prevNode, RawElement nextNode)
106+
public static Doc PrintBetweenLine(RawNode prevNode, RawNode nextNode)
107107
{
108108
return
109109
(prevNode.NodeType is XmlNodeType.Text && nextNode.NodeType is XmlNodeType.Comment)

0 commit comments

Comments
 (0)