Skip to content

Commit b441774

Browse files
committed
Use primary constructors
1 parent 313657f commit b441774

19 files changed

+116
-212
lines changed

src/DocoptNet/CodeGeneration/CSharpSourceBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace DocoptNet.CodeGeneration
88
using System.Diagnostics;
99
using System.Text;
1010

11-
sealed class CSharpSourceBuilder :
11+
sealed class CSharpSourceBuilder(StringBuilder sb) :
1212
CSharpSourceBuilder.IStatementFlow,
1313
CSharpSourceBuilder.IBlockFlow,
1414
CSharpSourceBuilder.IControlBlockFlow,
@@ -18,9 +18,8 @@ sealed class CSharpSourceBuilder :
1818
bool _skipNextNewLine;
1919

2020
public CSharpSourceBuilder() : this(new()) { }
21-
public CSharpSourceBuilder(StringBuilder sb) => StringBuilder = sb;
2221

23-
public StringBuilder StringBuilder { get; }
22+
public StringBuilder StringBuilder { get; } = sb;
2423

2524
public int Level { get; private set; }
2625
public bool IsNewLine { get; private set; } = true;

src/DocoptNet/CodeGeneration/SourceGenerator.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,10 @@ sealed class SyntaxReceiver : ISyntaxContextReceiver
4545

4646
public List<(ClassDeclarationSyntax Class, AttributeData Attributes)> ClassAttributes { get; } = [];
4747

48-
sealed class ModelSymbols
48+
sealed class ModelSymbols(SemanticModel model, INamedTypeSymbol attributeType)
4949
{
50-
public ModelSymbols(SemanticModel model, INamedTypeSymbol attributeType)
51-
{
52-
Model = model;
53-
AttributeType = attributeType;
54-
}
55-
56-
public SemanticModel Model { get; }
57-
public INamedTypeSymbol AttributeType { get; }
50+
public SemanticModel Model { get; } = model;
51+
public INamedTypeSymbol AttributeType { get; } = attributeType;
5852
}
5953

6054
ModelSymbols GetModelSymbols(SemanticModel semanticModel)

src/DocoptNet/CodeGeneration/StringBuilderSourceText.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,15 @@ namespace DocoptNet.CodeGeneration
66
using System.Text;
77
using Microsoft.CodeAnalysis.Text;
88

9-
sealed class StringBuilderSourceText : SourceText
9+
sealed class StringBuilderSourceText(StringBuilder builder, Encoding? encoding = null) : SourceText
1010
{
11-
readonly StringBuilder _builder;
11+
public override Encoding? Encoding { get; } = encoding;
12+
public override int Length => builder.Length;
13+
public override char this[int position] => builder[position];
1214

13-
public StringBuilderSourceText(StringBuilder builder, Encoding? encoding = null) =>
14-
(_builder, Encoding) = (builder, encoding);
15-
16-
public override Encoding? Encoding { get; }
17-
public override int Length => _builder.Length;
18-
public override char this[int position] => _builder[position];
19-
20-
public override string ToString(TextSpan span) => _builder.ToString(span.Start, span.Length);
15+
public override string ToString(TextSpan span) => builder.ToString(span.Start, span.Length);
2116

2217
public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) =>
23-
_builder.CopyTo(sourceIndex, destination, destinationIndex, count);
18+
builder.CopyTo(sourceIndex, destination, destinationIndex, count);
2419
}
2520
}

src/DocoptNet/Docopt.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,9 @@ internal static string[] ParseSection(string name, string source)
533533
}
534534
}
535535

536-
partial class PrintExitEventArgs : EventArgs
536+
partial class PrintExitEventArgs(string msg, int errorCode) : EventArgs
537537
{
538-
public PrintExitEventArgs(string msg, int errorCode)
539-
{
540-
Message = msg;
541-
ErrorCode = errorCode;
542-
}
543-
544-
public string Message { get; set; }
545-
public int ErrorCode { get; set; }
538+
public string Message { get; set; } = msg;
539+
public int ErrorCode { get; set; } = errorCode;
546540
}
547541
}

