From 1abff3004af0b3c64257596c9b04cd52af852ca5 Mon Sep 17 00:00:00 2001 From: "Denis Kuzmin (github/3F)" Date: Thu, 4 Apr 2024 00:34:19 +0300 Subject: [PATCH] Opened LineBuilder for 3rd party handlers public ctor & Default NewLine value changed to Environment.NewLine (platform dependent) Also RemoveNewLine() renamed as RemoveLastNewLine() --- MvsSln/Core/LineBuilder.cs | 78 +++++++++++++++++-- MvsSln/Core/ObjHandlers/WProject.cs | 2 +- .../Core/ObjHandlers/WProjectSolutionItems.cs | 2 +- .../Core/ObjHandlers/WVisualStudioVersion.cs | 2 +- MvsSlnTest/Core/LineBuilderTest.cs | 29 ++++--- Readme.md | 2 +- 6 files changed, 97 insertions(+), 18 deletions(-) diff --git a/MvsSln/Core/LineBuilder.cs b/MvsSln/Core/LineBuilder.cs index 2bd807f..f2c2dd7 100644 --- a/MvsSln/Core/LineBuilder.cs +++ b/MvsSln/Core/LineBuilder.cs @@ -20,6 +20,9 @@ public sealed class LineBuilder private string newline; + /// + /// Tab character or equivalent used in the current instance in related operations. + /// public string Tab { get => tab; @@ -30,6 +33,10 @@ public string Tab } } + /// + /// EOL character (or a sequence of characters) used for newline operations in the current instance. + /// + /// null as set value causes the value to be set using public string NewLine { get => newline; @@ -38,27 +45,67 @@ public string NewLine public int Length => sb.Length; + /// + /// Adds string value to the current character set. + /// + /// String value to be added to the current character set. + /// Self reference to continue the chain. + /// public LineBuilder Append(string value) { sb.Append(value ?? throw new ArgumentNullException(nameof(value))); return this; } + /// + /// Adds string value together with to the current character set. + /// + /// public LineBuilder AppendLine(string value) => Append(value).Append(newline); + /// + /// Adds to the current character set. + /// + /// Self reference to continue the chain. public LineBuilder AppendLine() => Append(newline); + /// + /// using first level indentation. + /// + /// public LineBuilder AppendLv1(string value) => Append(tab).Append(value); + /// + /// using second level indentation. + /// + /// public LineBuilder AppendLv2(string value) => Append(doubleTab).Append(value); + /// + /// using first level indentation. + /// + /// public LineBuilder AppendLv1Line(string value) => AppendLv1(value).AppendLine(); + /// + /// using second level indentation. + /// + /// public LineBuilder AppendLv2Line(string value) => AppendLv2(value).AppendLine(); - public LineBuilder RemoveNewLine() + /// + /// Remove the last from the current instance if present. + /// + /// Self reference to continue the chain. + public LineBuilder RemoveLastNewLine() => ContainsLast(newline) ? RemoveLast(newline.Length) : this; + /// + /// Remove the last characters from the current instance. + /// + /// Number of characters being removed. + /// Self reference to continue the chain. + /// public LineBuilder RemoveLast(int length) { if(length < 0 || length > sb.Length) @@ -69,18 +116,29 @@ public LineBuilder RemoveLast(int length) return Remove(sb.Length - length, length); } + /// public LineBuilder Remove(int startIndex, int length) { sb.Remove(startIndex, length); return this; } + /// + /// Removes all characters from the current instance. + /// + /// public LineBuilder Clear() { sb.Clear(); return this; } + /// + /// Checks whether there is a sequence from the passed value at the end. + /// + /// + /// true if the value being tested is at the end of the sequence of the current instance. + /// public bool ContainsLast(string value) { if(value == null) throw new ArgumentNullException(nameof(value)); @@ -88,20 +146,30 @@ public bool ContainsLast(string value) return value == ToString(sb.Length - value.Length, value.Length); } - public string ToString(bool removeNewLine) + /// If true, remove at the end of the resulting string if present. + /// + public string ToString(bool noLastNewLine) { - if(!removeNewLine) return sb.ToString(); + if(!noLastNewLine) return sb.ToString(); return ContainsLast(newline) ? sb.ToString(0, sb.Length - newline.Length) : sb.ToString(); } + /// public string ToString(int startIndex, int length) => sb.ToString(startIndex, length); + /// public override string ToString() => sb.ToString(); - internal LineBuilder(string newline = "\r\n", string tab = "\t") + public LineBuilder() + : this(newline: null) { - Tab = tab ?? throw new ArgumentNullException(nameof(tab)); + + } + + public LineBuilder(string newline, string tab = "\t") + { + Tab = tab; NewLine = newline; sb = new StringBuilder(); diff --git a/MvsSln/Core/ObjHandlers/WProject.cs b/MvsSln/Core/ObjHandlers/WProject.cs index ff48cba..b95240a 100644 --- a/MvsSln/Core/ObjHandlers/WProject.cs +++ b/MvsSln/Core/ObjHandlers/WProject.cs @@ -52,7 +52,7 @@ public override string Extract(object data) lbuilder.AppendLine(EndProject); } - return lbuilder.ToString(removeNewLine: true); + return lbuilder.ToString(noLastNewLine: true); } /// List of projects in solution. diff --git a/MvsSln/Core/ObjHandlers/WProjectSolutionItems.cs b/MvsSln/Core/ObjHandlers/WProjectSolutionItems.cs index c4f9ff4..85a8835 100644 --- a/MvsSln/Core/ObjHandlers/WProjectSolutionItems.cs +++ b/MvsSln/Core/ObjHandlers/WProjectSolutionItems.cs @@ -41,7 +41,7 @@ public override string Extract(object data) lbuilder.AppendLv1Line(EndProjectSection).AppendLine(EndProject); } - return lbuilder.ToString(removeNewLine: true); + return lbuilder.ToString(noLastNewLine: true); } /// List of solution folders. diff --git a/MvsSln/Core/ObjHandlers/WVisualStudioVersion.cs b/MvsSln/Core/ObjHandlers/WVisualStudioVersion.cs index ffbb621..4867297 100644 --- a/MvsSln/Core/ObjHandlers/WVisualStudioVersion.cs +++ b/MvsSln/Core/ObjHandlers/WVisualStudioVersion.cs @@ -40,7 +40,7 @@ public override string Extract(object data) lbuilder.AppendLine($"{MinimumVisualStudioVersion} = {header.MinimumVisualStudioVersion}"); } - return lbuilder.ToString(removeNewLine: true); + return lbuilder.ToString(noLastNewLine: true); } } } diff --git a/MvsSlnTest/Core/LineBuilderTest.cs b/MvsSlnTest/Core/LineBuilderTest.cs index 22a8caa..8638118 100644 --- a/MvsSlnTest/Core/LineBuilderTest.cs +++ b/MvsSlnTest/Core/LineBuilderTest.cs @@ -31,11 +31,11 @@ public void BuilderTest1(string nl, string tab) Assert.Equal(tab, lb.Tab); Assert.Equal(nl, lb.NewLine); - Assert.Equal(exp, lb.ToString(removeNewLine: true)); - Assert.Equal($"{exp}{nl}", lb.ToString(removeNewLine: false)); + Assert.Equal(exp, lb.ToString(noLastNewLine: true)); + Assert.Equal($"{exp}{nl}", lb.ToString(noLastNewLine: false)); Assert.Equal($"{exp}{nl}", lb.ToString()); - lb.RemoveNewLine(); + lb.RemoveLastNewLine(); Assert.Equal(exp, lb.ToString()); lb.Clear(); @@ -64,11 +64,11 @@ public void BuilderTest2(string nl, string tab) Assert.Equal(tab, lb.Tab); Assert.Equal(nl, lb.NewLine); - Assert.Equal(exp, lb.ToString(removeNewLine: true)); - Assert.Equal(exp, lb.ToString(removeNewLine: false)); + Assert.Equal(exp, lb.ToString(noLastNewLine: true)); + Assert.Equal(exp, lb.ToString(noLastNewLine: false)); Assert.Equal(exp, lb.ToString()); - lb.RemoveNewLine(); + lb.RemoveLastNewLine(); Assert.Equal(exp, lb.ToString()); lb.Clear(); @@ -95,6 +95,17 @@ public void CtorTest1(string nl, string tab) } #endif + [Fact] + public void CtorTest2() + { + LineBuilder lb = new(); + lb.AppendLine("1").AppendLine("2").AppendLine("3") + .RemoveLastNewLine(); + + string nl = Environment.NewLine; + Assert.Equal($"1{nl}2{nl}3", lb.ToString()); + } + [Fact] public void ContainsLastTest1() { @@ -137,16 +148,16 @@ public void ToStringTest1() { LineBuilder lb = new(); - Assert.Equal(string.Empty, lb.ToString(removeNewLine: true)); + Assert.Equal(string.Empty, lb.ToString(noLastNewLine: true)); Assert.Equal(0, lb.Length); string wrd = "Hello"; lb.AppendLine(wrd); Assert.Equal(wrd.Length + lb.NewLine.Length, lb.Length); - Assert.Equal(wrd, lb.ToString(removeNewLine: true)); + Assert.Equal(wrd, lb.ToString(noLastNewLine: true)); Assert.Equal(wrd.Length + lb.NewLine.Length, lb.Length); - lb.RemoveNewLine(); + lb.RemoveLastNewLine(); Assert.Equal(wrd.Length, lb.Length); } diff --git a/Readme.md b/Readme.md index 0dd2dbd..95016b4 100644 --- a/Readme.md +++ b/Readme.md @@ -17,7 +17,7 @@ We're waiting for your awesome contributions! | Releases | Windows | Linux |-------------|---------|-------- | [📦 ![NuGet](https://img.shields.io/nuget/v/MvsSln.svg)](https://www.nuget.org/packages/MvsSln/) | [![status](https://ci.appveyor.com/api/projects/status/6uunsds889rhkpo2/branch/master?svg=true)](https://ci.appveyor.com/project/3Fs/mvssln-fxjnf/branch/master) | [![status](https://ci.appveyor.com/api/projects/status/vdt3taxswrxo37tt/branch/master?svg=true)](https://ci.appveyor.com/project/3Fs/mvssln-2d2c2/branch/master) -| [![release](https://img.shields.io/github/release/3F/MvsSln.svg)](https://github.com/3F/MvsSln/releases/latest) | [![Tests](https://img.shields.io/appveyor/tests/3Fs/mvssln-fxjnf/master.svg)](https://ci.appveyor.com/project/3Fs/mvssln-fxjnf/build/tests) +| [![release](https://img.shields.io/github/release/3F/MvsSln.svg)](https://github.com/3F/MvsSln/releases/latest) | [![Tests](https://img.shields.io/appveyor/tests/3Fs/mvssln-fxjnf/master.svg)](https://ci.appveyor.com/project/3Fs/mvssln-fxjnf/build/tests) | [![Tests](https://img.shields.io/appveyor/tests/3Fs/mvssln-2d2c2/master.svg)](https://ci.appveyor.com/project/3Fs/mvssln-2d2c2/build/tests)