From 467397badf50362f9086f7d6d218791b80fb9ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20S=C3=A1nchez=20L=C3=B3pez?= <1175054+carlossanlop@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:49:36 -0700 Subject: [PATCH] Fix more cases (#173) * Improve the logic that wraps paragraphs with - Join paragraphs separated by newlines. - Split paragraphs that end in "." or ":". - Treat "-or-" and "- or -" as their own paragraphs. - Make sure single paragraphs are not wrapped. - Make sure empty newlines are ignored. * Tests * Version bump. --- src/PortToDocs/src/app/PortToDocs.csproj | 2 +- src/PortToDocs/src/libraries/XmlHelper.cs | 52 +++++++++++++-- .../tests/PortToDocs.Strings.Tests.cs | 64 ++++++++++++++++++- 3 files changed, 108 insertions(+), 10 deletions(-) diff --git a/src/PortToDocs/src/app/PortToDocs.csproj b/src/PortToDocs/src/app/PortToDocs.csproj index 09f8d36..5460e42 100644 --- a/src/PortToDocs/src/app/PortToDocs.csproj +++ b/src/PortToDocs/src/app/PortToDocs.csproj @@ -7,7 +7,7 @@ enable true true - 1.4 + 1.5 diff --git a/src/PortToDocs/src/libraries/XmlHelper.cs b/src/PortToDocs/src/libraries/XmlHelper.cs index e4582c6..f22a71c 100644 --- a/src/PortToDocs/src/libraries/XmlHelper.cs +++ b/src/PortToDocs/src/libraries/XmlHelper.cs @@ -203,19 +203,57 @@ public static string GetFormattedAsXml(string value, bool removeUndesiredEndline private static string ReplaceEndLinesWithParas(string updatedValue) { string[] splitted = updatedValue.Split(_splittingSeparators, _splittingStringSplitOptions); - bool moreThanOne = splitted.Count() > 1; + + if (splitted.Length == 1) + { + // No need to add paras + return splitted[0]; + } StringBuilder newValue = new(); - foreach (string s in splitted) + ReadOnlySpan ros; + bool addStartParaNext = true; + for (int i = 0; i < splitted.Length; i++) { - if (moreThanOne && !s.StartsWith("")) + ros = splitted[i]; + + if (ros.StartsWith("") && ros.EndsWith("")) { - newValue.Append(""); + // No change + newValue.Append(ros); + // Next time we find a line not surrounded by paras, we need to add the starting one first + addStartParaNext = true; } - newValue.Append(s); - if (moreThanOne && !s.EndsWith("")) + else { - newValue.Append(""); + if (addStartParaNext) + { + newValue.Append(""); + addStartParaNext = false; + } + + if (splitted[i].Equals("- or -")) + { + newValue.Append("-or-"); + } + else + { + newValue.Append(ros); + } + if (!ros.EndsWith("")) + { + if (ros[^1] is '.' or ':' || (i + 1) >= splitted.Length || splitted[i].Equals("-or-") || splitted[i].Equals("- or -")) + { + // We're done with this line + newValue.Append(""); + addStartParaNext = true; + } + else + { + // Space separator, the next line will get appended next + newValue.Append(' '); + } + } } } diff --git a/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs b/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs index 2b62e6a..802540b 100644 --- a/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs +++ b/src/PortToDocs/tests/PortToDocs.Strings.Tests.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; using System.Collections.Generic; using System.Text; using System.Xml.Linq; @@ -2466,6 +2467,67 @@ I am paragraph number three. TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration); } + [Fact] + public void Para_EdgeCases() + { + // Convert triple slash new lines to para xml items. If there are paras too, keep them. + + string originalIntellisense = @" + + + MyAssembly + + + + I am paragraph one with pre-existing paras. +I am paragraph number two but I am +divided into two lines. I am paragraph three but I am in the same line, my spacing between lines should be ignored + and the next newlines should be ignored. + + + I am the fourth paragraph with pre-existing paras. + + + +"; + + string originalDocs = @" + + + MyAssembly + + + To be added. + To be added. + + +"; + + string expectedDocs = @" + + + MyAssembly + + + + I am paragraph one with pre-existing paras. + I am paragraph number two but I am divided into two lines. I am paragraph three but I am in the same line, my spacing between lines should be ignored and the next newlines should be ignored. + I am the fourth paragraph with pre-existing paras. + + To be added. + + +"; + + Configuration configuration = new() + { + MarkdownRemarks = true + }; + configuration.IncludedAssemblies.Add(FileTestData.TestAssembly); + + TestWithStrings(originalIntellisense, originalDocs, expectedDocs, configuration); + } + [Fact] public void Convert_CodeDataDevCommentType_To_ExpectedElementNames() { @@ -2619,6 +2681,4 @@ private static void TestWithStrings(List intellisenseFiles, List