src/DocoptNet/Internals/BranchPatterns.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace DocoptNet.Internals
1313
/// </summary>
1414
abstract partial class BranchPattern : Pattern
1515
{
16+
#pragma warning disable IDE0290 // Use primary constructor (protected)
1617
protected BranchPattern(params Pattern[] children)
18+
#pragma warning restore IDE0290 // Use primary constructor
1719
{
1820
if (children == null) throw new ArgumentNullException(nameof(children));
1921
Children = children;
@@ -106,31 +108,16 @@ public Pattern FixRepeatingArguments()
106108
}
107109
}
108110

109-
class Required : BranchPattern
110-
{
111-
public Required(params Pattern[] patterns) : base(patterns) { }
112-
}
111+
class Required(params Pattern[] patterns) : BranchPattern(patterns);
113112

114-
class Optional : BranchPattern
115-
{
116-
public Optional(params Pattern[] patterns) : base(patterns) { }
117-
}
113+
class Optional(params Pattern[] patterns) : BranchPattern(patterns);
118114

119115
// Marker/placeholder for [options] shortcut.
120-
class OptionsShortcut : Optional
121-
{
122-
// TODO consider single pattern
123-
public OptionsShortcut(params Pattern[] patterns) : base(patterns) { }
124-
}
116+
// TODO consider single pattern
117+
class OptionsShortcut(params Pattern[] patterns) : Optional(patterns);
125118

126-
partial class Either : BranchPattern
127-
{
128-
public Either(params Pattern[] patterns) : base(patterns) { }
129-
}
119+
partial class Either(params Pattern[] patterns) : BranchPattern(patterns);
130120

131-
class OneOrMore : BranchPattern
132-
{
133-
// TODO consider single pattern
134-
public OneOrMore(params Pattern[] patterns) : base(patterns) { }
135-
}
121+
// TODO consider single pattern
122+
class OneOrMore(params Pattern[] patterns) : BranchPattern(patterns);
136123
}

src/DocoptNet/Internals/Command.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
namespace DocoptNet.Internals
55
{
6-
sealed class Command : LeafPattern
7-
{
8-
public Command(string name, bool value = false) :
9-
base(name, value ? ArgValue.True : ArgValue.False) { }
10-
}
6+
sealed class Command(string name, bool value = false)
7+
: LeafPattern(name, value ? ArgValue.True : ArgValue.False);
118
}

