#Support file-scoped namespace for Entities source generators

1 messages · Page 1 of 1 (latest)

cinder pulsar
#

Entities 1.4.3
Currently the source generators are explicitly use NamespaceDeclarationSyntax thus makes it impossible for users to write code like this:

using Game.Components;
using Unity.Entities;
using UnityEngine;

namespace Game.Systems; // file-scoped namespace

public partial struct SampleSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        var query = SystemAPI.QueryBuilder()
        .WithAll<Hp>()
        .Build();

        state.RequireForUpdate(query);
    }

    public void OnUpdate(ref SystemState state)
    {
        Debug.Log(SystemAPI.Time.DeltaTime);
    }
}

Trying to use this system will just throw

InvalidOperationException: No suitable code replacement generated, this is either due to generators failing, or lack of support in your current context

On the source-gen side, for most cases I've been experimenting with, NamespaceDeclarationSyntax can simply be replaced by BaseNamespaceDeclarationSyntax.

For example, at file PartialSystemTypeGenerator.cs, line 41, this is the original code:

case NamespaceDeclarationSyntax parentNameSpaceSyntax:
    foreach (var usingDir in parentNameSpaceSyntax.Usings)
        opening.Push(($"{usingDir}", AddIndentAfter: false));
    opening.Push(("{", AddIndentAfter: true));
    opening.Push(($"namespace {parentNameSpaceSyntax.Name}", AddIndentAfter: false));
    break;

I can just change the type name and no more:

case BaseNamespaceDeclarationSyntax parentNameSpaceSyntax:
    foreach (var usingDir in parentNameSpaceSyntax.Usings)
        opening.Push(($"{usingDir}", AddIndentAfter: false));
    opening.Push(("{", AddIndentAfter: true));
    opening.Push(($"namespace {parentNameSpaceSyntax.Name}", AddIndentAfter: false));
    break;
sour vapor
#

File scope namespaces aren't even supported by unity last I checked, you are manually changing your c# version correct?