Skip to content

Commit 4e6bd93

Browse files
committed
Change notification enumerators from PascalCase to SHOUT_CASE
1 parent c2a59a7 commit 4e6bd93

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

godot-codegen/src/conv/name_conversions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn to_pascal_case(ty_name: &str) -> String {
7777
.replace("Sdfgiy", "SdfgiY")
7878
}
7979

80+
#[allow(dead_code)] // Keep around in case we need it later.
8081
pub fn shout_to_pascal(shout_case: &str) -> String {
8182
// TODO use heck?
8283

godot-codegen/src/generator/notifications.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use crate::context::Context;
99
use crate::models::domain::TyName;
1010
use crate::models::json::JsonClassConstant;
11-
use crate::{conv, util};
11+
use crate::util;
1212
use proc_macro2::{Ident, TokenStream};
13-
use quote::{format_ident, quote};
13+
use quote::quote;
1414

1515
pub fn make_notify_methods(class_name: &TyName, ctx: &mut Context) -> TokenStream {
1616
// Note: there are two more methods, but only from Node downwards, not from Object:
@@ -85,10 +85,10 @@ pub fn make_notification_enum(
8585
c = class_name.rust_ty
8686
);
8787

88-
let mut notification_enumerators_pascal = Vec::new();
88+
let mut notification_enumerators_shout = Vec::new();
8989
let mut notification_enumerators_ord = Vec::new();
9090
for (constant_ident, constant_value) in all_constants {
91-
notification_enumerators_pascal.push(constant_ident);
91+
notification_enumerators_shout.push(constant_ident);
9292
notification_enumerators_ord.push(constant_value);
9393
}
9494

@@ -101,10 +101,11 @@ pub fn make_notification_enum(
101101
/// Contains the [`Unknown`] variant for forward compatibility.
102102
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
103103
#[repr(i32)]
104+
#[allow(non_camel_case_types)]
104105
#cfg_attributes
105106
pub enum #enum_name {
106107
#(
107-
#notification_enumerators_pascal = #notification_enumerators_ord,
108+
#notification_enumerators_shout = #notification_enumerators_ord,
108109
)*
109110

110111
/// Since Godot represents notifications as integers, it's always possible that a notification outside the known types
@@ -117,7 +118,7 @@ pub fn make_notification_enum(
117118
fn from(enumerator: i32) -> Self {
118119
match enumerator {
119120
#(
120-
#notification_enumerators_ord => Self::#notification_enumerators_pascal,
121+
#notification_enumerators_ord => Self::#notification_enumerators_shout,
121122
)*
122123
other_int => Self::Unknown(other_int),
123124
}
@@ -128,7 +129,7 @@ pub fn make_notification_enum(
128129
fn from(notification: #enum_name) -> i32 {
129130
match notification {
130131
#(
131-
#enum_name::#notification_enumerators_pascal => #notification_enumerators_ord,
132+
#enum_name::#notification_enumerators_shout => #notification_enumerators_ord,
132133
)*
133134
#enum_name::Unknown(int) => int,
134135
}
@@ -141,27 +142,27 @@ pub fn make_notification_enum(
141142

142143
/// Tries to interpret the constant as a notification one, and transforms it to a Rust identifier on success.
143144
pub fn try_to_notification(constant: &JsonClassConstant) -> Option<Ident> {
144-
constant
145-
.name
146-
.strip_prefix("NOTIFICATION_")
147-
.map(|s| util::ident(&conv::shout_to_pascal(s)))
145+
constant.name.strip_prefix("NOTIFICATION_").map(util::ident) // used to be conv::shout_to_pascal(s)
148146
}
149147

150148
// ----------------------------------------------------------------------------------------------------------------------------------------------
151149
// Implementation
152150

153-
/// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839
151+
/// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839, fixed in 4.2.
154152
///
155153
/// Godot has a collision for two notification constants (DRAW, NODE_CACHE_REQUESTED) in the same inheritance branch (as of 4.0.2).
156154
/// This cannot be represented in a Rust enum, so we merge the two constants into a single enumerator.
157155
fn workaround_constant_collision(all_constants: &mut Vec<(Ident, i32)>) {
156+
#[cfg(before_api = "4.2")]
158157
for first in ["Draw", "VisibilityChanged"] {
159158
if let Some(index_of_draw) = all_constants
160159
.iter()
161160
.position(|(constant_name, _)| constant_name == first)
162161
{
163-
all_constants[index_of_draw].0 = format_ident!("{first}OrNodeRecacheRequested");
162+
all_constants[index_of_draw].0 = quote::format_ident!("{first}OrNodeRecacheRequested");
164163
all_constants.retain(|(constant_name, _)| constant_name != "NodeRecacheRequested");
165164
}
166165
}
166+
167+
let _ = &all_constants; // unused warning
167168
}

itest/rust/src/object_tests/onready_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn onready_lifecycle_forget() {
5858
expect_panic(
5959
"Forgetting to initialize OnReady during ready() panics",
6060
move || {
61-
forgetful.notify(NodeNotification::Ready);
61+
forgetful.notify(NodeNotification::READY);
6262
},
6363
);
6464

@@ -69,7 +69,7 @@ fn onready_lifecycle_forget() {
6969
fn onready_lifecycle() {
7070
let mut obj = OnReadyWithImpl::create(true);
7171

72-
obj.notify(NodeNotification::Ready);
72+
obj.notify(NodeNotification::READY);
7373

7474
{
7575
let mut obj = obj.bind_mut();
@@ -87,7 +87,7 @@ fn onready_lifecycle() {
8787
fn onready_lifecycle_without_impl() {
8888
let mut obj = OnReadyWithoutImpl::create();
8989

90-
obj.notify(NodeNotification::Ready);
90+
obj.notify(NodeNotification::READY);
9191

9292
{
9393
let mut obj = obj.bind_mut();
@@ -104,7 +104,7 @@ fn onready_lifecycle_without_impl() {
104104
fn onready_lifecycle_with_impl_without_ready() {
105105
let mut obj = OnReadyWithImplWithoutReady::create();
106106

107-
obj.notify(NodeNotification::Ready);
107+
obj.notify(NodeNotification::READY);
108108

109109
{
110110
let mut obj = obj.bind_mut();
@@ -123,7 +123,7 @@ fn onready_lifecycle_with_impl_without_ready() {
123123
#[itest]
124124
fn onready_property_access() {
125125
let mut obj = OnReadyWithImpl::create(true);
126-
obj.notify(NodeNotification::Ready);
126+
obj.notify(NodeNotification::READY);
127127

128128
obj.set("auto".into(), 33.to_variant());
129129
obj.set("manual".into(), 44.to_variant());

itest/rust/src/object_tests/virtual_methods_test.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,19 +523,19 @@ fn test_input_event_multiple(test_context: &TestContext) {
523523
fn test_notifications() {
524524
let obj = NotificationTest::new_alloc();
525525
let mut node = obj.clone().upcast::<Node>();
526-
node.notify(NodeNotification::Unpaused);
527-
node.notify(NodeNotification::EditorPostSave);
528-
node.notify(NodeNotification::Ready);
529-
node.notify_reversed(NodeNotification::WmSizeChanged);
526+
node.notify(NodeNotification::UNPAUSED);
527+
node.notify(NodeNotification::EDITOR_POST_SAVE);
528+
node.notify(NodeNotification::READY);
529+
node.notify_reversed(NodeNotification::WM_SIZE_CHANGED);
530530

531531
assert_eq!(
532532
obj.bind().sequence,
533533
vec![
534-
ReceivedEvent::Notification(NodeNotification::Unpaused),
535-
ReceivedEvent::Notification(NodeNotification::EditorPostSave),
534+
ReceivedEvent::Notification(NodeNotification::UNPAUSED),
535+
ReceivedEvent::Notification(NodeNotification::EDITOR_POST_SAVE),
536536
ReceivedEvent::Ready,
537-
ReceivedEvent::Notification(NodeNotification::Ready),
538-
ReceivedEvent::Notification(NodeNotification::WmSizeChanged),
537+
ReceivedEvent::Notification(NodeNotification::READY),
538+
ReceivedEvent::Notification(NodeNotification::WM_SIZE_CHANGED),
539539
]
540540
);
541541
obj.free();

0 commit comments

Comments
 (0)