src/DocoptNet/Internals/LeafPattern.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ namespace DocoptNet.Internals
1212
/// </summary>
1313
abstract partial class LeafPattern : Pattern
1414
{
15+
#pragma warning disable IDE0290 // Use primary constructor (protected)
1516
protected LeafPattern(string name, ArgValue value = default)
17+
#pragma warning restore IDE0290 // Use primary constructor
1618
{
1719
Name = name;
1820
Value = value;

src/DocoptNet/Internals/MatchResult.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33

44
namespace DocoptNet.Internals
55
{
6-
readonly partial struct MatchResult
6+
readonly partial struct MatchResult(bool matched,
7+
ReadOnlyList<LeafPattern> left,
8+
ReadOnlyList<LeafPattern> collected)
79
{
8-
public readonly bool Matched;
9-
public readonly ReadOnlyList<LeafPattern> Left;
10-
public readonly ReadOnlyList<LeafPattern> Collected;
11-
12-
public MatchResult(bool matched, ReadOnlyList<LeafPattern> left, ReadOnlyList<LeafPattern> collected)
13-
{
14-
Matched = matched;
15-
Left = left;
16-
Collected = collected;
17-
}
10+
public readonly bool Matched = matched;
11+
public readonly ReadOnlyList<LeafPattern> Left = left;
12+
public readonly ReadOnlyList<LeafPattern> Collected = collected;
1813

1914
public override bool Equals(object? obj)
2015
{

src/DocoptNet/Internals/PatternMatcher.cs

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,18 @@ public bool Fold(MatchResult match)
9393
public MatchResult Result => _result ?? new MatchResult(true, Left, Collected);
9494
}
9595

96-
partial struct EitherMatcher : IBranchPatternMatcher
96+
partial struct EitherMatcher(int count, Leaves left, Leaves collected) :
97+
IBranchPatternMatcher
9798
{
98-
readonly int _count;
9999
int _i;
100-
MatchResult _match;
101-
102-
public EitherMatcher(int count, Leaves left, Leaves collected) : this()
103-
{
104-
_count = count;
105-
(Left, Collected) = (left, collected);
106-
_match = new MatchResult(false, left, collected);
107-
}
100+
MatchResult _match = new(false, left, collected);
108101

109102
public int Index => _i - 1;
110103

111-
public bool Next() => BranchPatternMatcher.Next(ref _i, _count);
104+
public bool Next() => BranchPatternMatcher.Next(ref _i, count);
112105

113-
public Leaves Left { get; }
114-
public Leaves Collected { get; }
106+
public Leaves Left { get; } = left;
107+
public Leaves Collected { get; } = collected;
115108

116109
public bool Match(LeafPatternMatcher matcher, string name, ArgValueKind kind) =>
117110
Fold(matcher.Match(Left, Collected, name, kind));
@@ -131,20 +124,17 @@ public bool Fold(MatchResult match)
131124
public MatchResult Result => _match;
132125
}
133126

134-
partial struct OptionalMatcher : IBranchPatternMatcher
127+
partial struct OptionalMatcher(int count, Leaves left, Leaves collected) :
128+
IBranchPatternMatcher
135129
{
136-
readonly int _count;
137130
int _i;
138131

139-
public OptionalMatcher(int count, Leaves left, Leaves collected) : this() =>
140-
(_count, Left, Collected) = (count, left, collected);
141-
142132
public int Index => _i - 1;
143133

144-
public bool Next() => BranchPatternMatcher.Next(ref _i, _count);
134+
public bool Next() => BranchPatternMatcher.Next(ref _i, count);
145135

146-
public Leaves Left { get; private set; }
147-
public Leaves Collected { get; private set; }
136+
public Leaves Left { get; private set; } = left;
137+
public Leaves Collected { get; private set; } = collected;
148138

149139
public bool Match(LeafPatternMatcher matcher, string name, ArgValueKind kind) =>
150140
Fold(matcher.Match(Left, Collected, name, kind));
@@ -163,23 +153,23 @@ public bool Fold(MatchResult match)
163153
public MatchResult Result => new(true, Left, Collected);
164154
}
165155

166-
partial struct OneOrMoreMatcher : IBranchPatternMatcher
156+
partial struct OneOrMoreMatcher(
157+
#pragma warning disable CS9113 // Parameter is unread.
158+
int count,
159+
#pragma warning restore CS9113 // Parameter is unread.
160+
Leaves left,
161+
Leaves collected) :
162+
IBranchPatternMatcher
167163
{
168-
readonly Leaves _initLeft, _initCollected;
164+
readonly Leaves _initLeft = left, _initCollected = collected;
169165
int _times;
170166
Leaves? _lastLeft;
171167

172-
public OneOrMoreMatcher(int count, Leaves left, Leaves collected) : this()
173-
{
174-
Left = _initLeft = left;
175-
Collected = _initCollected = collected;
176-
}
177-
178168
public int Index => 0;
179169
public bool Next() => true;
180170

181-
public Leaves Left { get; private set; }
182-
public Leaves Collected { get; private set; }
171+
public Leaves Left { get; private set; } = left;
172+
public Leaves Collected { get; private set; } = collected;
183173

184174
public bool Match(LeafPatternMatcher matcher, string name, ArgValueKind kind) =>
185175
Fold(matcher.Match(Left, Collected, name, kind));

src/DocoptNet/Internals/ReadOnlyList.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ static class ReadOnlyList
2222
/// read-write <seealso cref="IList{T}"/> is not changed.
2323
/// </remarks>
2424

25-
readonly partial struct ReadOnlyList<T> : IReadOnlyList<T>
25+
readonly partial struct ReadOnlyList<T>(IList<T> list) : IReadOnlyList<T>
2626
{
27-
public ReadOnlyList(IList<T> list) => List = list;
28-
IList<T> List => field ?? Array.Empty<T>();
27+
IList<T> List { get => field ?? Array.Empty<T>(); } = list;
28+
2929
public int Count => List.Count;
3030
public T this[int index] => List[index];
3131
public IEnumerator<T> GetEnumerator() => List.GetEnumerator();

0 commit comments

Comments
 (0)