From 2ea9aa4cf88a98abcc9d113e90a1e9481e583baa Mon Sep 17 00:00:00 2001 From: Raf Date: Fri, 26 May 2023 15:00:49 +0200 Subject: [PATCH 1/2] Fixes #4 --- src/SpeedyGenerators/PropertyChangedGenerator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SpeedyGenerators/PropertyChangedGenerator.cs b/src/SpeedyGenerators/PropertyChangedGenerator.cs index d5a276d..016d063 100644 --- a/src/SpeedyGenerators/PropertyChangedGenerator.cs +++ b/src/SpeedyGenerators/PropertyChangedGenerator.cs @@ -155,6 +155,7 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode) .FirstOrDefault() ?.Name ?.ToString(); + .OfType() namespaceName ??= String.Empty; var className = editedClass.Identifier.ToString(); var fullName = $"{namespaceName}.{className}"; From 93134fc12c45094c1749701e35cec02bb64cf7c1 Mon Sep 17 00:00:00 2001 From: Raf Date: Fri, 26 May 2023 15:01:08 +0200 Subject: [PATCH 2/2] Interactive test --- src/ConsoleTest/ConsoleTest.csproj | 1 + src/ConsoleTest/FileScopedClass.cs | 15 ++++++++++ src/LibraryTest/FileScopedClass.cs | 29 +++++++++++++++++++ src/LibraryTest/ShortDemo.cs | 4 +-- .../PropertyChangedGenerator.cs | 6 ++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/ConsoleTest/FileScopedClass.cs create mode 100644 src/LibraryTest/FileScopedClass.cs diff --git a/src/ConsoleTest/ConsoleTest.csproj b/src/ConsoleTest/ConsoleTest.csproj index 083aad4..f5d58dd 100644 --- a/src/ConsoleTest/ConsoleTest.csproj +++ b/src/ConsoleTest/ConsoleTest.csproj @@ -4,6 +4,7 @@ Exe net7.0 + 11 enable diff --git a/src/ConsoleTest/FileScopedClass.cs b/src/ConsoleTest/FileScopedClass.cs new file mode 100644 index 0000000..51fa3f7 --- /dev/null +++ b/src/ConsoleTest/FileScopedClass.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using SpeedyGenerators; + +namespace ConsoleTest; + +internal partial class FileScopedClass +{ + [MakeProperty("MyProperty")] + private int _myField; +} diff --git a/src/LibraryTest/FileScopedClass.cs b/src/LibraryTest/FileScopedClass.cs new file mode 100644 index 0000000..0a291c1 --- /dev/null +++ b/src/LibraryTest/FileScopedClass.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using SpeedyGenerators; + +namespace LibraryTest; + +internal partial class ClassOutsideNamespace +{ + [MakeProperty("MyProperty")] + private int _myField1; +} + +file partial class FileScopedClass +{ + // Cannot use source generators with file scoped classes because + // - source generators must create a separate source file + // - file scoped classes must reside in the same file + //[MakeProperty("YourProperty")] + //protected int _yourField1; +} + +// file scoped partial classes can be defined only in the same file +file partial class FileSCopedClass +{ +} \ No newline at end of file diff --git a/src/LibraryTest/ShortDemo.cs b/src/LibraryTest/ShortDemo.cs index 42cedd6..143a42e 100644 --- a/src/LibraryTest/ShortDemo.cs +++ b/src/LibraryTest/ShortDemo.cs @@ -10,7 +10,7 @@ namespace LibraryTest { internal partial class ShortDemo { - [MakeProperty("Name")] - private string? _name; + //[MakeProperty("Name")] + //private string? _name; } } diff --git a/src/SpeedyGenerators/PropertyChangedGenerator.cs b/src/SpeedyGenerators/PropertyChangedGenerator.cs index 016d063..3f52041 100644 --- a/src/SpeedyGenerators/PropertyChangedGenerator.cs +++ b/src/SpeedyGenerators/PropertyChangedGenerator.cs @@ -155,7 +155,13 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode) .FirstOrDefault() ?.Name ?.ToString(); + + namespaceName ??= editedClass.Ancestors() .OfType() + .FirstOrDefault() + ?.Name + ?.ToString(); + namespaceName ??= String.Empty; var className = editedClass.Identifier.ToString(); var fullName = $"{namespaceName}.{className}";