Skip to content

Commit 0ba5d6e

Browse files
committed
Shorten enum names (#775)
* Shortening of enum names
1 parent 92558e6 commit 0ba5d6e

File tree

284 files changed

+4247
-4187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+4247
-4187
lines changed

docs/src/doc/user-guide/api-differences.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ To avoid confusion and conflict with Kotlin types, the following Godot symbol is
132132
- `Array` -> `VariantArray` (to avoid confusion with a built-in type in Kotlin)
133133
- `PackedXArray::toByteArray` -> `PackedXArray::toPackedByteArray` (to avoid confusion with a built-in type in Kotlin)
134134
- `PackedByteArray::toXArray` -> `PackedByteArray::toPackedXArray` (to avoid confusion with a built-in type in Kotlin)
135+
- All enum values are shortened, the name of the enum itself has been removed. Here are some examples:
136+
- `Error.ERR_PARAMETER_RANGE_ERROR` -> `Error.PARAMETER_RANGE`
137+
- `MethodFlags.METHOD_FLAG_NORMAL` -> `MethodFlags.NORMAL`
138+
- `ProcessThreadMessages.FLAG_PROCESS_THREAD_MESSAGES_PHYSICS` -> `Error.FLAG_PHYSICS`
135139
136140
## Global functions
137141
@@ -207,4 +211,4 @@ If you want logs to appear both in CLI and in the Godot Editor you will have to
207211
GD.print("Hello There!")
208212
```
209213
210-
Kotlin's print functions, on the other hand, will only print to CLI! They will print to Godot editor's output panel.
214+
Kotlin's print functions, on the other hand, will only print to CLI! They won't print to Godot editor's output panel.

harness/tests/src/main/java/godot/tests/JavaTestClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void connectAndTriggerSignal() {
9494
connect(
9595
StringNames.asStringName("test_signal"),
9696
new NativeCallable(this, StringNames.asStringName("signal_callback")),
97-
(int) ConnectFlags.CONNECT_ONE_SHOT.getId()
97+
(int) ConnectFlags.ONE_SHOT.getId()
9898
);
9999
emitSignal(StringNames.asStringName("test_signal"));
100100
}

kt/api-generator/src/main/kotlin/godot/codegen/models/enriched/EnrichedEnum.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import godot.codegen.models.Enum
66
import godot.codegen.traits.IDocumented
77
import godot.codegen.traits.TypedTrait
88
import godot.codegen.workarounds.sanitizeApiType
9+
import godot.common.extensions.isValidKotlinIdentifier
10+
import godot.common.extensions.removeWords
11+
import godot.common.extensions.removePrefixWords
12+
import godot.common.extensions.removeSuffixWords
13+
import godot.common.extensions.toUpperSnakeCase
914

1015
class EnrichedEnum(model: Enum, val outerClass: String?) : TypedTrait {
1116
val simpleName = model.name.sanitizeApiType()
@@ -19,14 +24,34 @@ class EnrichedEnum(model: Enum, val outerClass: String?) : TypedTrait {
1924
val values = model.values.map {
2025
EnrichedEnumValue(
2126
it.name,
27+
simpleName,
2228
it.value,
2329
it.description ?: ""
2430
)
2531
}
2632
}
2733

28-
class EnrichedEnumValue(val name: String, val value: Long, override val description: String?) : IDocumented
34+
class EnrichedEnumValue(valueName: String, ownerName: String, val value: Long, override val description: String?) : IDocumented {
35+
val name = run {
36+
val uppercaseName = ownerName.toUpperSnakeCase()
37+
val prefixRemoved = valueName
38+
.removePrefixWords(uppercaseName)
39+
.removePrefix("_")
40+
.takeIf { it.isValidKotlinIdentifier() }
41+
?: valueName
2942

43+
val suffixRemoved = prefixRemoved
44+
.removeSuffixWords(uppercaseName)
45+
.removePrefix("_")
46+
.takeIf { it.isValidKotlinIdentifier() }
47+
?: prefixRemoved
48+
49+
suffixRemoved
50+
.removeWords(uppercaseName)
51+
.takeIf { it.isValidKotlinIdentifier() }
52+
?: suffixRemoved
53+
}
54+
}
3055

3156
fun List<Enum>.toEnriched(outerClass: String? = null) = map { EnrichedEnum(it, outerClass) }
3257
fun Enum.toEnriched(outerClass: String? = null) = EnrichedEnum(this, outerClass)

kt/api-generator/src/main/kotlin/godot/codegen/services/impl/AwaitGenerationService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ object AwaitGenerationService : IAwaitGenerationService {
145145
.beginControlFlow(".%M", AS_CALLABLE_UTIL_FUNCTION)
146146
.addStatement("cont.%L()", cancel)
147147
.endControlFlow()
148-
.addCode(",·%T.ConnectFlags.CONNECT_ONE_SHOT.id.toInt())", GODOT_OBJECT)
148+
.addCode(",·%T.ConnectFlags.ONE_SHOT.id.toInt())", GODOT_OBJECT)
149149
.endControlFlow()
150150
}
151151
}

kt/common/src/main/kotlin/godot/common/extensions/StringExtensions.kt

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,64 @@ fun String.convertToSnakeCase(): String =
4848
else accumulator.append(character)
4949
}.toString()
5050

51-
fun String.removeSuffixWords(wordWithDashes: String): String {
51+
fun String.toUpperSnakeCase(): String {
52+
if (this.isEmpty()) return this
53+
val originalString = this
54+
55+
val result = buildString {
56+
for (i in originalString.indices) {
57+
val currentChar = originalString[i]
58+
if (i > 0 && currentChar.isUpperCase()) {
59+
val prevChar = originalString[i - 1]
60+
// Insert underscore if the previous character is lowercase
61+
// OR if the previous character is uppercase but the next character exists and is lowercase
62+
if (prevChar.isLowerCase() || (prevChar.isUpperCase() && i < originalString.lastIndex && originalString[i + 1].isLowerCase())) {
63+
append('_')
64+
}
65+
}
66+
append(currentChar)
67+
}
68+
}
69+
return result.uppercase(Locale.getDefault())
70+
}
71+
72+
fun String.removePrefixWords(snakeCaseString: String): String {
73+
// We split the 2 strings into different "words"
74+
val otherWords = snakeCaseString.split('_')
75+
val valueWords = this.split('_')
76+
77+
var index = 0
78+
for (enumWord in otherWords) {
79+
if (index >= valueWords.size) break
80+
val valueWord = valueWords[index]
81+
// Remove the word if the receiver word is not longer than the parameter word
82+
// and the parameter word starts with the entire receiver word.
83+
if (valueWord.length <= enumWord.length && enumWord.startsWith(valueWord)) {
84+
index++
85+
} else {
86+
break
87+
}
88+
}
89+
// Join the remaining words with underscores.
90+
return valueWords.drop(index).joinToString("_")
91+
}
92+
93+
fun String.removeWords(other: String) = replace("_" + other + "_", "_")
94+
95+
fun String.removeSuffixWords(snakeCaseString: String): String {
5296
// We split the 2 strings into different "words"
53-
val otherWords = wordWithDashes.split('_')
97+
val otherWords = snakeCaseString.split('_')
5498
val valueWords = this.split('_')
5599

56100
var valueIndex = valueWords.size - 1
57101
var enumIndex = otherWords.size - 1
58102

59-
// Remove matching words from the end of the other value.
103+
// Remove matching words from the end of the parameter value.
60104
while (valueIndex >= 0 && enumIndex >= 0) {
61105
val valueWord = valueWords[valueIndex]
62106
val enumWord = otherWords[enumIndex]
63-
// If the receiver word is not longer than the other word and the other word starts with the receiver word,
64-
// we consider it a match.
107+
// Remove the word if the receiver word is not longer than the parameter word
108+
// and the parameter word starts with the entire receiver word.
65109
if (valueWord.length <= enumWord.length && enumWord.startsWith(valueWord)) {
66110
valueIndex--
67111
enumIndex--

kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/generator/FunctionRegistrationGenerator.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ object FunctionRegistrationGenerator {
131131
private fun getRpcModeEnum(registeredFunction: RegisteredFunction): ClassName {
132132
return when (registeredFunction.annotations.getAnnotation<RpcAnnotation>()?.rpcMode) {
133133
null,
134-
RpcMode.DISABLED -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "RPC_MODE_DISABLED")
135-
RpcMode.ANY -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "RPC_MODE_ANY_PEER")
136-
RpcMode.AUTHORITY -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "RPC_MODE_AUTHORITY")
134+
RpcMode.DISABLED -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "DISABLED")
135+
RpcMode.ANY -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "ANY_PEER")
136+
RpcMode.AUTHORITY -> ClassName("$godotApiPackage.${GodotTypes.rpcMode}", "AUTHORITY")
137137
}
138138
}
139139

140140
private fun getRpcTransferModeEnum(registeredFunction: RegisteredFunction): ClassName {
141141
return when (registeredFunction.annotations.getAnnotation<RpcAnnotation>()?.transferMode) {
142142
null,
143-
TransferMode.RELIABLE -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "TRANSFER_MODE_RELIABLE")
144-
TransferMode.UNRELIABLE -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "TRANSFER_MODE_UNRELIABLE")
145-
TransferMode.UNRELIABLE_ORDERED -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "TRANSFER_MODE_UNRELIABLE_ORDERED")
143+
TransferMode.RELIABLE -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "RELIABLE")
144+
TransferMode.UNRELIABLE -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "UNRELIABLE")
145+
TransferMode.UNRELIABLE_ORDERED -> ClassName("$godotApiPackage.${GodotTypes.transferMode}", "UNRELIABLE_ORDERED")
146146
}
147147
}
148148

kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/generator/PropertyRegistrationGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ object PropertyRegistrationGenerator {
142142
return if (registeredProperty.annotations.hasAnnotation<ExportAnnotation>()) {
143143
ClassName(
144144
"$godotCorePackage.${GodotTypes.propertyUsage}",
145-
"PROPERTY_USAGE_DEFAULT"
145+
"DEFAULT"
146146
)
147147
} else {
148148
ClassName(
149149
"$godotCorePackage.${GodotTypes.propertyUsage}",
150-
"PROPERTY_USAGE_NONE"
150+
"NONE"
151151
)
152152
}
153153
}

kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/generator/hintstring/ArrayAndDictionaryHintStringGenerator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ArrayAndDictionaryHintStringGenerator(
7777
// -> 2 == int -> variant type ordinal
7878
// example: Dictionary<Int, Button>() -> 2:int;24/34:Button
7979
// -> 24 == Object -> variant type ordinal -> VariantParser.OBJECT.id
80-
// -> 34 == Button -> node type property hint ordinal -> PropertyHint.PROPERTY_HINT_NODE_TYPE.id
80+
// -> 34 == Button -> node type property hint ordinal -> PropertyHint.NODE_TYPE.id
8181
loop@ while (currentElementType != null) {
8282
when {
8383
currentElementType.isCompatibleList() -> {
@@ -107,7 +107,7 @@ class ArrayAndDictionaryHintStringGenerator(
107107
?: currentElementType.baseGodotType()?.fqName?.substringAfterLast(".")
108108

109109
val subTypeString = if (className != null) {
110-
"/${PropertyHint.PROPERTY_HINT_NODE_TYPE.id}:$className"
110+
"/${PropertyHint.NODE_TYPE.id}:$className"
111111
} else {
112112
""
113113
}
@@ -123,7 +123,7 @@ class ArrayAndDictionaryHintStringGenerator(
123123
?: currentElementType.baseGodotType()?.fqName?.substringAfterLast(".")
124124

125125
val subTypeString = if (className != null) {
126-
"/${PropertyHint.PROPERTY_HINT_RESOURCE_TYPE.id}:$className"
126+
"/${PropertyHint.RESOURCE_TYPE.id}:$className"
127127
} else {
128128
""
129129
}

kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/generator/hintstring/PropertyHintStringGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package godot.entrygenerator.generator.hintstring
33
import godot.entrygenerator.model.PropertyHintAnnotation
44
import godot.entrygenerator.model.RegisteredProperty
55

6-
abstract class PropertyHintStringGenerator<PROPERTY_HINT_ANNOTATION_TYPE: PropertyHintAnnotation> (
6+
abstract class PropertyHintStringGenerator<ANNOTATION_TYPE: PropertyHintAnnotation> (
77
val registeredProperty: RegisteredProperty
88
) {
99
@Suppress("UNCHECKED_CAST")
1010
protected val propertyHintAnnotation = registeredProperty
1111
.annotations
12-
.firstOrNull { it is PropertyHintAnnotation } as PROPERTY_HINT_ANNOTATION_TYPE?
12+
.firstOrNull { it is PropertyHintAnnotation } as ANNOTATION_TYPE?
1313

1414
/**
1515
* Hint string formatting: https://github.com/godotengine/godot/blob/dcd11faad3802679a43b27155f1b6bc59aa39b60/core/object.h

kt/entry-generation/godot-entry-generator/src/main/kotlin/godot/entrygenerator/generator/typehint/PropertyTypeHintProvider.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ object PropertyTypeHintProvider {
2525
): ClassName {
2626
return when {
2727
registeredProperty.type.fqName == Int::class.qualifiedName -> if (registeredProperty.annotations.hasAnnotation<IntFlagHintAnnotation>()) {
28-
ClassName("$godotCorePackage.${GodotTypes.propertyHint}", "PROPERTY_HINT_FLAGS")
28+
ClassName("$godotCorePackage.${GodotTypes.propertyHint}", "FLAGS")
2929
} else {
3030
JvmPrimitivesTypeHintGenerator(registeredProperty).getPropertyTypeHint()
3131
}
3232
registeredProperty.type.fqName == String::class.qualifiedName -> when {
3333
registeredProperty.annotations.hasAnnotation<MultilineTextHintAnnotation>() -> {
3434
ClassName(
3535
"$godotCorePackage.${GodotTypes.propertyHint}",
36-
"PROPERTY_HINT_MULTILINE_TEXT"
36+
"MULTILINE_TEXT"
3737
)
3838
}
3939
registeredProperty.annotations.hasAnnotation<PlaceHolderTextHintAnnotation>() -> {
4040
ClassName(
4141
"$godotCorePackage.${GodotTypes.propertyHint}",
42-
"PROPERTY_HINT_PLACEHOLDER_TEXT"
42+
"PLACEHOLDER_TEXT"
4343
)
4444
}
4545
else -> {
@@ -60,22 +60,22 @@ object PropertyTypeHintProvider {
6060

6161
registeredProperty.type.isRefCounted() -> ClassName(
6262
"$godotCorePackage.${GodotTypes.propertyHint}",
63-
"PROPERTY_HINT_RESOURCE_TYPE"
63+
"RESOURCE_TYPE"
6464
)
6565

6666
registeredProperty.type.isDictionary() -> JvmArrayAndDictionaryTypeHintGenerator(registeredProperty).getPropertyTypeHint()
6767
registeredProperty.type.isCompatibleList() -> JvmArrayAndDictionaryTypeHintGenerator(registeredProperty).getPropertyTypeHint()
6868
registeredProperty.type.fqName.matches(Regex("^kotlin\\.collections\\..*Set\$")) -> ClassName(
6969
"$godotCorePackage.${GodotTypes.propertyHint}",
70-
"PROPERTY_HINT_RESOURCE_TYPE"
70+
"RESOURCE_TYPE"
7171
)
7272

7373
registeredProperty.type.isNodeType() -> ClassName(
7474
"$godotCorePackage.${GodotTypes.propertyHint}",
75-
"PROPERTY_HINT_NODE_TYPE"
75+
"NODE_TYPE"
7676
)
7777

78-
else -> ClassName("$godotCorePackage.${GodotTypes.propertyHint}", "PROPERTY_HINT_NONE")
78+
else -> ClassName("$godotCorePackage.${GodotTypes.propertyHint}", "NONE")
7979
}
8080
}
8181
}

0 commit comments

Comments
 (0)