From 3c50e8b0f99b097af4fe3b0782f70d6ee21545f2 Mon Sep 17 00:00:00 2001
From: PaulusParssinen <paulus.parssinen@gmail.com>
Date: Thu, 22 Feb 2024 13:24:35 +0200
Subject: [PATCH] lingo: minor adjustments to the source generation

---
 .../InstructionGeneratorTests.cs              | 13 +++++----
 .../InstructionGenerator.cs                   | 27 ++++++++++++-------
 2 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs b/Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs
index 6317251..55a24a0 100644
--- a/Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs
+++ b/Shockky.SourceGeneration.Tests/InstructionGeneratorTests.cs
@@ -20,7 +20,7 @@ public enum OPCode : byte
                 [OP] Return = 0x01,
             }
             """;
-        string expected = """
+        string expectedSyntaxForReturn = """
             namespace Shockky.Lingo.Instructions;
             
             [global::System.CodeDom.Compiler.GeneratedCode("InstructionGenerator", <ASSEMBLY_VERSION>)]
@@ -33,12 +33,11 @@ public sealed class Return : IInstruction
                 /// <inheritdoc />
                 public OPCode OP => OPCode.Return;
 
+                [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
+                [global::System.Diagnostics.DebuggerBrowsable(global::System.Diagnostics.DebuggerBrowsableState.Never)]
                 public int Immediate { get; set; }
 
-                public int GetSize()
-                {
-                    return sizeof(OPCode);
-                }
+                public int GetSize() => sizeof(OPCode);
 
                 public void WriteTo(global::Shockky.IO.ShockwaveWriter output)
                 {
@@ -49,7 +48,7 @@ public void WriteTo(global::Shockky.IO.ShockwaveWriter output)
 
         VerifyGenerateSources(source,
             [new InstructionGenerator()],
-            ("Shockky.Lingo.Instructions.Return.g.cs", expected));
+            ("Shockky.Lingo.Instructions.Return.g.cs", expectedSyntaxForReturn));
     }
 
     [Fact]
@@ -98,7 +97,7 @@ public void WriteTo(global::Shockky.IO.ShockwaveWriter output)
                         output.Write(op + 0x40);
                         output.Write((ushort)Immediate);
                     }
-                    else 
+                    else
                     {
                         output.Write(op + 0x80);
                         output.Write(Immediate);
diff --git a/Shockky.SourceGeneration/InstructionGenerator.cs b/Shockky.SourceGeneration/InstructionGenerator.cs
index 61a213e..3f13a74 100644
--- a/Shockky.SourceGeneration/InstructionGenerator.cs
+++ b/Shockky.SourceGeneration/InstructionGenerator.cs
@@ -127,25 +127,32 @@ internal static void WriteInstructionSyntax(IndentedTextWriter writer,
             writer.WriteLine($$"""
                 /// <inheritdoc />
                 public OPCode OP => OPCode.{{instruction.Name}};
-
-                public int Immediate { get; set; }
                 """, true);
+            writer.WriteLine();
+            
+            // If the OP has no immediate, hide it from debugger and editor on the concrete implementation.
+            if (!hasImmediate)
+            {
+                writer.WriteLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
+                writer.WriteLine("[global::System.Diagnostics.DebuggerBrowsable(global::System.Diagnostics.DebuggerBrowsableState.Never)]");
+            }
+            writer.WriteLine("public int Immediate { get; set; }");
 
             writer.WriteLine();
-            writer.WriteLine("public int GetSize()");
-            using (var getSizeBlock = writer.WriteBlock())
+
+            if (hasImmediate)
             {
-                if (hasImmediate)
-                {
-                    writer.WriteLine("""
+                writer.WriteLine("""
+                    public int GetSize()
+                    {
                         uint imm = (uint)Immediate;
                         if (imm <= byte.MaxValue) return sizeof(OPCode) + 1;
                         else if (imm <= ushort.MaxValue) return sizeof(OPCode) + 2;
                         else return sizeof(OPCode) + 4;
-                        """, true);
-                }
-                else writer.WriteLine("return sizeof(OPCode);");
+                    }
+                    """, true);
             }
+            else writer.WriteLine("public int GetSize() => sizeof(OPCode);");
 
             writer.WriteLine();
             writer.WriteLine("public void WriteTo(global::Shockky.IO.ShockwaveWriter output)");