Skip to content

Commit 59a2cba

Browse files
committed
Add additional mutation helpers for containers (#786)
* Add additional mutation helpers for containers
1 parent 3efb1f8 commit 59a2cba

File tree

218 files changed

+9093
-4502
lines changed

Some content is hidden

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

218 files changed

+9093
-4502
lines changed

kt/api-generator/src/main/kotlin/godot/codegen/generation/rule/PropertyRule.kt

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package godot.codegen.generation.rule
22

33
import com.squareup.kotlinpoet.AnnotationSpec
4+
import com.squareup.kotlinpoet.ClassName
45
import com.squareup.kotlinpoet.FunSpec
6+
import com.squareup.kotlinpoet.INT
57
import com.squareup.kotlinpoet.KModifier
68
import com.squareup.kotlinpoet.LambdaTypeName
79
import com.squareup.kotlinpoet.ParameterSpec
@@ -15,6 +17,7 @@ import godot.codegen.models.enriched.EnrichedClass
1517
import godot.codegen.models.enriched.EnrichedProperty
1618
import godot.tools.common.constants.CORE_TYPE_HELPER
1719
import godot.tools.common.constants.CORE_TYPE_LOCAL_COPY
20+
import godot.tools.common.constants.GodotTypes.localCopyCoreTypesMap
1821

1922
class PropertyRule : GodotApiRule<EnrichedPropertyTask>() {
2023
override fun apply(task: EnrichedPropertyTask, context: GenerationContext) = configure(task.builder) {
@@ -113,28 +116,36 @@ class PropertyRule : GodotApiRule<EnrichedPropertyTask>() {
113116
.build()
114117
)
115118
}
116-
117-
if (property.type.isLocalCopyCoreTypes()) {
118-
addAnnotation(CORE_TYPE_LOCAL_COPY)
119-
}
120-
121119
addKdoc(property)
122120
}
123121
}
124122

