diff --git a/lua_typed_macro/src/lib.rs b/lua_typed_macro/src/lib.rs index 5e6d270..b3af2fa 100644 --- a/lua_typed_macro/src/lib.rs +++ b/lua_typed_macro/src/lib.rs @@ -54,7 +54,7 @@ impl ToTokens for Struct { tokens.extend(quote! { fn generate_header() -> Option { let type_name = ::type_name(); - Some(format!("---@class {type_name}\nlocal {type_name}\n")) + Some(format!("---@class {type_name}\n")) } fn generate_members() -> Option { @@ -66,6 +66,11 @@ impl ToTokens for Struct { Some(output) } + + fn generate_footer() -> Option { + let type_name = ::type_name(); + Some(format!("local {type_name}\n")) + } }); } } @@ -202,7 +207,7 @@ impl ToTokens for ExtTaggedEnum { tokens.extend(quote! { fn generate_header() -> Option { let type_name = ::type_name(); - Some(format!("---@class {type_name}\nlocal {type_name}\n")) + Some(format!("---@class {type_name}\n")) } fn generate_members() -> Option { @@ -219,6 +224,11 @@ impl ToTokens for ExtTaggedEnum { Some(output) } + + fn generate_footer() -> Option { + let type_name = ::type_name(); + Some(format!("local {type_name}\n")) + } }); } } diff --git a/src/lib.rs b/src/lib.rs index 70ac47a..da11f8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,10 @@ pub trait Typed { None } + fn generate_footer() -> Option { + None + } + fn generate_full() -> Option { let mut output = String::new(); @@ -24,6 +28,10 @@ pub trait Typed { output += members; } + if let Some(footer) = &Self::generate_footer() { + output += footer; + } + Some(output) } } diff --git a/tests/flatten.rs b/tests/flatten.rs index 9c8ac99..88dcba7 100644 --- a/tests/flatten.rs +++ b/tests/flatten.rs @@ -17,9 +17,9 @@ pub struct A { fn flatten() { insta::assert_snapshot!(::generate_full().unwrap(), @r" ---@class A - local A ---@field hello string ---@field world boolean ---@field cool integer + local A "); } diff --git a/tests/notification.rs b/tests/notification.rs index 50ada08..a8d31bc 100644 --- a/tests/notification.rs +++ b/tests/notification.rs @@ -14,10 +14,10 @@ pub enum ActionType { fn action_type() { insta::assert_snapshot!(::generate_full().unwrap(), @r#" ---@class ActionType - local ActionType ---@field action ---| "broadcast" ---@field extras table? + local ActionType "#); } @@ -56,12 +56,12 @@ pub struct Action { fn action() { insta::assert_snapshot!(::generate_full().unwrap(), @r#" ---@class Action - local Action ---@field action ---| "broadcast" ---@field extras table? ---@field label string ---@field clear boolean? + local Action "#); } @@ -80,11 +80,11 @@ pub struct Notification { fn notification() { insta::assert_snapshot!(::generate_full().unwrap(), @r" ---@class Notification - local Notification ---@field title string ---@field message string? ---@field tags string[]? ---@field priority Priority? ---@field actions Action[]? + local Notification "); } diff --git a/tests/rename.rs b/tests/rename.rs index 76b4d94..11b2b4b 100644 --- a/tests/rename.rs +++ b/tests/rename.rs @@ -21,7 +21,7 @@ fn rename() { fn rename_nested() { insta::assert_snapshot!(::generate_full().unwrap(), @r" ---@class Other - local Other ---@field rust Lua + local Other "); }