Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions ModInteropImportGenerator.Sample/CommunalHelperImports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
using ModInteropImportGenerator.Sample.Stubs;

namespace ModInteropImportGenerator.Sample;

[GenerateImports("CommunalHelper.DashStates")]
public static partial class DashStates
partial class Sample
{
public static partial int GetDreamTunnelDashState();
public static partial bool HasDreamTunnelDash();
public static partial int GetDreamTunnelDashCount();
public static partial ComponentStub DreamTunnelInteraction(
Action<PlayerStub> onPlayerEnter,
Action<PlayerStub> onPlayerExit);
public static partial bool HasSeekerDash();
public static partial bool IsSeekerDashAttacking();
[GenerateImports("CommunalHelper.DashStates")]
public static partial class DashStates
{
public static partial int GetDreamTunnelDashState();
public static partial bool HasDreamTunnelDash();
public static partial int GetDreamTunnelDashCount();
public static partial ComponentStub DreamTunnelInteraction(
Action<PlayerStub> onPlayerEnter,
Action<PlayerStub> onPlayerExit);
public static partial bool HasSeekerDash();
public static partial bool IsSeekerDashAttacking();
}
}
13 changes: 11 additions & 2 deletions ModInteropImportGenerator/ModInteropImportSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,18 @@ private static void GenerateCode(

sourceGen.AddUsings("System", "System.Diagnostics", "MonoMod.ModInterop", "ModInteropImportGenerator");

sourceGen.WriteLine($"public static partial class {sourceGen.ClassName}");
using (sourceGen.UseCodeBlock())
var classes = classDeclaration.AncestorsAndSelf().OfType<ClassDeclarationSyntax>().ToArray();
using (var indent = sourceGen.StartMultiIndent())
{
for (int i = classes.Length - 1; i >= 0; i--)
{
sourceGen.WriteLine(classes[i] switch
{
ClassDeclarationSyntax c => $"public static partial class {c.Identifier.Text}",
});
indent.Indent();
}

sourceGen.WriteLine(
$"public static {ImportStateEnumTypeName} {SourceGenerators.ImportStateFieldName} "
+ $"{{ get; private set; }}");
Expand Down
44 changes: 37 additions & 7 deletions ModInteropImportGenerator/SimpleSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public int IndentLevel
public readonly ModImportMetadata ImportMeta;
public readonly SemanticModel SemanticModel;

public readonly string Namespace;
public readonly INamespaceSymbol Namespace;
public readonly string ClassName;

public SimpleSourceGenerator(ClassDeclarationSyntax classDeclaration,
Expand All @@ -42,7 +42,7 @@ public SimpleSourceGenerator(ClassDeclarationSyntax classDeclaration,
$"Attempted to create a source generator for something that isn't an {nameof(INamedTypeSymbol)}.");

ClassSymbol = typeSymbol;
Namespace = typeSymbol.ContainingNamespace.ToDisplayString();
Namespace = typeSymbol.ContainingNamespace;
ClassName = classDeclaration.Identifier.Text;
}

Expand Down Expand Up @@ -104,12 +104,15 @@ public SimpleSourceGenerator Write(char c)

public string Generate()
{
var ns = "";
if (!Namespace.IsGlobalNamespace)
{
ns = $"\nnamespace {Namespace.ToDisplayString()};\n";
}
return $"""
// <auto-generated />
{string.Join("\n", Usings.Select(usingNamespace => $"using {usingNamespace};"))}

namespace {Namespace};

{ns}
{SourceCode}
""";
}
Expand All @@ -125,6 +128,8 @@ public SimpleSourceGenerator Dedent(int dedentCount = 1)
IndentLevel -= dedentCount;
return this;
}
public MultipleIndentedCodeBlockContext StartMultiIndent(int indentCount = 1)
=> new(this, indentCount);

public IndentContext UseIndent(int indentCount = 1)
=> new(this, indentCount);
Expand All @@ -139,8 +144,8 @@ private void WriteIndent()
return;

for (int i = 0; i < IndentLevel; i++)
for (int j = 0; j < 4; j++)
SourceCode.Append(' ');
for (int j = 0; j < 4; j++)
SourceCode.Append(' ');
}

public ref struct IndentContext : IDisposable
Expand Down Expand Up @@ -199,4 +204,29 @@ public void Dispose()
SourceGen.WriteLine();
}
}
public ref struct MultipleIndentedCodeBlockContext(SimpleSourceGenerator sourceGen, int indentCount) : IDisposable
{
private bool Disposed;
private int IndentTimes;

public void Indent()
{
IndentTimes++;
sourceGen.WriteLine('{');
sourceGen.IndentLevel += indentCount;
}

public void Dispose()
{
if (Disposed)
throw new ObjectDisposedException(nameof(IndentedCodeBlockContext));

Disposed = true;
for (int i = 0; i < IndentTimes; i++)
{
sourceGen.IndentLevel -= indentCount;
sourceGen.WriteLine('}');
}
}
}
}