125-
class CoreTypeHelperRule : GodotApiRule<EnrichedClassTask>() {
123+
class LocalCopyHelperRule : GodotApiRule<EnrichedClassTask>() {
126124
override fun apply(task: EnrichedClassTask, context: GenerationContext) = with(task.builder) {
127125
val clazz = task.clazz
128126
for (propertyTask in task.enrichedProperties) {
129127
val property = propertyTask.property
130-
if (property.hasSetter && property.type.isLocalCopyCoreTypes()) {
131-
addFunction(getHelper(clazz, property))
132-
}
128+
if (!property.hasSetter || !property.type.isLocalCopyCoreTypes()) continue
129+
130+
propertyTask.builder.addAnnotation(CORE_TYPE_LOCAL_COPY)
131+
propertyTask.builder.addKdoc(
132+
"""|
133+
|
134+
|**Warning:**
135+
|Be careful when trying to modify a local [copy](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types) obtained from this getter.
136+
|Mutating it alone won't have any effect on the actual property, it has to be reassigned again afterward.
137+
|""".trimMargin()
138+
)
139+
addFunction(getMutateHelper(clazz, property))
140+
141+
if (!property.type.isLocalIndexedCopyCoreTypes()) continue
133142

143+
val className = localCopyCoreTypesMap[property.type.identifier]!!
144+
addFunction(getMutateEachHelper(property, className))
134145
}
135146
}
136147

137-
private fun getHelper(clazz: EnrichedClass, property: EnrichedProperty): FunSpec {
148+
private fun getMutateHelper(clazz: EnrichedClass, property: EnrichedProperty): FunSpec {
138149
val parameterTypeName = property.getCastedType()
139150
val parameterName = property.name
140151

@@ -153,24 +164,14 @@ class CoreTypeHelperRule : GodotApiRule<EnrichedClassTask>() {
153164
.addAnnotation(CORE_TYPE_HELPER)
154165
.returns(parameterTypeName)
155166
.addStatement(
156-
"""return $parameterName.apply{
157-
| block(this)
158-
| $parameterName = this
159-
|}
160-
|""".trimMargin()
167+
"""return·$parameterName.apply·{
168+
| block(this)
169+
| $parameterName·=·this
170+
|}""".trimMargin()
161171
).apply {
162172
val kDoc = buildString {
163-
val propertyKdoc = property.description
164-
if (propertyKdoc != null && propertyKdoc.isNotBlank()) {
165-
appendLine(propertyKdoc)
166-
appendLine()
167-
}
168-
169173
appendLine(
170-
"""This is a helper function to make dealing with local copies easier.
171-
|
172-
|For more information, see our [documentation](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types).
173-
|
174+
"""This is a helper function for [$parameterName] to make dealing with local copies easier.
174175
|Allow to directly modify the local copy of the property and assign it back to the Object.
175176
|
176177
|Prefer that over writing:
@@ -181,6 +182,59 @@ class CoreTypeHelperRule : GodotApiRule<EnrichedClassTask>() {
181182
|``````
182183
|""".trimMargin()
183184
)
185+
186+
187+
val propertyKdoc = property.description
188+
if (propertyKdoc != null && propertyKdoc.isNotBlank()) {
189+
appendLine(propertyKdoc)
190+
appendLine()
191+
}
192+
}
193+
addKdoc(kDoc)
194+
}.build()
195+
}
196+
197+
private fun getMutateEachHelper(property: EnrichedProperty, elementType: ClassName): FunSpec {
198+
val parameterTypeName = property.getCastedType()
199+
val parameterName = property.name
200+
201+
val propertyFunSpec = FunSpec.builder("${parameterName}MutateEach").addModifiers(KModifier.FINAL)
202+
203+
val lambdaType = ParameterSpec.builder(
204+
"block",
205+
LambdaTypeName.get(
206+
parameters = listOf(
207+
ParameterSpec.builder("index", INT).build(),
208+
ParameterSpec.builder("value", elementType).build()
209+
),
210+
returnType = UNIT
211+
)).build()
212+
213+
return propertyFunSpec
214+
.addParameter(lambdaType)
215+
.addAnnotation(CORE_TYPE_HELPER)
216+
.returns(parameterTypeName)
217+
.addStatement(
218+
"""return·$parameterName.apply·{
219+
| this.forEachIndexed {·index,·value·->
220+
| block(index,·value)
221+
| this[index]·=·value
222+
| }
223+
| $parameterName·=·this
224+
|}""".trimMargin()
225+
).apply {
226+
val kDoc = buildString {
227+
appendLine(
228+
"""This is a helper function for [$parameterName] to make dealing with local copies easier.
229+
|Allow to directly modify each element of the local copy of the property and assign it back to the Object.
230+
|""".trimMargin()
231+
)
232+
233+
val propertyKdoc = property.description
234+
if (propertyKdoc != null && propertyKdoc.isNotBlank()) {
235+
appendLine(propertyKdoc)
236+
appendLine()
237+
}
184238
}
185239
addKdoc(kDoc)
186240
}.build()

kt/api-generator/src/main/kotlin/godot/codegen/models/traits/GenerationType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ interface TypeGenerationTrait {
8383
fun isCoreType() = nature == Nature.CORE || nature == Nature.TYPED_ARRAY
8484
fun isPrimitive() = nature == Nature.PRIMITIVE
8585
fun isLocalCopyCoreTypes() = GodotTypes.localCopyCoreTypes.find { s -> s == identifier } != null
86+
fun isLocalIndexedCopyCoreTypes() = GodotTypes.indexedLocalCopyCoreTypes.find { s -> s == identifier } != null
8687
fun isEnum() = nature == Nature.ENUM
8788
fun isBitField() = nature == Nature.BITFIELD
8889
fun isTypedArray() = nature == Nature.TYPED_ARRAY

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import godot.codegen.generation.rule.BindingRule
66
import godot.codegen.generation.rule.BitfieldExtensionRule
77
import godot.codegen.generation.rule.ConstantRule
88
import godot.codegen.generation.rule.CoreRule
9-
import godot.codegen.generation.rule.CoreTypeHelperRule
9+
import godot.codegen.generation.rule.LocalCopyHelperRule
1010
import godot.codegen.generation.rule.DocumentationRule
1111
import godot.codegen.generation.rule.EnrichedClassRule
1212
import godot.codegen.generation.rule.EnrichedCoreRule
@@ -63,7 +63,7 @@ class ApiGenerationService(
6363
subRule(EnrichedClassTask::enrichedMethods, ::OverLoadRule)
6464
subRule(EnrichedClassTask::enrichedStaticMethods, ::OverLoadRule)
6565
rule(::BindingRule)
66-
rule(::CoreTypeHelperRule)
66+
rule(::LocalCopyHelperRule)
6767
}
6868
subRule(FileTask::enums, ::EnumRule)
6969
rule(::StaticRule)

kt/godot-library/godot-api-library/src/main/kotlin/godot/api/AStarGrid2D.kt

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public open class AStarGrid2D : RefCounted() {
7979
/**
8080
* The region of grid cells available for pathfinding. If changed, [update] needs to be called
8181
* before finding the next path.
82+
*
83+
* **Warning:**
84+
* Be careful when trying to modify a local
85+
* [copy](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types) obtained from this
86+
* getter.
87+
* Mutating it alone won't have any effect on the actual property, it has to be reassigned again
88+
* afterward.
8289
*/
8390
@CoreTypeLocalCopy
8491
public final inline var region: Rect2i
@@ -92,6 +99,13 @@ public open class AStarGrid2D : RefCounted() {
9299
/**
93100
* The size of the grid (number of cells of size [cellSize] on each axis). If changed, [update]
94101
* needs to be called before finding the next path.
102+
*
103+
* **Warning:**
104+
* Be careful when trying to modify a local
105+
* [copy](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types) obtained from this
106+
* getter.
107+
* Mutating it alone won't have any effect on the actual property, it has to be reassigned again
108+
* afterward.
95109
*/
96110
@CoreTypeLocalCopy
97111
public final inline var size: Vector2i
@@ -105,6 +119,13 @@ public open class AStarGrid2D : RefCounted() {
105119
/**
106120
* The offset of the grid which will be applied to calculate the resulting point position returned
107121
* by [getPointPath]. If changed, [update] needs to be called before finding the next path.
122+
*
123+
* **Warning:**
124+
* Be careful when trying to modify a local
125+
* [copy](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types) obtained from this
126+
* getter.
127+
* Mutating it alone won't have any effect on the actual property, it has to be reassigned again
128+
* afterward.
108129
*/
109130
@CoreTypeLocalCopy
110131
public final inline var offset: Vector2
@@ -118,6 +139,13 @@ public open class AStarGrid2D : RefCounted() {
118139
/**
119140
* The size of the point cell which will be applied to calculate the resulting point position
120141
* returned by [getPointPath]. If changed, [update] needs to be called before finding the next path.
142+
*
143+
* **Warning:**
144+
* Be careful when trying to modify a local
145+
* [copy](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types) obtained from this
146+
* getter.
147+
* Mutating it alone won't have any effect on the actual property, it has to be reassigned again
148+
* afterward.
121149
*/
122150
@CoreTypeLocalCopy
123151
public final inline var cellSize: Vector2
@@ -196,14 +224,7 @@ public open class AStarGrid2D : RefCounted() {
196224
}
197225

198226
/**
199-
* The region of grid cells available for pathfinding. If changed, [update] needs to be called
200-
* before finding the next path.
201-
*
202-
* This is a helper function to make dealing with local copies easier.
203-
*
204-
* For more information, see our
205-
* [documentation](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types).
206-
*
227+
* This is a helper function for [region] to make dealing with local copies easier.
207228
* Allow to directly modify the local copy of the property and assign it back to the Object.
208229
*
209230
* Prefer that over writing:
@@ -212,23 +233,18 @@ public open class AStarGrid2D : RefCounted() {
212233
* //Your changes
213234
* astargrid2d.region = myCoreType
214235
* ``````
236+
*
237+
* The region of grid cells available for pathfinding. If changed, [update] needs to be called
238+
* before finding the next path.
215239
*/
216240
@CoreTypeHelper
217-
public final fun regionMutate(block: Rect2i.() -> Unit): Rect2i = region.apply{
218-
block(this)
219-
region = this
241+
public final fun regionMutate(block: Rect2i.() -> Unit): Rect2i = region.apply {
242+
block(this)
243+
region = this
220244
}
221245

222-
223246
/**
224-
* The size of the grid (number of cells of size [cellSize] on each axis). If changed, [update]
225-
* needs to be called before finding the next path.
226-
*
227-
* This is a helper function to make dealing with local copies easier.
228-
*
229-
* For more information, see our
230-
* [documentation](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types).
231-
*
247+
* This is a helper function for [size] to make dealing with local copies easier.
232248
* Allow to directly modify the local copy of the property and assign it back to the Object.
233249
*
234250
* Prefer that over writing:
@@ -237,23 +253,18 @@ public open class AStarGrid2D : RefCounted() {
237253
* //Your changes
238254
* astargrid2d.size = myCoreType
239255
* ``````
256+
*
257+
* The size of the grid (number of cells of size [cellSize] on each axis). If changed, [update]
258+
* needs to be called before finding the next path.
240259
*/
241260
@CoreTypeHelper
242-
public final fun sizeMutate(block: Vector2i.() -> Unit): Vector2i = size.apply{
243-
block(this)
244-
size = this
261+
public final fun sizeMutate(block: Vector2i.() -> Unit): Vector2i = size.apply {
262+
block(this)
263+
size = this
245264
}
246265

247-
248266
/**
249-
* The offset of the grid which will be applied to calculate the resulting point position returned
250-
* by [getPointPath]. If changed, [update] needs to be called before finding the next path.
251-
*
252-
* This is a helper function to make dealing with local copies easier.
253-
*
254-
* For more information, see our
255-
* [documentation](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types).
256-
*
267+
* This is a helper function for [offset] to make dealing with local copies easier.
257268
* Allow to directly modify the local copy of the property and assign it back to the Object.
258269
*
259270
* Prefer that over writing:
@@ -262,23 +273,18 @@ public open class AStarGrid2D : RefCounted() {
262273
* //Your changes
263274
* astargrid2d.offset = myCoreType
264275
* ``````
276+
*
277+
* The offset of the grid which will be applied to calculate the resulting point position returned
278+
* by [getPointPath]. If changed, [update] needs to be called before finding the next path.
265279
*/
266280
@CoreTypeHelper
267-
public final fun offsetMutate(block: Vector2.() -> Unit): Vector2 = offset.apply{
268-
block(this)
269-
offset = this
281+
public final fun offsetMutate(block: Vector2.() -> Unit): Vector2 = offset.apply {
282+
block(this)
283+
offset = this
270284
}
271285

272-
273286
/**
274-
* The size of the point cell which will be applied to calculate the resulting point position
275-
* returned by [getPointPath]. If changed, [update] needs to be called before finding the next path.
276-
*
277-
* This is a helper function to make dealing with local copies easier.
278-
*
279-
* For more information, see our
280-
* [documentation](https://godot-kotl.in/en/stable/user-guide/api-differences/#core-types).
281-
*
287+
* This is a helper function for [cellSize] to make dealing with local copies easier.
282288
* Allow to directly modify the local copy of the property and assign it back to the Object.
283289
*
284290
* Prefer that over writing:
@@ -287,14 +293,16 @@ public open class AStarGrid2D : RefCounted() {
287293
* //Your changes
288294
* astargrid2d.cellSize = myCoreType
289295
* ``````
296+
*
297+
* The size of the point cell which will be applied to calculate the resulting point position
298+
* returned by [getPointPath]. If changed, [update] needs to be called before finding the next path.
290299
*/
291300
@CoreTypeHelper
292-
public final fun cellSizeMutate(block: Vector2.() -> Unit): Vector2 = cellSize.apply{
293-
block(this)
294-
cellSize = this
301+
public final fun cellSizeMutate(block: Vector2.() -> Unit): Vector2 = cellSize.apply {
302+
block(this)
303+
cellSize = this
295304
}
296305

297-
298306
/**
299307
* Called when estimating the cost between a point and the path's ending point.
300308
*

0 commit comments

Comments
 (0)