From ebea44e70c33c71571cf17a6d92502ead811a5b5 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 17 Sep 2025 01:27:59 +0200 Subject: [PATCH] feat: Create local variable to allow for method definitions --- lua_typed_macro/src/lib.rs | 6 ++++-- tests/flatten.rs | 1 + tests/notification.rs | 3 +++ tests/rename.rs | 6 +++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lua_typed_macro/src/lib.rs b/lua_typed_macro/src/lib.rs index 1da7794..e3011e6 100644 --- a/lua_typed_macro/src/lib.rs +++ b/lua_typed_macro/src/lib.rs @@ -54,7 +54,8 @@ impl ToTokens for Struct { tokens.extend(quote! { fn generate_header() -> Option { - Some(format!("---@class {}\n", <#name as Typed>::type_name())) + let type_name = <#name as Typed>::type_name(); + Some(format!("---@class {type_name}\nlocal {type_name}\n")) } fn generate_members() -> Option { @@ -217,7 +218,8 @@ impl ToTokens for ExtTaggedEnum { tokens.extend(quote! { fn generate_header() -> Option { - Some(format!("---@class {}\n", <#name as Typed>::type_name())) + let type_name = <#name as Typed>::type_name(); + Some(format!("---@class {type_name}\nlocal {type_name}\n")) } fn generate_members() -> Option { diff --git a/tests/flatten.rs b/tests/flatten.rs index f3b2c97..9c8ac99 100644 --- a/tests/flatten.rs +++ b/tests/flatten.rs @@ -17,6 +17,7 @@ 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 diff --git a/tests/notification.rs b/tests/notification.rs index a2b350e..50ada08 100644 --- a/tests/notification.rs +++ b/tests/notification.rs @@ -14,6 +14,7 @@ pub enum ActionType { fn action_type() { insta::assert_snapshot!(::generate_full().unwrap(), @r#" ---@class ActionType + local ActionType ---@field action ---| "broadcast" ---@field extras table? @@ -55,6 +56,7 @@ pub struct Action { fn action() { insta::assert_snapshot!(::generate_full().unwrap(), @r#" ---@class Action + local Action ---@field action ---| "broadcast" ---@field extras table? @@ -78,6 +80,7 @@ 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[]? diff --git a/tests/rename.rs b/tests/rename.rs index 98dfb3c..b659adc 100644 --- a/tests/rename.rs +++ b/tests/rename.rs @@ -11,13 +11,17 @@ struct Other { #[test] fn rename() { - insta::assert_snapshot!(::generate_full().unwrap(), @"---@class Lua"); + insta::assert_snapshot!(::generate_full().unwrap(), @r" + ---@class Lua + local Lua + "); } #[test] fn rename_nested() { insta::assert_snapshot!(::generate_full().unwrap(), @r" ---@class Other + local Other ---@field rust Lua "); }