From 4cf39bde45ffdeddec87aad9edce7f2c0f78b9ee Mon Sep 17 00:00:00 2001 From: Min Huang Date: Wed, 9 Mar 2022 18:22:42 +0800 Subject: Support .NET language features: init only setter (#619) --- mdoc/Consts.cs | 1 + .../Updater/Formatters/CSharpFullMemberFormatter.cs | 9 ++++++++- mdoc/mdoc.Test/FormatterTests.cs | 11 +++++++++++ mdoc/mdoc.Test/SampleClasses/InitOnlySetter.cs | 20 ++++++++++++++++++++ mdoc/mdoc.Test/SampleClasses/IsExternalInit.cs | 7 +++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 mdoc/mdoc.Test/SampleClasses/InitOnlySetter.cs create mode 100644 mdoc/mdoc.Test/SampleClasses/IsExternalInit.cs diff --git a/mdoc/Consts.cs b/mdoc/Consts.cs index 4d2ef7c3..e047f2af 100644 --- a/mdoc/Consts.cs +++ b/mdoc/Consts.cs @@ -50,5 +50,6 @@ namespace Mono.Documentation public const string IsReadOnlyAttribute = "System.Runtime.CompilerServices.IsReadOnlyAttribute"; public const string InAttribute = "System.Runtime.InteropServices.InAttribute"; public const string TupleElementNamesAttribute = "System.Runtime.CompilerServices.TupleElementNamesAttribute"; + public const string IsExternalInit = "System.Runtime.CompilerServices.IsExternalInit"; } } diff --git a/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs b/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs index e885df57..3a36a5e7 100644 --- a/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs +++ b/mdoc/Mono.Documentation/Updater/Formatters/CSharpFullMemberFormatter.cs @@ -725,7 +725,14 @@ namespace Mono.Documentation.Updater.Formatters { if (set_visible != visibility) buf.Append (' ').Append (set_visible); - buf.Append (" set;"); + if (property.SetMethod.ReturnType is RequiredModifierType returnType && returnType.ModifierType.FullName == Consts.IsExternalInit) + { + buf.Append(" init;"); + } + else + { + buf.Append(" set;"); + } } buf.Append (" }"); diff --git a/mdoc/mdoc.Test/FormatterTests.cs b/mdoc/mdoc.Test/FormatterTests.cs index 868def32..fd987dec 100644 --- a/mdoc/mdoc.Test/FormatterTests.cs +++ b/mdoc/mdoc.Test/FormatterTests.cs @@ -453,6 +453,17 @@ namespace mdoc.Test Assert.AreEqual(expectedSignature, methodSignature); } + [TestCase("Property1", "public int Property1 { get; set; }")] + [TestCase("Property2", "public int Property2 { get; init; }")] + [TestCase("Property3", "public int Property3 { get; protected init; }")] + [TestCase("Item", "public int this[int index] { get; init; }")] + public void CSharpInitOnlySetterTest(string propertyName, string expectedSignature) + { + var property = GetProperty(typeof(SampleClasses.InitOnlySetter), p => p.Name == propertyName); + var propertySignature = formatter.GetDeclaration(property); + Assert.AreEqual(expectedSignature, propertySignature); + } + #region Helper Methods string RealTypeName(string name){ switch (name) { diff --git a/mdoc/mdoc.Test/SampleClasses/InitOnlySetter.cs b/mdoc/mdoc.Test/SampleClasses/InitOnlySetter.cs new file mode 100644 index 00000000..4ffc55c4 --- /dev/null +++ b/mdoc/mdoc.Test/SampleClasses/InitOnlySetter.cs @@ -0,0 +1,20 @@ +namespace mdoc.Test.SampleClasses +{ + public class InitOnlySetter + { + public int Property1 { get; set; } + public int Property2 { get; init; } + public int Property3 { get; protected init; } + public int this[int index] + { + get + { + throw null; + } + init + { + throw null; + } + } + } +} diff --git a/mdoc/mdoc.Test/SampleClasses/IsExternalInit.cs b/mdoc/mdoc.Test/SampleClasses/IsExternalInit.cs new file mode 100644 index 00000000..48584312 --- /dev/null +++ b/mdoc/mdoc.Test/SampleClasses/IsExternalInit.cs @@ -0,0 +1,7 @@ +using System.ComponentModel; + +namespace System.Runtime.CompilerServices +{ + [EditorBrowsable(EditorBrowsableState.Never)] + internal class IsExternalInit { } +} \ No newline at end of file -- cgit v1.2.3