Reformat: "Generate member" actions

This commit is contained in:
Mikhail Glukhikh
2018-11-08 12:36:00 +03:00
parent cb28387da4
commit b15b993b26
@@ -55,11 +55,13 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
var KtClass.adjuster: ((Info) -> Info)? by UserDataProperty(Key.create("ADJUSTER"))
}
data class Info(val classDescriptor: ClassDescriptor,
val variablesToUse: List<VariableDescriptor>,
val generateSuperCall: Boolean,
val generator: Generator,
val project: Project)
data class Info(
val classDescriptor: ClassDescriptor,
val variablesToUse: List<VariableDescriptor>,
val generateSuperCall: Boolean,
val generator: Generator,
val project: Project
)
enum class Generator(val text: String) {
SINGLE_TEMPLATE("Single template") {
@@ -69,7 +71,8 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
return buildString {
append("return \"${className.quoteIfNeeded()}(")
info.variablesToUse.joinTo(this) {
val ref = (DescriptorToSourceUtilsIde.getAnyDeclaration(info.project, it) as PsiNameIdentifierOwner).nameIdentifier!!.text
val ref =
(DescriptorToSourceUtilsIde.getAnyDeclaration(info.project, it) as PsiNameIdentifierOwner).nameIdentifier!!.text
"$ref=${renderVariableValue(it, ref)}"
}
append(")")
@@ -91,7 +94,8 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
val varIterator = info.variablesToUse.iterator()
while (varIterator.hasNext()) {
val it = varIterator.next()
val ref = (DescriptorToSourceUtilsIde.getAnyDeclaration(info.project, it) as PsiNameIdentifierOwner).nameIdentifier!!.text
val ref = (DescriptorToSourceUtilsIde.getAnyDeclaration(info.project, it) as PsiNameIdentifierOwner)
.nameIdentifier!!.text
append("\"$ref=${renderVariableValue(it, ref)}")
if (varIterator.hasNext()) {
append(", ")
@@ -99,8 +103,7 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
append("\" +\n")
}
append("\")\"")
}
else {
} else {
append("return \"$className()\"")
}
@@ -123,11 +126,8 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
abstract fun generate(info: Info): String
}
override fun isValidForClass(targetClass: KtClassOrObject): Boolean {
return targetClass is KtClass
&& !targetClass.isAnnotation()
&& !targetClass.isInterface()
}
override fun isValidForClass(targetClass: KtClassOrObject): Boolean =
targetClass is KtClass && !targetClass.isAnnotation() && !targetClass.isInterface()
override fun prepareMembersInfo(klass: KtClassOrObject, project: Project, editor: Editor?): Info? {
if (klass !is KtClass) throw AssertionError("Not a class: ${klass.getElementTextWithContext()}")
@@ -141,8 +141,7 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
runWriteAction {
try {
it.source.getPsi()?.delete()
}
catch(e: IncorrectOperationException) {
} catch (e: IncorrectOperationException) {
LOG.error(e)
}
}
@@ -150,11 +149,13 @@ class KotlinGenerateToStringAction : KotlinGenerateMemberActionBase<KotlinGenera
val properties = getPropertiesToUseInGeneratedMember(klass)
if (ApplicationManager.getApplication().isUnitTestMode) {
val info = Info(classDescriptor,
properties.map { context[BindingContext.DECLARATION_TO_DESCRIPTOR, it] as VariableDescriptor },
false,
Generator.SINGLE_TEMPLATE,
project)
val info = Info(
classDescriptor,
properties.map { context[BindingContext.DECLARATION_TO_DESCRIPTOR, it] as VariableDescriptor },
false,
Generator.SINGLE_TEMPLATE,
project
)
return klass.adjuster?.let { it(info) } ?: info
}