diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index d2f1f9a97..c0078a494 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -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(); @@ -422,6 +423,12 @@ CancellationToken cancellationToken return; } + if (!codeFormattingResult.WarningMessage.IsBlank()) + { + fileIssueLogger.WriteWarning(codeFormattingResult.WarningMessage); + return; + } + if (!codeFormattingResult.FailureMessage.IsBlank()) { fileIssueLogger.WriteError(codeFormattingResult.FailureMessage); diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedLineEnding.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedLineEnding.test deleted file mode 100644 index 0eb046873..000000000 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedLineEnding.test +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.expected.test new file mode 100644 index 000000000..c1be262a5 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.expected.test @@ -0,0 +1,5 @@ + + + + + diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.test new file mode 100644 index 000000000..2fed68f8a --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/xml/EncodedValues.test @@ -0,0 +1,5 @@ + + + + + diff --git a/Src/CSharpier.Tests/Samples/AllInOne.test b/Src/CSharpier.Tests/Samples/AllInOne.test index 5f282702b..1d8ef2950 100644 --- a/Src/CSharpier.Tests/Samples/AllInOne.test +++ b/Src/CSharpier.Tests/Samples/AllInOne.test @@ -1 +1 @@ - \ No newline at end of file +Doc.HardLineIfNoPreviousLine \ No newline at end of file diff --git a/Src/CSharpier.Tests/Samples/Scratch.test b/Src/CSharpier.Tests/Samples/Scratch.test index 5f282702b..1d8ef2950 100644 --- a/Src/CSharpier.Tests/Samples/Scratch.test +++ b/Src/CSharpier.Tests/Samples/Scratch.test @@ -1 +1 @@ - \ No newline at end of file +Doc.HardLineIfNoPreviousLine \ No newline at end of file diff --git a/Src/CSharpier/CodeFormatterResult.cs b/Src/CSharpier/CodeFormatterResult.cs index 253cf151c..d8eab98ab 100644 --- a/Src/CSharpier/CodeFormatterResult.cs +++ b/Src/CSharpier/CodeFormatterResult.cs @@ -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 CompilationErrors { get; internal init; } = Enumerable.Empty(); diff --git a/Src/CSharpier/Formatters/Xml/XNodePrinters/Attributes.cs b/Src/CSharpier/Formatters/Xml/XNodePrinters/Attributes.cs index c1bfa13b4..b239a4842 100644 --- a/Src/CSharpier/Formatters/Xml/XNodePrinters/Attributes.cs +++ b/Src/CSharpier/Formatters/Xml/XNodePrinters/Attributes.cs @@ -1,4 +1,5 @@ using System.Text; +using System.Text.RegularExpressions; using System.Xml; using System.Xml.Linq; @@ -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("\"", """) - .Replace("\r\n", " ") - .Replace("\n", " "); + // 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) @@ -112,11 +113,6 @@ string GetAttributeValue() xmlChar = xmlValue[xmlIndex]; } - if (xChar == xmlChar || (xChar == ' ' && xmlChar == '\n')) - { - valueBuilder.Append(xmlChar); - } - if (xChar == '&') { do @@ -128,6 +124,11 @@ string GetAttributeValue() valueBuilder.Append(xChar); } + if (xChar == xmlChar || (xChar == ' ' && xmlChar == '\n')) + { + valueBuilder.Append(xmlChar); + } + xIndex++; xmlIndex++; } diff --git a/Src/CSharpier/Formatters/Xml/XmlFormatter.cs b/Src/CSharpier/Formatters/Xml/XmlFormatter.cs index a515efd22..3743afe9f 100644 --- a/Src/CSharpier/Formatters/Xml/XmlFormatter.cs +++ b/Src/CSharpier/Formatters/Xml/XmlFormatter.cs @@ -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(); 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 {