Skip to content

Commit d31ff0b

Browse files
committed
Added new formatting option to remove successive blank lines.
1 parent 9efc01d commit d31ff0b

File tree

7 files changed

+82
-17
lines changed

7 files changed

+82
-17
lines changed

src/IniFile/Ini.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,7 @@ public void Format(IniFormatOptions options = null)
354354
Section section = this[s];
355355

356356
// Reset padding for each minor item in the section
357-
foreach (MinorIniItem minorItem in section.Items)
358-
{
359-
if (minorItem is BlankLine blankLine)
360-
blankLine.Padding.Reset();
361-
else if (minorItem is Comment comment)
362-
comment.Padding.Reset();
363-
}
357+
FormatMinorItems(section.Items, options.RemoveSuccessiveBlankLines);
364358

365359
// Insert blank line between sections, if specified by options
366360
if (options.EnsureBlankLineBetweenSections)
@@ -377,13 +371,7 @@ public void Format(IniFormatOptions options = null)
377371
Property property = section[p];
378372

379373
// Reset padding for each minor item in the property
380-
foreach (MinorIniItem minorItem in property.Items)
381-
{
382-
if (minorItem is BlankLine blankLine)
383-
blankLine.Padding.Reset();
384-
else if (minorItem is Comment comment)
385-
comment.Padding.Reset();
386-
}
374+
FormatMinorItems(property.Items, options.RemoveSuccessiveBlankLines);
387375

388376
// Insert blank line between properties, if specified
389377
if (options.EnsureBlankLineBetweenProperties)
@@ -409,13 +397,24 @@ public void Format(IniFormatOptions options = null)
409397
}
410398

411399
// Format any remaining trailing items
412-
foreach (MinorIniItem trailingItem in TrailingItems)
400+
FormatMinorItems(TrailingItems, options.RemoveSuccessiveBlankLines);
401+
}
402+
403+
private static void FormatMinorItems(IList<MinorIniItem> minorItems, bool removeSuccessiveBlankLines)
404+
{
405+
foreach (MinorIniItem minorItem in minorItems)
413406
{
414-
if (trailingItem is BlankLine blankLine)
407+
if (minorItem is BlankLine blankLine)
415408
blankLine.Padding.Reset();
416-
else if (trailingItem is Comment comment)
409+
else if (minorItem is Comment comment)
417410
comment.Padding.Reset();
418411
}
412+
413+
for (int i = minorItems.Count - 1; i > 0; i--)
414+
{
415+
if (minorItems[i] is BlankLine && minorItems[i - 1] is BlankLine)
416+
minorItems.RemoveAt(i);
417+
}
419418
}
420419
}
421420

src/IniFile/IniFile.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/IniFile/IniFormatOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public sealed class IniFormatOptions
3535
/// </summary>
3636
public bool EnsureBlankLineBetweenProperties { get; set; }
3737

38+
/// <summary>
39+
/// Removes successive blank lines
40+
/// </summary>
41+
public bool RemoveSuccessiveBlankLines { get; set; }
42+
3843
/// <summary>
3944
/// Default formatting options for INI content.
4045
/// </summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
; This is a comment
4+
; This is another comment
5+
6+
[Section One]
7+
8+
Key1 = Value1
9+
Key2 = Value Two
10+
11+
Key3 = Value Three
12+
13+
14+
[Section Two]
15+
16+
Key4 = Value Four
17+
18+
; Trailing comment
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
; This is a comment
4+
;This is another comment
5+
6+
[ Section One ]
7+
8+
Key1 = Value1
9+
Key2= Value Two
10+
11+
Key3 = Value Three
12+
13+
14+
[ Section Two]
15+
16+
Key4 = Value Four
17+
18+
19+
; Trailing comment
20+
21+
22+

tests/IniFile.Tests/IniFile.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
<ItemGroup>
2020
<None Remove="Data\EmptyProperties.ini" />
2121
<None Remove="Data\EmptySections.ini" />
22+
<None Remove="Data\Formatted.ini" />
2223
<None Remove="Data\PropertyWithoutSection.ini" />
24+
<None Remove="Data\Unformatted.ini" />
2325
<None Remove="Data\UnrecognizedLine.ini" />
2426
<None Remove="Players.ini" />
2527
<None Remove="Sample.json" />
@@ -28,7 +30,9 @@
2830
<ItemGroup>
2931
<EmbeddedResource Include="Data\EmptyProperties.ini" />
3032
<EmbeddedResource Include="Data\EmptySections.ini" />
33+
<EmbeddedResource Include="Data\Formatted.ini" />
3134
<EmbeddedResource Include="Data\PropertyWithoutSection.ini" />
35+
<EmbeddedResource Include="Data\Unformatted.ini" />
3236
<EmbeddedResource Include="Data\UnrecognizedLine.ini" />
3337
<EmbeddedResource Include="Players.ini" />
3438
</ItemGroup>

tests/IniFile.Tests/IniFileTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,17 @@ public void Empty_properties_are_allowed(string iniContent)
9090
ini["Section1"]["Key2"].ShouldBe(string.Empty);
9191
ini["Section2"]["Key4"].ShouldBe(string.Empty);
9292
}
93+
94+
[Theory]
95+
[EmbeddedResourceContent("IniFile.Tests.Data.Unformatted.ini")]
96+
public void Format_ini(string iniContent)
97+
{
98+
Ini ini = Ini.Load(iniContent);
99+
100+
ini.Format(new IniFormatOptions { RemoveSuccessiveBlankLines = true });
101+
string formatted = ini.ToString();
102+
103+
formatted.ShouldNotBeNull();
104+
}
93105
}
94106
}

0 commit comments

Comments
 (0)