Browse Source

Use a UI to control whether to use the `struct field initializer` syntax sugar.

pull/3598/head
sonyps5201314 2 months ago
parent
commit
84a07fb395
  1. 2
      ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs
  2. 5
      ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs
  3. 21
      ICSharpCode.Decompiler/DecompilerSettings.cs
  4. 9
      ILSpy/Properties/Resources.Designer.cs
  5. 3
      ILSpy/Properties/Resources.resx
  6. 3
      ILSpy/Properties/Resources.zh-Hans.resx

2
ICSharpCode.Decompiler.Tests/RoundtripAssembly.cs

@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.Roundtrip @@ -70,7 +70,7 @@ namespace ICSharpCode.Decompiler.Roundtrip
[Test]
public async Task NRefactory_CSharp()
{
await RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll", LanguageVersion.CSharp10_0);
await RunWithTest("NRefactory", "ICSharpCode.NRefactory.CSharp.dll", "ICSharpCode.NRefactory.Tests.dll");
}
[Test]

5
ICSharpCode.Decompiler/CSharp/Transforms/TransformFieldAndConstructorInitializers.cs

@ -205,6 +205,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -205,6 +205,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
if (ctorMethodDef != null && declaringTypeDefinition?.IsReferenceType == false && !declaringTypeDefinition.IsRecord && !isStruct)
return;
if (isStruct && !context.Settings.StructFieldInitializers)
{
return;
}
bool ctorIsUnsafe = instanceCtorsNotChainingWithThis.All(c => c.HasModifier(Modifiers.Unsafe));
if (!context.DecompileRun.RecordDecompilers.TryGetValue(declaringTypeDefinition, out var record))

21
ICSharpCode.Decompiler/DecompilerSettings.cs

@ -150,6 +150,7 @@ namespace ICSharpCode.Decompiler @@ -150,6 +150,7 @@ namespace ICSharpCode.Decompiler
if (languageVersion < CSharp.LanguageVersion.CSharp10_0)
{
fileScopedNamespaces = false;
structFieldInitializers = false;
recordStructs = false;
}
if (languageVersion < CSharp.LanguageVersion.CSharp11_0)
@ -187,7 +188,7 @@ namespace ICSharpCode.Decompiler @@ -187,7 +188,7 @@ namespace ICSharpCode.Decompiler
return CSharp.LanguageVersion.CSharp12_0;
if (scopedRef || requiredMembers || numericIntPtr || utf8StringLiterals || unsignedRightShift || checkedOperators)
return CSharp.LanguageVersion.CSharp11_0;
if (fileScopedNamespaces || recordStructs)
if (fileScopedNamespaces || structFieldInitializers || recordStructs)
return CSharp.LanguageVersion.CSharp10_0;
if (nativeIntegers || initAccessors || functionPointers || forEachWithGetEnumeratorExtension
|| recordClasses || withExpressions || usePrimaryConstructorSyntax || covariantReturns
@ -464,6 +465,24 @@ namespace ICSharpCode.Decompiler @@ -464,6 +465,24 @@ namespace ICSharpCode.Decompiler
}
}
bool structFieldInitializers = true;
/// <summary>
/// Gets/Sets whether C# 10 struct field initializers should be used.
/// </summary>
[Category("C# 10.0 / VS 2022")]
[Description("DecompilerSettings.UseStructFieldInitializerSyntax")]
public bool StructFieldInitializers {
get { return structFieldInitializers; }
set {
if (structFieldInitializers != value)
{
structFieldInitializers = value;
OnPropertyChanged();
}
}
}
bool anonymousMethods = true;
/// <summary>

9
ILSpy/Properties/Resources.Designer.cs generated

@ -1632,6 +1632,15 @@ namespace ICSharpCode.ILSpy.Properties { @@ -1632,6 +1632,15 @@ namespace ICSharpCode.ILSpy.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to Use struct field initializer syntax.
/// </summary>
public static string DecompilerSettings_UseStructFieldInitializerSyntax {
get {
return ResourceManager.GetString("DecompilerSettings.UseStructFieldInitializerSyntax", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Use throw expressions.
/// </summary>

3
ILSpy/Properties/Resources.resx

@ -564,6 +564,9 @@ Are you sure you want to continue?</value> @@ -564,6 +564,9 @@ Are you sure you want to continue?</value>
<data name="DecompilerSettings.UseStringInterpolation" xml:space="preserve">
<value>Use string interpolation</value>
</data>
<data name="DecompilerSettings.UseStructFieldInitializerSyntax" xml:space="preserve">
<value>Use struct field initializer syntax</value>
</data>
<data name="DecompilerSettings.UseThrowExpressions" xml:space="preserve">
<value>Use throw expressions</value>
</data>

3
ILSpy/Properties/Resources.zh-Hans.resx

@ -522,6 +522,9 @@ @@ -522,6 +522,9 @@
<data name="DecompilerSettings.UseStringInterpolation" xml:space="preserve">
<value>使用字符串内插</value>
</data>
<data name="DecompilerSettings.UseStructFieldInitializerSyntax" xml:space="preserve">
<value>使用结构字段初始化器语法</value>
</data>
<data name="DecompilerSettings.UseThrowExpressions" xml:space="preserve">
<value>使用 throw 表达式</value>
</data>

Loading…
Cancel
Save