Skip to content

Commit

Permalink
Dealing with more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Dec 25, 2024
1 parent 84dd9fc commit 3560bcb
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 16 deletions.
7 changes: 7 additions & 0 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ CancellationToken cancellationToken
return;
}

// TODO #819 can probably clean up how we get errors/warnings back here
if (codeFormattingResult.CompilationErrors.Any())
{
var errorMessage = new StringBuilder();
Expand All @@ -422,6 +423,12 @@ CancellationToken cancellationToken
return;
}

if (!codeFormattingResult.WarningMessage.IsBlank())
{
fileIssueLogger.WriteWarning(codeFormattingResult.WarningMessage);
return;
}

if (!codeFormattingResult.FailureMessage.IsBlank())
{
fileIssueLogger.WriteError(codeFormattingResult.FailureMessage);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<Element AttributeLineEndingDecimal="@('', '&#xA;')" />
<Element AttributeLineEndingHexidecimal="@('', '&#xA;')" />
<Element Attribute="&quot;&quot; &amp;&amp;" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<Element AttributeLineEndingDecimal="@('', '&#010;')" />
<Element AttributeLineEndingHexidecimal="@('', '&#xA;')" />
<Element Attribute="&quot;&quot; &amp;&amp;" />
</Project>
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/Samples/AllInOne.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@

Doc.HardLineIfNoPreviousLine
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/Samples/Scratch.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@

Doc.HardLineIfNoPreviousLine
1 change: 1 addition & 0 deletions Src/CSharpier/CodeFormatterResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal CodeFormatterResult() { }
public string Code { get; internal init; } = string.Empty;
internal string DocTree { get; init; } = string.Empty;
internal string AST { get; init; } = string.Empty;
internal string WarningMessage { get; init; } = string.Empty;
public IEnumerable<Diagnostic> CompilationErrors { get; internal init; } =
Enumerable.Empty<Diagnostic>();

Expand Down
19 changes: 10 additions & 9 deletions Src/CSharpier/Formatters/Xml/XNodePrinters/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

Expand Down Expand Up @@ -86,10 +87,10 @@ private static Doc PrintAttribute(XAttribute attribute, XmlAttribute xmlAttribut
// this makes use of both values to get the final value we want to display
string GetAttributeValue()
{
var xValue = attribute
.Value.Replace("\"", "&quot;")
.Replace("\r\n", "&#010;")
.Replace("\n", "&#010;");
// this gives us the attribute value with everything encoded after we remove the name + quotes
var xValue = attribute.ToString();
xValue = xValue[(xValue.IndexOf('=') + 2)..];
xValue = xValue[..^1];
var xmlValue = xmlAttribute.Value;

if (xValue == xmlValue)
Expand All @@ -112,11 +113,6 @@ string GetAttributeValue()
xmlChar = xmlValue[xmlIndex];
}

if (xChar == xmlChar || (xChar == ' ' && xmlChar == '\n'))
{
valueBuilder.Append(xmlChar);
}

if (xChar == '&')
{
do
Expand All @@ -128,6 +124,11 @@ string GetAttributeValue()
valueBuilder.Append(xChar);
}

if (xChar == xmlChar || (xChar == ' ' && xmlChar == '\n'))
{
valueBuilder.Append(xmlChar);
}

xIndex++;
xmlIndex++;
}
Expand Down
27 changes: 25 additions & 2 deletions Src/CSharpier/Formatters/Xml/XmlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,39 @@ internal static CodeFormatterResult Format(string xml, PrinterOptions printerOpt
// with xmlDocument we can't get the proper encoded values in an attribute
// with xDocument we can't retain any newlines in attributes
// so let's just use them both
var xDocument = XDocument.Parse(xml);
XDocument xDocument;
try
{
xDocument = XDocument.Parse(xml);
}
catch (XmlException)
{
return new CodeFormatterResult
{
Code = xml,
WarningMessage = "Appeared to be invalid xml so was not formatted.",
};
}
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var mapping = new Dictionary<XNode, XmlNode>();
CreateMapping(xDocument, xmlDocument, mapping);

// TODO #819 lots of new errors when formatting a second time
// TODO #819 Error ./efcore/eng/sdl-tsa-vars.config - Threw exception while formatting.
// Data at the root level is invalid. Line 1, position 1.
// Data at the root level is invalid. Line 1, position 1 - just ignore these?
// not all configs are xml, should we try to detect that?

/* TODO #819 Review
aspnetcore - https://github.com/belav/csharpier-repos/pull/121/files
*/

/* TODO #819 Errors
runtime
*/

var lineEnding = PrinterOptions.GetLineEnding(xml, printerOptions);
var printingContext = new XmlPrintingContext
{
Expand Down

0 comments on commit 3560bcb

Please sign in to comment.