New J2K: Print & filter primary constructor modifiers

This commit is contained in:
Ilya Kirillov
2018-11-08 19:46:22 +03:00
committed by Ilya Kirillov
parent 7e7d865054
commit 0873045718
2 changed files with 27 additions and 7 deletions
@@ -194,11 +194,10 @@ class NewCodeBuilder {
builder.append(" ") builder.append(" ")
printer.printWithNoIndent(klass.name.value) printer.printWithNoIndent(klass.name.value)
klass.typeParameterList.accept(this) klass.typeParameterList.accept(this)
printer.printWithNoIndent(" ")
val primaryConstructor = klass.primaryConstructor() val primaryConstructor = klass.primaryConstructor()
if (primaryConstructor != null && primaryConstructor.parameters.isNotEmpty()) { primaryConstructor?.accept(this)
renderParameterList(primaryConstructor.parameters)
}
if (klass.inheritance.inherit.isNotEmpty()) { if (klass.inheritance.inherit.isNotEmpty()) {
printer.printWithNoIndent(" : ") printer.printWithNoIndent(" : ")
@@ -217,10 +216,11 @@ class NewCodeBuilder {
//TODO should it be here? //TODO should it be here?
renderExtraTypeParametersUpperBounds(klass.typeParameterList) renderExtraTypeParametersUpperBounds(klass.typeParameterList)
if (klass.declarationList.any { it !is JKKtPrimaryConstructor }) { val declarationsToPrint = klass.declarationList.filterNot { it is JKKtPrimaryConstructor}
if (declarationsToPrint.isNotEmpty()) {
printer.block(multiline = true) { printer.block(multiline = true) {
renderEnumConstants(klass.declarationList.filterIsInstance()) renderEnumConstants(declarationsToPrint.filterIsInstance())
renderNonEnumClassDeclarations(klass.declarationList.filterNot { it is JKEnumConstant }) renderNonEnumClassDeclarations(declarationsToPrint.filterNot { it is JKEnumConstant })
} }
} else { } else {
printer.println() printer.println()
@@ -610,7 +610,19 @@ class NewCodeBuilder {
} }
} }
override fun visitKtPrimaryConstructor(ktPrimaryConstructor: JKKtPrimaryConstructor) {} override fun visitKtPrimaryConstructor(ktPrimaryConstructor: JKKtPrimaryConstructor) {
val hasInitDeclaration =
(ktPrimaryConstructor.parent as JKClass).declarationList.any { it is JKKtInitDeclaration }
val hasAccessModifier =
ktPrimaryConstructor.modifierList.modifiers.any { it is JKAccessModifier }
if (hasAccessModifier && hasInitDeclaration && ktPrimaryConstructor.parameters.isNotEmpty()) {
ktPrimaryConstructor.modifierList.accept(this)
printer.printWithNoIndent(" constructor ")
}
if (ktPrimaryConstructor.parameters.isNotEmpty()) {
renderParameterList(ktPrimaryConstructor.parameters)
}
}
private inline fun Printer.indented(block: () -> Unit) { private inline fun Printer.indented(block: () -> Unit) {
this.pushIndent() this.pushIndent()
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.j2k.conversions
import org.jetbrains.kotlin.j2k.ConversionContext import org.jetbrains.kotlin.j2k.ConversionContext
import org.jetbrains.kotlin.j2k.ast.Mutability import org.jetbrains.kotlin.j2k.ast.Mutability
import org.jetbrains.kotlin.j2k.tree.* import org.jetbrains.kotlin.j2k.tree.*
import org.jetbrains.kotlin.j2k.tree.impl.JKKtConstructorImpl
import org.jetbrains.kotlin.j2k.tree.impl.JKKtModifierImpl import org.jetbrains.kotlin.j2k.tree.impl.JKKtModifierImpl
import org.jetbrains.kotlin.j2k.tree.impl.mutability import org.jetbrains.kotlin.j2k.tree.impl.mutability
import org.jetbrains.kotlin.j2k.tree.impl.visibility import org.jetbrains.kotlin.j2k.tree.impl.visibility
@@ -29,11 +30,18 @@ class ModifiersConversion(private val context: ConversionContext) : RecursiveApp
if (element is JKParameter) { if (element is JKParameter) {
convertParameterModifiers(element) convertParameterModifiers(element)
} }
if (element is JKKtConstructor) {
convertConstructorModifiers(element)
}
element.sortModifiers() element.sortModifiers()
} }
return recurse(element) return recurse(element)
} }
private fun convertConstructorModifiers(constructor: JKKtConstructor) {
constructor.filterModifiers { it !is JKModalityModifier }
}
private fun convertParameterModifiers(jkParameter: JKParameter) { private fun convertParameterModifiers(jkParameter: JKParameter) {
if (jkParameter.modifierList.mutability == Mutability.Default) { if (jkParameter.modifierList.mutability == Mutability.Default) {
jkParameter.modifierList.modifiers = emptyList() jkParameter.modifierList.modifiers = emptyList()