Skip to content

Commit

Permalink
Merge pull request #5 from raffaeler/FileScopedBug
Browse files Browse the repository at this point in the history
Support for FileScopedNamespaceDeclarationSyntax
  • Loading branch information
raffaeler authored May 26, 2023
2 parents cbfb0e4 + 93134fc commit 4f905d6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/ConsoleTest/ConsoleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<!--<ImplicitUsings>enable</ImplicitUsings>-->
<LangVersion>11</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
15 changes: 15 additions & 0 deletions src/ConsoleTest/FileScopedClass.cs
Original file line number Diff line number Diff line change
@@ -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;
}
29 changes: 29 additions & 0 deletions src/LibraryTest/FileScopedClass.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
4 changes: 2 additions & 2 deletions src/LibraryTest/ShortDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LibraryTest
{
internal partial class ShortDemo
{
[MakeProperty("Name")]
private string? _name;
//[MakeProperty("Name")]
//private string? _name;
}
}
7 changes: 7 additions & 0 deletions src/SpeedyGenerators/PropertyChangedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
.FirstOrDefault()
?.Name
?.ToString();

namespaceName ??= editedClass.Ancestors()
.OfType<FileScopedNamespaceDeclarationSyntax>()
.FirstOrDefault()
?.Name
?.ToString();

namespaceName ??= String.Empty;
var className = editedClass.Identifier.ToString();
var fullName = $"{namespaceName}.{className}";
Expand Down

0 comments on commit 4f905d6

Please sign in to comment.