Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public boolean enterPythonClass(@NotNull AnnotatedPythonClass pythonClass) {
new ArrayList<>(pythonClass.getDecorators()),
new ArrayList<>(pythonClass.getSuperclasses()),
new ArrayList<>(),
new ArrayList<>(),
pythonClass.getDescription(),
pythonClass.getFullDocstring(),
annotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ class RenameAnnotationProcessor : AbstractPackageDataTransformer() {
return super.createNewClassOnLeave(oldClass, newAttributes, newMethods)
}

override fun createNewAttribute(oldAttribute: AnnotatedPythonAttribute): AnnotatedPythonAttribute {
return oldAttribute.rename { newName ->
oldAttribute.fullCopy(
name = newName,
qualifiedName = qualifiedName(newName),
originalDeclaration = oldAttribute.originalDeclaration ?: oldAttribute
)
}
}

override fun createNewFunctionOnEnter(oldFunction: AnnotatedPythonFunction): AnnotatedPythonFunction {
val result = oldFunction.rename { newName ->
oldFunction.fullCopy(
Expand Down Expand Up @@ -88,10 +98,11 @@ class RenameAnnotationProcessor : AbstractPackageDataTransformer() {

private fun <T : AnnotatedPythonDeclaration> T.rename(creator: (String) -> T): T {
val renameAnnotations = this.annotations.filterIsInstance<RenameAnnotation>()
return when {
renameAnnotations.isEmpty() -> this
else -> creator(renameAnnotations[0].newName)
val newName = when {
renameAnnotations.isEmpty() -> this.name
else -> renameAnnotations[0].newName
}
return creator(newName)
}

private fun qualifiedName(vararg additionalSegments: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.larsreimann.api_editor.util

import com.larsreimann.api_editor.model.AnnotatedPythonAttribute
import com.larsreimann.api_editor.model.AnnotatedPythonClass
import com.larsreimann.api_editor.model.AnnotatedPythonFunction
import com.larsreimann.api_editor.model.AnnotatedPythonModule
Expand Down Expand Up @@ -55,6 +56,7 @@ fun createPythonClass(
qualifiedName: String = name,
decorators: List<String> = emptyList(),
superclasses: List<String> = emptyList(),
attributes: List<AnnotatedPythonAttribute> = mutableListOf(),
methods: List<AnnotatedPythonFunction> = mutableListOf(),
description: String = "",
fullDocstring: String = "",
Expand All @@ -71,10 +73,35 @@ fun createPythonClass(
fullDocstring,
annotations
)
newPythonClass.attributes += attributes
newPythonClass.originalDeclaration = originalDeclaration
return newPythonClass
}

@JvmOverloads
fun createPythonAttribute(
name: String,
qualifiedName: String = name,
defaultValue: String = "",
isPublic: Boolean = true,
typeInDocs: String = "",
description: String = "",
annotations: MutableList<EditorAnnotation> = mutableListOf(),
originalDeclaration: AnnotatedPythonAttribute? = null,
): AnnotatedPythonAttribute {
val result = AnnotatedPythonAttribute(
name,
qualifiedName,
defaultValue,
isPublic,
typeInDocs,
description,
annotations
)
result.originalDeclaration = originalDeclaration
return result
}

@JvmOverloads
fun createPythonFunction(
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.larsreimann.api_editor.model.AnnotatedPythonFunction
import com.larsreimann.api_editor.model.AnnotatedPythonPackage
import com.larsreimann.api_editor.model.AnnotatedPythonParameter
import com.larsreimann.api_editor.model.RenameAnnotation
import com.larsreimann.api_editor.util.createPythonAttribute
import com.larsreimann.api_editor.util.createPythonClass
import com.larsreimann.api_editor.util.createPythonFunction
import com.larsreimann.api_editor.util.createPythonModule
Expand Down Expand Up @@ -33,6 +34,12 @@ internal class RenameAnnotationProcessorTest {
createPythonClass(
name = "testClass",
qualifiedName = "testModule.testClass",
attributes = listOf(
createPythonAttribute(
name = "testAttribute",
qualifiedName = "testModule.testClass.testAttribute"
)
),
methods = listOf(
createPythonFunction(
name = "testMethod",
Expand All @@ -54,7 +61,7 @@ internal class RenameAnnotationProcessorTest {
parameters = listOf(
createPythonParameter(
name = "testParameter",
qualifiedName = "testModule.testClass.testMethod.testParameter",
qualifiedName = "testModule.testFunction.testParameter",
)
)
)
Expand Down Expand Up @@ -83,6 +90,26 @@ internal class RenameAnnotationProcessorTest {
qualifiedName shouldBe "testModule.renamedTestClass"
}

@Test
fun `should rename method`() {
// given
val testMethod = testPackage.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testMethod")
testMethod.annotations += RenameAnnotation("renamedTestMethod")

// when
val modifiedPackage = testPackage.accept(RenameAnnotationProcessor())

// then
modifiedPackage.shouldNotBeNull()
modifiedPackage.modules.shouldHaveSize(1)
modifiedPackage.modules[0].classes.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods.shouldHaveSize(1)

val (name, qualifiedName) = modifiedPackage.modules[0].classes[0].methods[0]
name shouldBe "renamedTestMethod"
qualifiedName shouldBe "testModule.testClass.renamedTestMethod"
}

@Test
fun `should rename global function`() {
// given
Expand All @@ -103,10 +130,11 @@ internal class RenameAnnotationProcessorTest {
}

@Test
fun `should rename method`() {
fun `should rename parameter`() {
// given
val testMethod = testPackage.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testMethod")
testMethod.annotations += RenameAnnotation("renamedTestMethod")
val testParameter = testMethod.findUniqueDescendantOrFail<AnnotatedPythonParameter>("testParameter")
testParameter.annotations += RenameAnnotation("renamedTestParameter")

// when
val modifiedPackage = testPackage.accept(RenameAnnotationProcessor())
Expand All @@ -116,51 +144,53 @@ internal class RenameAnnotationProcessorTest {
modifiedPackage.modules.shouldHaveSize(1)
modifiedPackage.modules[0].classes.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods[0].parameters.shouldHaveSize(1)

val (name, qualifiedName) = modifiedPackage.modules[0].classes[0].methods[0]
name shouldBe "renamedTestMethod"
qualifiedName shouldBe "testModule.testClass.renamedTestMethod"
val (parameterName, parameterQualifiedName) = modifiedPackage.modules[0].classes[0].methods[0].parameters[0]
parameterName shouldBe "renamedTestParameter"
parameterQualifiedName shouldBe "testModule.testClass.testMethod.renamedTestParameter"
}

@Test
fun `should rename class and its method`() {
fun `should change qualified names of descendants when renaming class`() {
// given
val testClass = testPackage.findUniqueDescendantOrFail<AnnotatedPythonClass>("testClass")
testClass.annotations += RenameAnnotation("renamedTestClass")

val testMethod = testClass.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testMethod")
testMethod.annotations += RenameAnnotation("renamedTestMethod")

// when
val modifiedPackage = testPackage.accept(RenameAnnotationProcessor())

// then
modifiedPackage.shouldNotBeNull()
modifiedPackage.modules.shouldHaveSize(1)
modifiedPackage.modules[0].classes.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].attributes.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods[0].parameters.shouldHaveSize(1)

val (className, classQualifiedName) = modifiedPackage.modules[0].classes[0]
className shouldBe "renamedTestClass"
classQualifiedName shouldBe "testModule.renamedTestClass"

val (attributeName, attributeQualifiedName) = modifiedPackage.modules[0].classes[0].attributes[0]
attributeName shouldBe "testAttribute"
attributeQualifiedName shouldBe "testModule.renamedTestClass.testAttribute"

val (methodName, methodQualifiedName) = modifiedPackage.modules[0].classes[0].methods[0]
methodName shouldBe "renamedTestMethod"
methodQualifiedName shouldBe "testModule.renamedTestClass.renamedTestMethod"
methodName shouldBe "testMethod"
methodQualifiedName shouldBe "testModule.renamedTestClass.testMethod"

val (parameterName, parameterQualifiedName) = modifiedPackage.modules[0].classes[0].methods[0].parameters[0]
parameterName shouldBe "testParameter"
parameterQualifiedName shouldBe "testModule.renamedTestClass.testMethod.testParameter"
}

@Test
fun `should rename class, method, and parameter`() {
fun `should change qualified names of descendants when renaming method`() {
// given
val testClass = testPackage.findUniqueDescendantOrFail<AnnotatedPythonClass>("testClass")
testClass.annotations += RenameAnnotation("renamedTestClass")

val testMethod = testClass.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testMethod")
val testMethod = testPackage.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testMethod")
testMethod.annotations += RenameAnnotation("renamedTestMethod")

val testParameter = testMethod.findUniqueDescendantOrFail<AnnotatedPythonParameter>("testParameter")
testParameter.annotations += RenameAnnotation("renamedTestParameter")

// when
val modifiedPackage = testPackage.accept(RenameAnnotationProcessor())

Expand All @@ -171,28 +201,21 @@ internal class RenameAnnotationProcessorTest {
modifiedPackage.modules[0].classes[0].methods.shouldHaveSize(1)
modifiedPackage.modules[0].classes[0].methods[0].parameters.shouldHaveSize(1)

val (className, classQualifiedName) = modifiedPackage.modules[0].classes[0]
className shouldBe "renamedTestClass"
classQualifiedName shouldBe "testModule.renamedTestClass"

val (methodName, methodQualifiedName) = modifiedPackage.modules[0].classes[0].methods[0]
methodName shouldBe "renamedTestMethod"
methodQualifiedName shouldBe "testModule.renamedTestClass.renamedTestMethod"
methodQualifiedName shouldBe "testModule.testClass.renamedTestMethod"

val (parameterName, parameterQualifiedName) = modifiedPackage.modules[0].classes[0].methods[0].parameters[0]
parameterName shouldBe "renamedTestParameter"
parameterQualifiedName shouldBe "testModule.renamedTestClass.renamedTestMethod.renamedTestParameter"
parameterName shouldBe "testParameter"
parameterQualifiedName shouldBe "testModule.testClass.renamedTestMethod.testParameter"
}

@Test
fun `should rename global function and parameter`() {
fun `should change qualified names of descendants when renaming global function`() {
// given
val testFunction = testPackage.findUniqueDescendantOrFail<AnnotatedPythonFunction>("testFunction")
testFunction.annotations += RenameAnnotation("renamedTestFunction")

val testParameter = testFunction.findUniqueDescendantOrFail<AnnotatedPythonParameter>("testParameter")
testParameter.annotations += RenameAnnotation("renamedTestParameter")

// when
val modifiedPackage = testPackage.accept(RenameAnnotationProcessor())

Expand All @@ -207,8 +230,8 @@ internal class RenameAnnotationProcessorTest {
functionQualifiedName shouldBe "testModule.renamedTestFunction"

val (parameterName, parameterQualifiedName) = modifiedPackage.modules[0].functions[0].parameters[0]
parameterName shouldBe "renamedTestParameter"
parameterQualifiedName shouldBe "testModule.renamedTestFunction.renamedTestParameter"
parameterName shouldBe "testParameter"
parameterQualifiedName shouldBe "testModule.renamedTestFunction.testParameter"
}

@Test
Expand Down