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