Java to Kotlin converter: working on not loosing comments - Element made Class instead of Trait

This commit is contained in:
Valentin Kipyatkov
2014-06-17 13:36:22 +04:00
parent 600446bbd2
commit c4bac83cc9
14 changed files with 81 additions and 22 deletions
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.j2k
import com.intellij.psi.*
import java.util.ArrayList
import org.jetbrains.jet.j2k.ast.Element
class CommentConverter(private val topElement: PsiElement) {
private enum class CommentPlacement {
Before
SameLineBefore
SameLineAfter
After
}
private class CommentInfo(val comment: PsiComment) {
val startOffset: Int
val endOffset: Int
var isAttached = false
{
val range = comment.getTextRange()!!
startOffset = range.getStartOffset()
endOffset = range.getEndOffset()
}
}
private val allComments: List<CommentInfo> = run {
val list = ArrayList<CommentInfo>()
topElement.accept(object : JavaRecursiveElementVisitor(){
override fun visitComment(comment: PsiComment) {
list.add(CommentInfo(comment))
}
})
list
}
/*
public fun toKotlin(elementToKotlin: () -> String, originalElements: List<PsiElement>): String {
}
*/
}
@@ -18,7 +18,7 @@ package org.jetbrains.jet.j2k.ast
import java.util.HashSet
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>, val brackets: Boolean) : Element {
class Annotation(val name: Identifier, val arguments: List<Pair<Identifier?, Expression>>, val brackets: Boolean) : Element() {
private fun surroundWithBrackets(text: String) = if (brackets) "[$text]" else text
override fun toKotlin(): String {
+5 -5
View File
@@ -16,17 +16,17 @@
package org.jetbrains.jet.j2k.ast
trait Element {
public fun toKotlin(): String
abstract class Element {
public abstract fun toKotlin(): String
public val isEmpty: Boolean get() = false
public open val isEmpty: Boolean get() = false
object Empty : Element {
object Empty : Element() {
override fun toKotlin() = ""
override val isEmpty: Boolean get() = true
}
}
class Comment(val text: String) : Element {
class Comment(val text: String) : Element() {
override fun toKotlin() = text
}
+2 -2
View File
@@ -18,14 +18,14 @@ package org.jetbrains.jet.j2k.ast
class FileMemberList(elements: List<Element>) : WhiteSpaceSeparatedElementList(elements, WhiteSpace.NewLine, false)
class PackageStatement(val packageName: String) : Element {
class PackageStatement(val packageName: String) : Element() {
override fun toKotlin(): String = "package " + packageName
}
class File(
val body: FileMemberList,
val mainFunction: String
) : Element {
) : Element() {
override fun toKotlin(): String {
return body.toKotlin() + mainFunction
+2 -2
View File
@@ -24,11 +24,11 @@ import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap
import com.intellij.psi.PsiJavaCodeReferenceElement
import org.jetbrains.jet.asJava.KotlinLightClassForPackage
class Import(val name: String) : Element {
class Import(val name: String) : Element() {
override fun toKotlin() = "import " + name
}
class ImportList(public val imports: List<Import>) : Element {
class ImportList(public val imports: List<Import>) : Element() {
override val isEmpty: Boolean
get() = imports.isEmpty()
@@ -26,7 +26,7 @@ class LocalVariable(
private val initializer: Expression,
private val isVal: Boolean,
private val settings: ConverterSettings
) : Element {
) : Element() {
override fun toKotlin(): String {
val start = annotations.toKotlin() + if (isVal) "val" else "var"
+1 -1
View File
@@ -30,7 +30,7 @@ class MemberComments(elements: List<Element>) : WhiteSpaceSeparatedElementList(e
}
}
abstract class Member(val comments: MemberComments, val annotations: Annotations, val modifiers: Set<Modifier>) : Element {
abstract class Member(val comments: MemberComments, val annotations: Annotations, val modifiers: Set<Modifier>) : Element() {
fun commentsToKotlin(): String = comments.toKotlin()
}
@@ -20,7 +20,7 @@ class Parameter(val identifier: Identifier,
val `type`: Type,
val varVal: Parameter.VarValModifier,
val annotations: Annotations,
val modifiers: Collection<Modifier>) : Element {
val modifiers: Collection<Modifier>) : Element() {
public enum class VarValModifier {
None
Val
@@ -17,7 +17,7 @@
package org.jetbrains.jet.j2k.ast
open class ParameterList(val parameters: List<Parameter>) : Element {
open class ParameterList(val parameters: List<Parameter>) : Element() {
override fun toKotlin() = parameters.map { it.toKotlin() }.makeString(", ")
}
@@ -16,6 +16,6 @@
package org.jetbrains.jet.j2k.ast
class ReferenceElement(val reference: Identifier, val types: List<Type>) : Element {
class ReferenceElement(val reference: Identifier, val types: List<Type>) : Element() {
override fun toKotlin() = reference.toKotlin() + types.toKotlin(", ", "<", ">")
}
@@ -17,7 +17,7 @@
package org.jetbrains.jet.j2k.ast
abstract class Statement() : Element {
abstract class Statement() : Element() {
object Empty : Statement() {
override fun toKotlin() = ""
@@ -16,7 +16,7 @@
package org.jetbrains.jet.j2k.ast
class TypeElement(val `type`: Type) : Element {
class TypeElement(val `type`: Type) : Element() {
override fun toKotlin() = `type`.toKotlin()
fun toKotlinNotNull(): String = `type`.toNotNullType().toKotlin()
@@ -21,11 +21,12 @@ import org.jetbrains.jet.j2k.Converter
import com.intellij.psi.PsiTypeParameterList
import java.util.ArrayList
class TypeParameter(val name: Identifier, val extendsTypes: List<Type>) : Element {
class TypeParameter(val name: Identifier, val extendsTypes: List<Type>) : Element() {
fun hasWhere(): Boolean = extendsTypes.size() > 1
fun getWhereToKotlin(): String {
if (hasWhere()) {
return name.toKotlin() + " : " + extendsTypes.get(1).toKotlin()
return name.toKotlin() + " : " + extendsTypes[1].toKotlin()
}
return ""
@@ -33,14 +34,14 @@ class TypeParameter(val name: Identifier, val extendsTypes: List<Type>) : Elemen
override fun toKotlin(): String {
if (extendsTypes.size() > 0) {
return name.toKotlin() + " : " + extendsTypes [0].toKotlin()
return name.toKotlin() + " : " + extendsTypes[0].toKotlin()
}
return name.toKotlin()
}
}
class TypeParameterList(val parameters: List<TypeParameter>) : Element {
class TypeParameterList(val parameters: List<TypeParameter>) : Element() {
override fun toKotlin(): String = if (!parameters.isEmpty())
parameters.map {
it.toKotlin()
@@ -17,7 +17,7 @@
package org.jetbrains.jet.j2k.ast
class WhiteSpace(val text: String) : Element {
class WhiteSpace(val text: String) : Element() {
override fun toKotlin() = text
override val isEmpty: Boolean