comments processing finished
This commit is contained in:
committed by
Pavel V. Talanov
parent
4bb3731272
commit
5664283a30
@@ -93,21 +93,32 @@ public open class Converter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public open fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
public open fun anonymousClassToAnonymousClass(anonymousClass: PsiAnonymousClass): AnonymousClass {
|
||||||
return AnonymousClass(this, getMembers(anonymousClass, false))
|
return AnonymousClass(this, getMembers(anonymousClass))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMembers(psiClass: PsiClass, takeDocComments: Boolean): List<Node> {
|
private fun getMembers(psiClass: PsiClass): List<Node> {
|
||||||
val members: List<Node> = arrayList()
|
val members: List<Node> = arrayList()
|
||||||
val lbraceOffset = psiClass.getLBrace()?.getTextRange()?.getStartOffset() ?: 0
|
val lbraceOffset = psiClass.getLBrace()?.getTextRange()?.getStartOffset() ?: 0
|
||||||
for (e : PsiElement? in psiClass.getChildren()) {
|
for (e : PsiElement? in psiClass.getChildren()) {
|
||||||
val isDocComment = e?.getTextRange()?.getStartOffset() ?: 0 < lbraceOffset
|
val isDocComment = e?.getTextRange()?.getStartOffset() ?: 0 < lbraceOffset
|
||||||
if (isDocComment != takeDocComments) continue
|
if (isDocComment) continue
|
||||||
val converted = memberToMember(e, psiClass)
|
val converted = memberToMember(e, psiClass)
|
||||||
if (converted != null) members.add(converted)
|
if (converted != null) members.add(converted)
|
||||||
}
|
}
|
||||||
return members
|
return members
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getDocComments(element: PsiElement): List<Node> {
|
||||||
|
val comments: List<Node> = arrayList()
|
||||||
|
val textOffset = element.getTextOffset()
|
||||||
|
for (e : PsiElement? in element.getChildren()) {
|
||||||
|
if (e is PsiComment && e.getTextRange()?.getStartOffset() ?: 0 < textOffset) {
|
||||||
|
comments.add(Comment(e.getText()!!));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return comments
|
||||||
|
}
|
||||||
|
|
||||||
private fun memberToMember(e: PsiElement?, containingClass: PsiClass): Node? = when(e) {
|
private fun memberToMember(e: PsiElement?, containingClass: PsiClass): Node? = when(e) {
|
||||||
is PsiMethod -> methodToFunction(e, true)
|
is PsiMethod -> methodToFunction(e, true)
|
||||||
is PsiField -> fieldToField(e, containingClass)
|
is PsiField -> fieldToField(e, containingClass)
|
||||||
@@ -125,8 +136,8 @@ public open class Converter() {
|
|||||||
val extendsTypes: List<Type> = typesToNotNullableTypeList(psiClass.getExtendsListTypes())
|
val extendsTypes: List<Type> = typesToNotNullableTypeList(psiClass.getExtendsListTypes())
|
||||||
val name: Identifier = Identifier(psiClass.getName()!!)
|
val name: Identifier = Identifier(psiClass.getName()!!)
|
||||||
val baseClassParams: List<Expression> = arrayList()
|
val baseClassParams: List<Expression> = arrayList()
|
||||||
val members: List<Node> = getMembers(psiClass, false)
|
val members: List<Node> = getMembers(psiClass)
|
||||||
val docComments = getMembers(psiClass, true)
|
val docComments = getDocComments(psiClass)
|
||||||
val visitor: SuperVisitor = SuperVisitor()
|
val visitor: SuperVisitor = SuperVisitor()
|
||||||
psiClass.accept(visitor)
|
psiClass.accept(visitor)
|
||||||
val resolvedSuperCallParameters = visitor.resolvedSuperCallParameters
|
val resolvedSuperCallParameters = visitor.resolvedSuperCallParameters
|
||||||
@@ -146,8 +157,8 @@ public open class Converter() {
|
|||||||
val init: String = getDefaultInitializer(fo)
|
val init: String = getDefaultInitializer(fo)
|
||||||
initializers.put(fo.identifier.toKotlin(), init)
|
initializers.put(fo.identifier.toKotlin(), init)
|
||||||
}
|
}
|
||||||
val newStatements: List<Statement> = arrayList()
|
val newStatements: List<Element> = arrayList()
|
||||||
for (s : Statement? in m.block!!.statements) {
|
for (s in m.block!!.statements) {
|
||||||
var isRemoved: Boolean = false
|
var isRemoved: Boolean = false
|
||||||
if (s is AssignmentExpression) {
|
if (s is AssignmentExpression) {
|
||||||
val assignee = s.left
|
val assignee = s.left
|
||||||
@@ -165,7 +176,7 @@ public open class Converter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isRemoved) {
|
if (!isRemoved) {
|
||||||
newStatements.add(s!!)
|
newStatements.add(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -174,7 +185,7 @@ public open class Converter() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
members.add(Constructor(Identifier.EMPTY_IDENTIFIER, Collections.emptySet<String>()!!,
|
members.add(Constructor(Identifier.EMPTY_IDENTIFIER, arrayList(), Collections.emptySet<String>()!!,
|
||||||
ClassType(name, Collections.emptyList<Element>()!!, false),
|
ClassType(name, Collections.emptyList<Element>()!!, false),
|
||||||
Collections.emptyList<Element>()!!,
|
Collections.emptyList<Element>()!!,
|
||||||
ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
|
ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
|
||||||
@@ -203,14 +214,17 @@ public open class Converter() {
|
|||||||
|
|
||||||
private fun fieldToField(field: PsiField, psiClass: PsiClass?): Field {
|
private fun fieldToField(field: PsiField, psiClass: PsiClass?): Field {
|
||||||
val modifiers: Set<String> = modifiersListToModifiersSet(field.getModifierList())
|
val modifiers: Set<String> = modifiersListToModifiersSet(field.getModifierList())
|
||||||
if (field is PsiEnumConstant?) {
|
val docComments = getDocComments(field)
|
||||||
|
if (field is PsiEnumConstant) {
|
||||||
return EnumConstant(Identifier(field.getName()!!),
|
return EnumConstant(Identifier(field.getName()!!),
|
||||||
|
docComments,
|
||||||
modifiers,
|
modifiers,
|
||||||
typeToType(field.getType()),
|
typeToType(field.getType()),
|
||||||
elementToElement(field.getArgumentList()))
|
elementToElement(field.getArgumentList()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return Field(Identifier(field.getName()!!),
|
return Field(Identifier(field.getName()!!),
|
||||||
|
docComments,
|
||||||
modifiers,
|
modifiers,
|
||||||
typeToType(field.getType()),
|
typeToType(field.getType()),
|
||||||
expressionToExpression(field.getInitializer(), field.getType()),
|
expressionToExpression(field.getInitializer(), field.getType()),
|
||||||
@@ -237,8 +251,9 @@ public open class Converter() {
|
|||||||
blockToBlock(method.getBody(), notEmpty))
|
blockToBlock(method.getBody(), notEmpty))
|
||||||
|
|
||||||
val params: Element = createFunctionParameters(method)
|
val params: Element = createFunctionParameters(method)
|
||||||
val typeParameters: List<Element> = elementsToElementList(method.getTypeParameters())
|
val typeParameters = elementsToElementList(method.getTypeParameters())
|
||||||
val modifiers: Set<String> = modifiersListToModifiersSet(method.getModifierList())
|
val modifiers = modifiersListToModifiersSet(method.getModifierList())
|
||||||
|
val docComments = getDocComments(method)
|
||||||
if (isOverrideAnyMethodExceptMethodsFromObject(method)) {
|
if (isOverrideAnyMethodExceptMethodsFromObject(method)) {
|
||||||
modifiers.add(Modifier.OVERRIDE)
|
modifiers.add(Modifier.OVERRIDE)
|
||||||
}
|
}
|
||||||
@@ -254,10 +269,11 @@ public open class Converter() {
|
|||||||
|
|
||||||
if (method.isConstructor()) {
|
if (method.isConstructor()) {
|
||||||
val isPrimary: Boolean = isConstructorPrimary(method)
|
val isPrimary: Boolean = isConstructorPrimary(method)
|
||||||
return Constructor(identifier, modifiers, returnType, typeParameters, params, Block(removeEmpty(body.statements), false), isPrimary)
|
return Constructor(identifier, docComments, modifiers, returnType, typeParameters, params,
|
||||||
|
Block(removeEmpty(body.statements), false), isPrimary)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Function(identifier, modifiers, returnType, typeParameters, params, body)
|
return Function(identifier, docComments, modifiers, returnType, typeParameters, params, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createFunctionParameters(method: PsiMethod): ParameterList {
|
private fun createFunctionParameters(method: PsiMethod): ParameterList {
|
||||||
@@ -303,22 +319,22 @@ public open class Converter() {
|
|||||||
if (block == null)
|
if (block == null)
|
||||||
return Block.EMPTY_BLOCK
|
return Block.EMPTY_BLOCK
|
||||||
|
|
||||||
return Block(statementsToStatementList(block.getStatements()), notEmpty)
|
return Block(block.getChildren().map { statementToStatement(it) }, notEmpty)
|
||||||
}
|
}
|
||||||
|
|
||||||
public open fun blockToBlock(block: PsiCodeBlock?): Block {
|
public open fun blockToBlock(block: PsiCodeBlock?): Block {
|
||||||
return blockToBlock(block, true)
|
return blockToBlock(block, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
public open fun statementsToStatementList(statements: Array<PsiStatement?>): List<Statement> {
|
public open fun statementsToStatementList(statements: Array<PsiElement?>): List<Element> {
|
||||||
return statements.map { statementToStatement(it) }
|
return statements.map { statementToStatement(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
public open fun statementsToStatementList(statements: List<PsiStatement?>): List<Statement> {
|
public open fun statementsToStatementList(statements: List<PsiElement?>): List<Element> {
|
||||||
return statements.map { statementToStatement(it) }
|
return statements.map { statementToStatement(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
public open fun statementToStatement(s: PsiStatement?): Statement {
|
public open fun statementToStatement(s: PsiElement?): Element {
|
||||||
if (s == null)
|
if (s == null)
|
||||||
return Statement.EMPTY_STATEMENT
|
return Statement.EMPTY_STATEMENT
|
||||||
|
|
||||||
@@ -487,8 +503,8 @@ public open class Converter() {
|
|||||||
return fields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, true) }
|
return fields.map { Parameter(Identifier("_" + it.identifier.name), it.`type`, true) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createInitStatementsFromFields(fields: List<out Field>): List<Statement> {
|
private fun createInitStatementsFromFields(fields: List<out Field>): List<Element> {
|
||||||
val result: List<Statement> = arrayList()
|
val result: List<Element> = arrayList()
|
||||||
for (f : Field in fields) {
|
for (f : Field in fields) {
|
||||||
val identifierToKotlin: String? = f.identifier.toKotlin()
|
val identifierToKotlin: String? = f.identifier.toKotlin()
|
||||||
result.add(DummyStringExpression(identifierToKotlin + " = " + "_" + identifierToKotlin))
|
result.add(DummyStringExpression(identifierToKotlin + " = " + "_" + identifierToKotlin))
|
||||||
@@ -545,8 +561,10 @@ public open class Converter() {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
private fun removeEmpty(statements: List<Statement>): List<Statement> {
|
private fun removeEmpty(statements: List<Element>): List<Element> {
|
||||||
return statements.filterNot { it == Statement.EMPTY_STATEMENT || it == Expression.EMPTY_EXPRESSION }
|
return statements.filterNot { it == Statement.EMPTY_STATEMENT ||
|
||||||
|
it == Expression.EMPTY_EXPRESSION ||
|
||||||
|
it == Element.EMPTY_ELEMENT }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isNotOpenMethod(method: PsiMethod): Boolean {
|
private fun isNotOpenMethod(method: PsiMethod): Boolean {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
import java.util.LinkedList
|
import java.util.LinkedList
|
||||||
import java.util.List
|
import java.util.List
|
||||||
|
|
||||||
public open class Block(val statements: List<Statement>, val notEmpty: Boolean = false): Statement() {
|
public open class Block(val statements: List<Element>, val notEmpty: Boolean = false): Statement() {
|
||||||
public override fun isEmpty(): Boolean {
|
public override fun isEmpty(): Boolean {
|
||||||
return !notEmpty && statements.size() == 0
|
return !notEmpty && statements.size() == 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,13 +62,13 @@ public open class Class(converter : Converter,
|
|||||||
private fun constructorToInit(f: Function): Function {
|
private fun constructorToInit(f: Function): Function {
|
||||||
val modifiers : Set<String> = HashSet<String>(f.getModifiers())
|
val modifiers : Set<String> = HashSet<String>(f.getModifiers())
|
||||||
modifiers.add(Modifier.STATIC)
|
modifiers.add(Modifier.STATIC)
|
||||||
val statements : List<Statement> = f.block?.statements ?: arrayList()
|
val statements : List<Element> = f.block?.statements ?: arrayList()
|
||||||
statements.add(ReturnStatement(Identifier("__")))
|
statements.add(ReturnStatement(Identifier("__")))
|
||||||
val block : Block = Block(statements)
|
val block = Block(statements)
|
||||||
val constructorTypeParameters : List<Element> = arrayList()
|
val constructorTypeParameters : List<Element> = arrayList()
|
||||||
constructorTypeParameters.addAll(typeParameters)
|
constructorTypeParameters.addAll(typeParameters)
|
||||||
constructorTypeParameters.addAll(f.typeParameters)
|
constructorTypeParameters.addAll(f.typeParameters)
|
||||||
return Function(Identifier("init"), modifiers, ClassType(name, constructorTypeParameters, false),
|
return Function(Identifier("init"), arrayList(), modifiers, ClassType(name, constructorTypeParameters, false),
|
||||||
constructorTypeParameters, f.params, block)
|
constructorTypeParameters, f.params, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ public open class Class(converter : Converter,
|
|||||||
}
|
}
|
||||||
|
|
||||||
open fun implementTypesToKotlin() : String {
|
open fun implementTypesToKotlin() : String {
|
||||||
val allTypes : List<String?> = arrayList()
|
val allTypes : List<String> = arrayList()
|
||||||
allTypes.addAll(baseClassSignatureWithParams())
|
allTypes.addAll(baseClassSignatureWithParams())
|
||||||
allTypes.addAll(implementsTypes.map { it.toKotlin() })
|
allTypes.addAll(implementsTypes.map { it.toKotlin() })
|
||||||
return if (allTypes.size() == 0)
|
return if (allTypes.size() == 0)
|
||||||
|
|||||||
@@ -5,12 +5,13 @@ import java.util.Set
|
|||||||
import org.jetbrains.jet.j2k.ast.types.Type
|
import org.jetbrains.jet.j2k.ast.types.Type
|
||||||
|
|
||||||
public open class Constructor(identifier : Identifier,
|
public open class Constructor(identifier : Identifier,
|
||||||
|
docComments: List<Node>,
|
||||||
modifiers : Set<String>,
|
modifiers : Set<String>,
|
||||||
`type` : Type,
|
`type` : Type,
|
||||||
typeParameters : List<Element>,
|
typeParameters : List<Element>,
|
||||||
params : Element,
|
params : Element,
|
||||||
block : Block,
|
block : Block,
|
||||||
val isPrimary : Boolean) : Function(identifier, modifiers, `type`, typeParameters, params, block) {
|
val isPrimary : Boolean) : Function(identifier, docComments, modifiers, `type`, typeParameters, params, block) {
|
||||||
|
|
||||||
public open fun primarySignatureToKotlin() : String {
|
public open fun primarySignatureToKotlin() : String {
|
||||||
return "(" + params.toKotlin() + ")"
|
return "(" + params.toKotlin() + ")"
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package org.jetbrains.jet.j2k.ast
|
|||||||
|
|
||||||
import org.jetbrains.jet.j2k.ast.types.Type
|
import org.jetbrains.jet.j2k.ast.types.Type
|
||||||
import java.util.Set
|
import java.util.Set
|
||||||
|
import java.util.List
|
||||||
|
|
||||||
public open class EnumConstant(identifier : Identifier,
|
public open class EnumConstant(identifier : Identifier,
|
||||||
|
docComments: List<Node>,
|
||||||
modifiers : Set<String>,
|
modifiers : Set<String>,
|
||||||
`type` : Type,
|
`type` : Type,
|
||||||
params : Element) : Field(identifier, modifiers, `type`.convertedToNotNull(), params, 0) {
|
params : Element) : Field(identifier, docComments, modifiers, `type`.convertedToNotNull(), params, 0) {
|
||||||
|
|
||||||
public override fun toKotlin() : String {
|
public override fun toKotlin() : String {
|
||||||
if (initializer.toKotlin().isEmpty()) {
|
if (initializer.toKotlin().isEmpty()) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.Set
|
|||||||
import org.jetbrains.jet.j2k.Converter
|
import org.jetbrains.jet.j2k.Converter
|
||||||
|
|
||||||
public open class Field(val identifier : Identifier,
|
public open class Field(val identifier : Identifier,
|
||||||
|
val docComments: List<Node>,
|
||||||
modifiers : Set<String>,
|
modifiers : Set<String>,
|
||||||
val `type` : Type,
|
val `type` : Type,
|
||||||
val initializer : Element,
|
val initializer : Element,
|
||||||
@@ -37,7 +38,8 @@ public open class Field(val identifier : Identifier,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override fun toKotlin() : String {
|
public override fun toKotlin() : String {
|
||||||
val declaration : String? = modifiersToKotlin() + identifier.toKotlin() + " : " + `type`.toKotlin()
|
val declaration : String = docComments.toKotlin("\n", "", "\n") +
|
||||||
|
modifiersToKotlin() + identifier.toKotlin() + " : " + `type`.toKotlin()
|
||||||
if (initializer.isEmpty()) {
|
if (initializer.isEmpty()) {
|
||||||
return declaration + ((if (isVal() && !isStatic() && writingAccesses == 1)
|
return declaration + ((if (isVal() && !isStatic() && writingAccesses == 1)
|
||||||
""
|
""
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import java.util.Set
|
|||||||
import org.jetbrains.jet.j2k.ast.types.Type
|
import org.jetbrains.jet.j2k.ast.types.Type
|
||||||
|
|
||||||
public open class Function(val name : Identifier,
|
public open class Function(val name : Identifier,
|
||||||
|
val docComments: List<Node>,
|
||||||
modifiers : Set<String>,
|
modifiers : Set<String>,
|
||||||
val `type` : Type,
|
val `type` : Type,
|
||||||
val typeParameters : List<Element>,
|
val typeParameters : List<Element>,
|
||||||
@@ -33,18 +34,15 @@ public open class Function(val name : Identifier,
|
|||||||
open fun modifiersToKotlin() : String {
|
open fun modifiersToKotlin() : String {
|
||||||
val modifierList: List<String> = arrayList()
|
val modifierList: List<String> = arrayList()
|
||||||
val accessModifier : String = accessModifier()
|
val accessModifier : String = accessModifier()
|
||||||
if (!accessModifier.isEmpty())
|
if (!accessModifier.isEmpty()) {
|
||||||
{
|
|
||||||
modifierList.add(accessModifier)
|
modifierList.add(accessModifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isAbstract())
|
if (isAbstract()) {
|
||||||
{
|
|
||||||
modifierList.add(Modifier.ABSTRACT)
|
modifierList.add(Modifier.ABSTRACT)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myModifiers.contains(Modifier.OVERRIDE))
|
if (myModifiers.contains(Modifier.OVERRIDE)) {
|
||||||
{
|
|
||||||
modifierList.add(Modifier.OVERRIDE)
|
modifierList.add(Modifier.OVERRIDE)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,13 +54,11 @@ public open class Function(val name : Identifier,
|
|||||||
modifierList.add(Modifier.OPEN)
|
modifierList.add(Modifier.OPEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (myModifiers.contains(Modifier.NOT_OPEN))
|
if (myModifiers.contains(Modifier.NOT_OPEN)) {
|
||||||
{
|
|
||||||
modifierList.remove(Modifier.OPEN)
|
modifierList.remove(Modifier.OPEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (modifierList.size() > 0)
|
if (modifierList.size() > 0) {
|
||||||
{
|
|
||||||
return modifierList.makeString(" ") + " "
|
return modifierList.makeString(" ") + " "
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +66,12 @@ public open class Function(val name : Identifier,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override fun toKotlin() : String {
|
public override fun toKotlin() : String {
|
||||||
return modifiersToKotlin() + "fun " + name.toKotlin() + typeParametersToKotlin() + "(" + params.toKotlin() + ") : " + `type`.toKotlin() + " "+ typeParameterWhereToKotlin() + block?.toKotlin()
|
return docComments.toKotlin("\n", "", "\n") +
|
||||||
|
modifiersToKotlin() +
|
||||||
|
"fun " + name.toKotlin() +
|
||||||
|
typeParametersToKotlin() +
|
||||||
|
"(" + params.toKotlin() + ") : " +
|
||||||
|
`type`.toKotlin() + " "+ typeParameterWhereToKotlin() +
|
||||||
|
block?.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public open class ExpressionListStatement(val expressions: List<Expression>): Ex
|
|||||||
public override fun toKotlin() = expressions.toKotlin("\n")
|
public override fun toKotlin() = expressions.toKotlin("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class LabelStatement(val name: Identifier, val statement: Statement): Statement() {
|
public open class LabelStatement(val name: Identifier, val statement: Element): Statement() {
|
||||||
public override fun toKotlin(): String = "@" + name.toKotlin() + " " + statement.toKotlin()
|
public override fun toKotlin(): String = "@" + name.toKotlin() + " " + statement.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,9 @@ public open class ReturnStatement(val expression: Expression): Statement() {
|
|||||||
public override fun toKotlin() = "return " + expression.toKotlin()
|
public override fun toKotlin() = "return " + expression.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class IfStatement(val condition: Expression, val thenStatement: Statement, val elseStatement: Statement): Expression() {
|
public open class IfStatement(val condition: Expression,
|
||||||
|
val thenStatement: Element,
|
||||||
|
val elseStatement: Element): Expression() {
|
||||||
public override fun toKotlin(): String {
|
public override fun toKotlin(): String {
|
||||||
val result: String = "if (" + condition.toKotlin() + ")\n" + thenStatement.toKotlin() + "\n"
|
val result: String = "if (" + condition.toKotlin() + ")\n" + thenStatement.toKotlin() + "\n"
|
||||||
if (elseStatement != Statement.EMPTY_STATEMENT) {
|
if (elseStatement != Statement.EMPTY_STATEMENT) {
|
||||||
@@ -49,25 +51,25 @@ public open class IfStatement(val condition: Expression, val thenStatement: Stat
|
|||||||
|
|
||||||
// Loops --------------------------------------------------------------------------------------------------
|
// Loops --------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
public open class WhileStatement(val condition: Expression, val statement: Statement): Statement() {
|
public open class WhileStatement(val condition: Expression, val body: Element): Statement() {
|
||||||
public override fun toKotlin() = "while (" + condition.toKotlin() + ")\n" + statement.toKotlin()
|
public override fun toKotlin() = "while (" + condition.toKotlin() + ")\n" + body.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class DoWhileStatement(condition: Expression, statement: Statement): WhileStatement(condition, statement) {
|
public open class DoWhileStatement(condition: Expression, body: Element): WhileStatement(condition, body) {
|
||||||
public override fun toKotlin() = "do\n" + statement.toKotlin() + "\nwhile (" + condition.toKotlin() + ")"
|
public override fun toKotlin() = "do\n" + body.toKotlin() + "\nwhile (" + condition.toKotlin() + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class ForeachStatement(val variable: Parameter,
|
public open class ForeachStatement(val variable: Parameter,
|
||||||
val expression: Expression,
|
val expression: Expression,
|
||||||
val statement: Statement): Statement() {
|
val body: Element): Statement() {
|
||||||
public override fun toKotlin() = "for (" + variable.toKotlin() + " in " +
|
public override fun toKotlin() = "for (" + variable.toKotlin() + " in " +
|
||||||
expression.toKotlin() + ")\n" + statement.toKotlin()
|
expression.toKotlin() + ")\n" + body.toKotlin()
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class ForeachWithRangeStatement(val identifier: Identifier,
|
public open class ForeachWithRangeStatement(val identifier: Identifier,
|
||||||
val start: Expression,
|
val start: Expression,
|
||||||
val end: Expression,
|
val end: Expression,
|
||||||
val body: Statement): Statement() {
|
val body: Element): Statement() {
|
||||||
public override fun toKotlin() = "for (" + identifier.toKotlin() + " in " +
|
public override fun toKotlin() = "for (" + identifier.toKotlin() + " in " +
|
||||||
start.toKotlin() + ".." + end.toKotlin() + ") " + body.toKotlin()
|
start.toKotlin() + ".." + end.toKotlin() + ") " + body.toKotlin()
|
||||||
}
|
}
|
||||||
@@ -105,11 +107,11 @@ public open class SwitchContainer(val expression: Expression, val caseContainers
|
|||||||
public override fun toKotlin() = "when (" + expression.toKotlin() + ") {\n" + caseContainers.toKotlin("\n") + "\n}"
|
public override fun toKotlin() = "when (" + expression.toKotlin() + ") {\n" + caseContainers.toKotlin("\n") + "\n}"
|
||||||
}
|
}
|
||||||
|
|
||||||
public open class CaseContainer(val caseStatement: List<Statement>, statements: List<Statement>): Statement() {
|
public open class CaseContainer(val caseStatement: List<Element>, statements: List<Element>): Statement() {
|
||||||
private val myBlock: Block
|
private val myBlock: Block
|
||||||
|
|
||||||
{
|
{
|
||||||
val newStatements: List<Statement> = statements.filterNot { it is BreakStatement || it is ContinueStatement }
|
val newStatements = statements.filterNot { it is BreakStatement || it is ContinueStatement }
|
||||||
myBlock = Block(newStatements, false)
|
myBlock = Block(newStatements, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import java.util.List
|
|||||||
import org.jetbrains.jet.j2k.ConverterUtil.isAnnotatedAsNotNull
|
import org.jetbrains.jet.j2k.ConverterUtil.isAnnotatedAsNotNull
|
||||||
|
|
||||||
public open class ElementVisitor(val myConverter : Converter) : JavaElementVisitor() {
|
public open class ElementVisitor(val myConverter : Converter) : JavaElementVisitor() {
|
||||||
private var myResult : Element = Element.EMPTY_ELEMENT
|
protected var myResult : Element = Element.EMPTY_ELEMENT
|
||||||
|
|
||||||
public fun getConverter() : Converter {
|
public fun getConverter() : Converter {
|
||||||
return myConverter
|
return myConverter
|
||||||
|
|||||||
@@ -14,14 +14,12 @@ import java.util.List
|
|||||||
import com.intellij.psi.CommonClassNames.*
|
import com.intellij.psi.CommonClassNames.*
|
||||||
|
|
||||||
public open class ExpressionVisitor(converter: Converter): StatementVisitor(converter) {
|
public open class ExpressionVisitor(converter: Converter): StatementVisitor(converter) {
|
||||||
var myResult: Expression = Expression.EMPTY_EXPRESSION
|
{
|
||||||
|
|
||||||
public override fun visitExpression(expression: PsiExpression?): Unit {
|
|
||||||
myResult = Expression.EMPTY_EXPRESSION
|
myResult = Expression.EMPTY_EXPRESSION
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun getResult(): Expression {
|
public override fun getResult(): Expression {
|
||||||
return myResult
|
return myResult as Expression
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun visitArrayAccessExpression(expression: PsiArrayAccessExpression?): Unit {
|
public override fun visitArrayAccessExpression(expression: PsiArrayAccessExpression?): Unit {
|
||||||
|
|||||||
@@ -12,20 +12,13 @@ import java.util.List
|
|||||||
import org.jetbrains.jet.j2k.ConverterUtil.countWritingAccesses
|
import org.jetbrains.jet.j2k.ConverterUtil.countWritingAccesses
|
||||||
|
|
||||||
public open class StatementVisitor(converter: Converter): ElementVisitor(converter) {
|
public open class StatementVisitor(converter: Converter): ElementVisitor(converter) {
|
||||||
private var myResult: Statement = Statement.EMPTY_STATEMENT
|
|
||||||
|
|
||||||
public override fun getResult(): Statement {
|
|
||||||
return myResult
|
|
||||||
}
|
|
||||||
|
|
||||||
public override fun visitAssertStatement(statement: PsiAssertStatement?): Unit {
|
public override fun visitAssertStatement(statement: PsiAssertStatement?): Unit {
|
||||||
myResult = AssertStatement(getConverter().expressionToExpression(statement?.getAssertCondition()),
|
myResult = AssertStatement(getConverter().expressionToExpression(statement?.getAssertCondition()),
|
||||||
getConverter().expressionToExpression(statement?.getAssertDescription()))
|
getConverter().expressionToExpression(statement?.getAssertDescription()))
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun visitBlockStatement(statement: PsiBlockStatement?): Unit {
|
public override fun visitBlockStatement(statement: PsiBlockStatement?): Unit {
|
||||||
val statements: Array<PsiStatement?> = statement?.getCodeBlock()?.getStatements()!!
|
myResult = myConverter.blockToBlock(statement?.getCodeBlock(), true)
|
||||||
myResult = Block(getConverter().statementsToStatementList(statements), true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun visitBreakStatement(statement: PsiBreakStatement?): Unit {
|
public override fun visitBreakStatement(statement: PsiBreakStatement?): Unit {
|
||||||
@@ -104,7 +97,7 @@ public open class StatementVisitor(converter: Converter): ElementVisitor(convert
|
|||||||
getConverter().statementToStatement(body))
|
getConverter().statementToStatement(body))
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var forStatements: List<Statement> = arrayList()
|
var forStatements: List<Element> = arrayList()
|
||||||
forStatements.add(getConverter().statementToStatement(initialization))
|
forStatements.add(getConverter().statementToStatement(initialization))
|
||||||
forStatements.add(WhileStatement(getConverter().expressionToExpression(condition),
|
forStatements.add(WhileStatement(getConverter().expressionToExpression(condition),
|
||||||
Block(arrayList(getConverter().statementToStatement(body),
|
Block(arrayList(getConverter().statementToStatement(body),
|
||||||
@@ -145,23 +138,23 @@ public open class StatementVisitor(converter: Converter): ElementVisitor(convert
|
|||||||
}
|
}
|
||||||
|
|
||||||
private open fun switchBodyToCases(body: PsiCodeBlock?): List<CaseContainer> {
|
private open fun switchBodyToCases(body: PsiCodeBlock?): List<CaseContainer> {
|
||||||
val cases: List<List<PsiStatement?>> = splitToCases(body)
|
val cases: List<List<PsiElement?>> = splitToCases(body)
|
||||||
val allSwitchStatements: List<PsiStatement?> = arrayList()
|
val allSwitchStatements: List<PsiElement?> = arrayList()
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
// TODO Arrays.asList()
|
// TODO Arrays.asList()
|
||||||
for(s in body.getStatements()) allSwitchStatements.add(s)
|
for(s in body.getStatements()) allSwitchStatements.add(s)
|
||||||
}
|
}
|
||||||
val result: List<CaseContainer> = arrayList()
|
val result: List<CaseContainer> = arrayList()
|
||||||
var pendingLabels: List<Statement> = arrayList()
|
var pendingLabels: List<Element> = arrayList()
|
||||||
var i: Int = 0
|
var i: Int = 0
|
||||||
for (ls : List<PsiStatement?> in cases) {
|
for (ls in cases) {
|
||||||
// TODO assert {(ls?.size()).sure() > 0}
|
// TODO assert {(ls?.size()).sure() > 0}
|
||||||
var label: PsiStatement? = ls[0]
|
var label = ls[0]
|
||||||
// TODO assert {(label is PsiSwitchLabelStatement?)}
|
// TODO assert {(label is PsiSwitchLabelStatement?)}
|
||||||
// TODO assert("not a right index") {allSwitchStatements?.get(i) == label}
|
// TODO assert("not a right index") {allSwitchStatements?.get(i) == label}
|
||||||
if (ls.size() > 1) {
|
if (ls.size() > 1) {
|
||||||
pendingLabels.add(getConverter().statementToStatement(label))
|
pendingLabels.add(getConverter().statementToStatement(label))
|
||||||
val slice: List<PsiStatement?> = ls.subList(1, (ls.size()))
|
val slice: List<PsiElement?> = ls.subList(1, (ls.size()))
|
||||||
if (!containsBreak(slice)) {
|
if (!containsBreak(slice)) {
|
||||||
val statements = getConverter().statementsToStatementList(slice)
|
val statements = getConverter().statementsToStatementList(slice)
|
||||||
statements.addAll(getConverter().statementsToStatementList(getAllToNextBreak(allSwitchStatements, i + ls.size())))
|
statements.addAll(getConverter().statementsToStatementList(getAllToNextBreak(allSwitchStatements, i + ls.size())))
|
||||||
@@ -227,20 +220,12 @@ public open class StatementVisitor(converter: Converter): ElementVisitor(convert
|
|||||||
(psiElement is PsiPrefixExpression && psiElement.getOperationTokenType() == JavaTokenType.PLUSPLUS)
|
(psiElement is PsiPrefixExpression && psiElement.getOperationTokenType() == JavaTokenType.PLUSPLUS)
|
||||||
}
|
}
|
||||||
|
|
||||||
private open fun containsBreak(slice: List<PsiStatement?>): Boolean {
|
private fun containsBreak(slice: List<PsiElement?>) = slice.any { it is PsiBreakStatement }
|
||||||
for (s : PsiStatement? in slice)
|
|
||||||
if ((s is PsiBreakStatement?))
|
|
||||||
{
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
private open fun getAllToNextBreak(allStatements: List<PsiElement?>, start: Int): List<PsiElement?> {
|
||||||
}
|
val result: List<PsiElement?> = arrayList()
|
||||||
|
|
||||||
private open fun getAllToNextBreak(allStatements: List<PsiStatement?>, start: Int): List<PsiStatement?> {
|
|
||||||
var result: List<PsiStatement?> = LinkedList<PsiStatement?>()
|
|
||||||
for (i in start..allStatements.size() - 1) {
|
for (i in start..allStatements.size() - 1) {
|
||||||
var s: PsiStatement? = allStatements.get(i)
|
val s = allStatements.get(i)
|
||||||
if (s is PsiBreakStatement || s is PsiReturnStatement) {
|
if (s is PsiBreakStatement || s is PsiReturnStatement) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -253,19 +238,20 @@ public open class StatementVisitor(converter: Converter): ElementVisitor(convert
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private open fun splitToCases(body: PsiCodeBlock?): List<List<PsiStatement?>> {
|
private open fun splitToCases(body: PsiCodeBlock?): List<List<PsiElement?>> {
|
||||||
val cases: List<List<PsiStatement?>> = arrayList()
|
val cases: List<List<PsiElement?>> = arrayList()
|
||||||
var currentCaseStatements: List<PsiStatement?> = arrayList()
|
var currentCaseStatements: List<PsiElement?> = arrayList()
|
||||||
var isFirst: Boolean = true
|
var isFirst: Boolean = true
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
for (s : PsiStatement? in body.getStatements()) {
|
for (s in body.getChildren()) {
|
||||||
if ((s is PsiSwitchLabelStatement?)) {
|
if (s !is PsiStatement && s !is PsiComment) continue
|
||||||
|
if (s is PsiSwitchLabelStatement) {
|
||||||
if (isFirst) {
|
if (isFirst) {
|
||||||
isFirst = false
|
isFirst = false
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cases.add(currentCaseStatements)
|
cases.add(currentCaseStatements)
|
||||||
currentCaseStatements = LinkedList<PsiStatement?>()
|
currentCaseStatements = arrayList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,17 @@ This is a block comment
|
|||||||
|
|
||||||
|
|
||||||
class C {
|
class C {
|
||||||
// This is a comment
|
// This is a class comment
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a field doc comment.
|
||||||
|
*/
|
||||||
|
private int i;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a function doc comment.
|
||||||
|
*/
|
||||||
public void foo() {
|
public void foo() {
|
||||||
/* This is a comment */
|
/* This is a function comment */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,16 @@
|
|||||||
/*
|
/*
|
||||||
This is a block comment
|
This is a block comment
|
||||||
*/
|
*/
|
||||||
|
open class C() {
|
||||||
|
// This is a class comment
|
||||||
|
/**
|
||||||
|
* This is a field doc comment.
|
||||||
|
*/
|
||||||
|
private var i : Int = 0
|
||||||
|
/**
|
||||||
|
* This is a function doc comment.
|
||||||
|
*/
|
||||||
|
public open fun foo() : Unit {
|
||||||
|
/* This is a function comment */
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user