Skip to content

Commit

Permalink
Use C# 11 collection expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Feb 5, 2024
1 parent 3d5d798 commit 21a3c75
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 51 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ csharp_style_prefer_top_level_statements = true:silent
csharp_style_throw_expression = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
dotnet_diagnostic.CC0022.severity = silent
csharp_style_prefer_primary_constructors = false:suggestion

# Code files
[*.{cs,vb}]
Expand Down
4 changes: 2 additions & 2 deletions src/Menees.Chords/ChordDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ private ChordDefinition(Chord chord, IReadOnlyList<byte?> definition)
{
if (byte.TryParse(part, out byte fret))
{
frets ??= new();
frets ??= [];
frets.Add(fret);
}
else if (ChordParser.Comparer.Equals(part, "x") || part == "_")
{
frets ??= new();
frets ??= [];
frets.Add(null);
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/Menees.Chords/ChordProLyricLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ChordProLyricLine Convert(ChordLyricPair pair)
{
Conditions.RequireNonNull(pair);

List<TextSegment> segments = new();
List<TextSegment> segments = [];
string lyricText = pair.Lyrics.Text;
int lyricIndex = 0;

Expand Down Expand Up @@ -183,7 +183,7 @@ void AddLyrics(int length)
// D/F# A
// Baby, it's [D/F#]all right [A]now => Baby, it's all right now
int indentChord = 0;
List<TextSegment> chordLineSegments = new();
List<TextSegment> chordLineSegments = [];
StringBuilder lyricLineText = new();
foreach (TextSegment segment in this.Segments)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Menees.Chords/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected Entry(IEnumerable<Entry>? annotations = null)
/// This is useful for entries like a <see cref="HeaderLine"/> that also contains a <see cref="Comment"/>,
/// and for when <see cref="ChordDefinitions"/> are at the end of a <see cref="ChordLine"/>.
/// </remarks>
public IReadOnlyList<Entry> Annotations => this.annotations ?? (IReadOnlyList<Entry>)Array.Empty<Entry>();
public IReadOnlyList<Entry> Annotations => this.annotations ?? (IReadOnlyList<Entry>)[];

#endregion

Expand Down Expand Up @@ -175,7 +175,7 @@ protected Entry Clone()
protected void AddAnnotation(Entry annotation)
{
Conditions.RequireNonNull(annotation);
this.annotations ??= new();
this.annotations ??= [];
this.annotations.Add(annotation);
}

Expand All @@ -186,7 +186,7 @@ protected void AddAnnotation(Entry annotation)
protected void AddAnnotations(IEnumerable<Entry> annotations)
{
Conditions.RequireNonNull(annotations);
this.annotations ??= new();
this.annotations ??= [];
this.annotations.AddRange(annotations);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Menees.Chords/Parsers/ChordParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public sealed class ChordParser
'*', '~', '←', '↑', '↓', '→',
};

private readonly List<string> errors = new();
private readonly List<string> errors = [];
private Notation notation;
private int index;

Expand Down Expand Up @@ -235,7 +235,7 @@ private void Parse()
{
if (this.ParseRoot(out string? root))
{
List<string> modifiers = new();
List<string> modifiers = [];
while (this.TryParseModifier(modifiers))
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Chords/Parsers/Cleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private static void RemoveLeadingAndTrailingBlankLines(List<string> lines)

private string Clean()
{
List<string> lines = new();
List<string> lines = [];
using (StringReader reader = new(this.OriginalText ?? string.Empty))
{
string? line;
Expand Down
17 changes: 8 additions & 9 deletions src/Menees.Chords/Parsers/DocumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ public sealed class DocumentParser
/// ChordPro format.
/// </remarks>
public static Func<LineContext, Entry?>[] DefaultLineParsers { get; } =
new Func<LineContext, Entry?>[]
{
// Add line parsers in order from most specific syntax to least specific syntax.
[
/* Add line parsers in order from most specific syntax to least specific syntax. */
UriLine.TryParse,
HeaderLine.TryParse,
ChordProRemarkLine.TryParse, // This will parse #-prefixed lines before Comment.TryParse gets them.
Expand All @@ -106,7 +105,7 @@ public sealed class DocumentParser
MetadataEntry.TryParse,
TitleLine.TryParse,
LyricLine.Parse,
};
];

/// <summary>
/// Gets the default collection of <see cref="Entry"/> groupers.
Expand All @@ -118,13 +117,13 @@ public sealed class DocumentParser
/// convert the lines into <see cref="Entry"/>s.
/// </remarks>
public static Func<GroupContext, IReadOnlyList<Entry>>[] DefaultGroupers { get; }
= new Func<GroupContext, IReadOnlyList<Entry>>[]
{
=
[
Parsers.GroupEntries.ByChordLinePair,
Parsers.GroupEntries.ByChordProEnvironment,
Parsers.GroupEntries.ByHeaderLine,
Parsers.GroupEntries.ByBlankLine,
};
];

/// <summary>
/// Gets an empty collection of groupers to use when processing input where grouping isn't desired.
Expand Down Expand Up @@ -220,9 +219,9 @@ internal IReadOnlyList<Entry> GroupEntries(IReadOnlyList<Entry> entries)

#region Private Methods

private IReadOnlyList<Entry> ParseLines(TextReader reader)
private List<Entry> ParseLines(TextReader reader)
{
List<Entry> result = new();
List<Entry> result = [];
LineContext context = new(this);

string? rawLineText;
Expand Down
8 changes: 4 additions & 4 deletions src/Menees.Chords/Parsers/GroupEntries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static IReadOnlyList<Entry> ByChordProEnvironment(GroupContext context)
const StringComparison Comparison = ChordParser.Comparison;
if (directive.LongName.StartsWith(StartOfPrefix, Comparison))
{
List<Entry> section = new() { directive };
List<Entry> section = [directive];
sectionStack.Push(section);
}
else if (directive.LongName.StartsWith(EndOfPrefix, Comparison)
Expand Down Expand Up @@ -137,7 +137,7 @@ public static IReadOnlyList<Entry> ByHeaderLine(GroupContext context)
if (entry is HeaderLine header)
{
FinishSection();
section = new() { header };
section = [header];
}
else
{
Expand All @@ -153,7 +153,7 @@ void FinishSection()
{
// Make sure trailing blank lines aren't included in the section.
// They should come after it and separate it from the next section (if any).
List<BlankLine> blankLines = new();
List<BlankLine> blankLines = [];
while (section.Count > 0 && section[^1] is BlankLine)
{
blankLines.Add(BlankLine.Instance);
Expand Down Expand Up @@ -210,7 +210,7 @@ public static IReadOnlyList<Entry> ByBlankLine(GroupContext context)
}
else
{
section ??= new();
section ??= [];
AddEntry(entry);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Menees.Chords/Parsers/LineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private static Comment CreateComment(string text, string start, string end)

private IReadOnlyList<Entry> SplitAnnotations(out int annotationStartIndex)
{
List<Entry> result = new();
List<Entry> result = [];
annotationStartIndex = this.LineText.Length;

bool tryComment = this.Parser.TryParseComment;
Expand Down Expand Up @@ -181,7 +181,7 @@ private IReadOnlyList<Entry> SplitAnnotations(out int annotationStartIndex)
ChordDefinition? chordDefinition;
if (chord.Success && (chordDefinition = ChordDefinition.TryParse(chord.Value, group.Value)) != null)
{
List<ChordDefinition> definitions = new() { chordDefinition };
List<ChordDefinition> definitions = [chordDefinition];
ChordDefinitions? previousDefinitionEntry = result.Count > 0 ? result[^1] as ChordDefinitions : null;
if (previousDefinitionEntry != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Chords/SegmentedEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected SegmentedEntry(IReadOnlyList<TextSegment> segments)
{
Conditions.RequireNonNull(context);

List<TextSegment> result = new();
List<TextSegment> result = [];
TokenType chordTokenType = requiredBracketedChords ? TokenType.Bracketed : TokenType.Text;

Lexer lexer = context.CreateLexer(out annotations);
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Chords/TitleLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private TitleLine(string text, IReadOnlyList<MetadataEntry> metadata)
Group titleGroup = match.Groups["title"];
if (titleGroup.Success)
{
List<MetadataEntry> metadata = new() { new("title", titleGroup.Value), };
List<MetadataEntry> metadata = [new("title", titleGroup.Value),];

Group artistGroup = match.Groups["artist"];
if (artistGroup.Success)
Expand Down
4 changes: 2 additions & 2 deletions src/Menees.Chords/Transformers/ChordOverLyricTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static Entry ParseChordDirective(string text, IReadOnlyList<Entry> annot
{
string initialSectionName = string.Empty;
string name = parts[0];
List<string> current = new();
List<string> current = [];
Dictionary<string, List<string>> sections = new(ChordParser.Comparer) { { initialSectionName, current } };
foreach (string part in parts.Skip(1))
{
Expand All @@ -197,7 +197,7 @@ private static Entry ParseChordDirective(string text, IReadOnlyList<Entry> annot
}
else
{
current = new();
current = [];
sections.Add(part, current);
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Menees.Chords.Tests/ChordDefinitionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public void TryParseValidTest()
Test1("C# = x46664", "C# x46664");
Test1("Em 022000 (Haunting)", "Em 022000", "(Haunting)");

TestN(" C x32010, D/F# = 200232 ** Soft **", new[] { "C x32010", "D/F# 200232" }, new[] { "** Soft **" });
TestN(" C x32010; D/F# = 200232; ** Soft **", new[] { "C x32010", "D/F# 200232" }, new[] { "** Soft **" });
TestN(" A/C#: x4222x; D/F#: 2x0232 (use thumb for bass note)", new[] { "A/C# x4222x", "D/F# 2x0232" }, new[] { "(use thumb for bass note)" });
TestN(" C x32010, D/F# = 200232 ** Soft **", ["C x32010", "D/F# 200232"], ["** Soft **"]);
TestN(" C x32010; D/F# = 200232; ** Soft **", ["C x32010", "D/F# 200232"], ["** Soft **"]);
TestN(" A/C#: x4222x; D/F#: 2x0232 (use thumb for bass note)", ["A/C# x4222x", "D/F# 2x0232"], ["(use thumb for bass note)"]);

static void Test1(string text, string? expectedDefinition, string? expectedComment = null)
=> TestN(text, new[] { expectedDefinition ?? text }, expectedComment is null ? null : new[] { expectedComment });
=> TestN(text, [expectedDefinition ?? text], expectedComment is null ? null : [expectedComment]);

static void TestN(string text, string[] expectedDefinitions, string[]? expectedComments = null)
{
LineContext context = LineContextTests.Create(text);
ChordDefinitions definitions = ChordDefinitions.TryParse(context).ShouldNotBeNull();
definitions.Definitions.Select(def => def.ToString()).ShouldBe(expectedDefinitions);
definitions.Annotations.Select(anno => anno.ToString()).ShouldBe(expectedComments ?? Array.Empty<string>());
definitions.Annotations.Select(anno => anno.ToString()).ShouldBe(expectedComments ?? []);
}
}

Expand Down
22 changes: 11 additions & 11 deletions tests/Menees.Chords.Tests/Parsers/ChordParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,21 @@ public void ValidTest()
{
Test("A");
Test("D/F#", root: "D", bass: "F#");
Test("Am", root: "A", modifiers: new[] { "m" });
Test("Asus2", root: "A", modifiers: new[] { "sus", "2" });
Test("Dadd9add11", root: "D", modifiers: new[] { "add", "9", "add", "11" }); // x54030
Test("[C#7b5/D]", start: 1, length: 7, root: "C#", modifiers: new[] { "7", "b", "5" }, bass: "D");
Test("Am", root: "A", modifiers: ["m"]);
Test("Asus2", root: "A", modifiers: ["sus", "2"]);
Test("Dadd9add11", root: "D", modifiers: ["add", "9", "add", "11"]); // x54030
Test("[C#7b5/D]", start: 1, length: 7, root: "C#", modifiers: ["7", "b", "5"], bass: "D");
Test("C/Ab", root: "C", bass: "Ab");
Test("Caugmaj13", root: "C", modifiers: new[] { "aug", "maj", "13" });
Test("C#min7dim5", root: "C#", modifiers: new[] { "min", "7", "dim", "5" });
Test(" Ebm7 ", name: "Ebm7", root: "Eb", modifiers: new[] { "m", "7" });
Test("CM7", root: "C", modifiers: new[] { "M", "7" });
Test("Caugmaj13", root: "C", modifiers: ["aug", "maj", "13"]);
Test("C#min7dim5", root: "C#", modifiers: ["min", "7", "dim", "5"]);
Test(" Ebm7 ", name: "Ebm7", root: "Eb", modifiers: ["m", "7"]);
Test("CM7", root: "C", modifiers: ["M", "7"]);

Test("1/3", root: "1", bass: "3", notation: Notation.Nashville);
Test("3#7b9", root: "3", modifiers: new[] { "#", "7", "b", "9" }, notation: Notation.Nashville);
Test("3#7b9", root: "3", modifiers: ["#", "7", "b", "9"], notation: Notation.Nashville);

Test("I/IV", root: "I", bass: "IV", notation: Notation.Roman);
Test("viiadd3sus4", root: "vii", modifiers: new[] { "add", "3", "sus", "4" }, notation: Notation.Roman);
Test("viiadd3sus4", root: "vii", modifiers: ["add", "3", "sus", "4"], notation: Notation.Roman);

static void Test(
string text,
Expand All @@ -76,7 +76,7 @@ public void ValidTest()
Chord chord = parser.Chord.ShouldNotBeNull();
chord.Name.ShouldBe(name);
chord.Root.ShouldBe(root);
chord.Modifiers.ShouldBe(modifiers ?? Array.Empty<string>());
chord.Modifiers.ShouldBe(modifiers ?? []);
chord.Bass.ShouldBe(bass);
chord.Notation.ShouldBe(notation);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Menees.Chords.Tests/Parsers/LexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ReadTest()

static void Test(string text, params Token[] expected)
{
List<Token> actual = new();
List<Token> actual = [];
Lexer lexer = new(text);
lexer.Token.ShouldBe(default);

Expand Down
10 changes: 5 additions & 5 deletions tests/Menees.Chords.Tests/Parsers/LineContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public void CreateLexerTest()
[TestMethod]
public void CreateLexerWithAnnotationsTest()
{
Test("This (a) has (comment)", "This (a) has ", new[] { "(comment)" });
Test("This (also) has one ** here **", "This (also) has one ", new[] { "** here **" });
Test("This has (a) Cmaj7/F=1-3-2-0-0-0", "This has ", new[] { "(a)" }, "Cmaj7/F 132000");
Test("This (a) has (comment)", "This (a) has ", ["(comment)"]);
Test("This (also) has one ** here **", "This (also) has one ", ["** here **"]);
Test("This has (a) Cmaj7/F=1-3-2-0-0-0", "This has ", ["(a)"], "Cmaj7/F 132000");
Test("High chord: Dm x-x-12-10-10-10", "High chord: ", expectedDefinitions: "Dm x-x-12-10-10-10");
Test("I play Em7 several ways: Em7 020000", "I play Em7 several ways: ", expectedDefinitions: "Em7 020000");
Test("I play Em7 several ways: Em7 = 022030", "I play Em7 several ways: ", expectedDefinitions: "Em7 022030");
Test("I play Em7 several ways: Em7 020003, Em7 = 020030", "I play Em7 several ways: ", expectedDefinitions: "Em7 020003, Em7 020030");
Test("A Bm C Db F# 3x", "A Bm C Db F# ", new[] { "3x" });
Test("A Bm C Db F# x12", "A Bm C Db F# ", new[] { "x12" });
Test("A Bm C Db F# 3x", "A Bm C Db F# ", ["3x"]);
Test("A Bm C Db F# x12", "A Bm C Db F# ", ["x12"]);
Test("A Bm C Db F# x123", "A Bm C Db ", expectedDefinitions: "F# x123"); // Chord def NOT repeat 123x!

Test("This doesn't. )", "This doesn't. )");
Expand Down

0 comments on commit 21a3c75

Please sign in to comment.