Converter:
Member comments are preserved (not only doc comments)
This commit is contained in:
committed by
Pavel V. Talanov
parent
f08861c14a
commit
098a80a2af
@@ -31,7 +31,6 @@ import com.intellij.psi.CommonClassNames.*
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions.*
|
||||
import com.intellij.psi.util.PsiUtil
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.javadoc.PsiDocComment
|
||||
|
||||
public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
|
||||
@@ -89,19 +88,24 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
|
||||
private fun convertMembers(psiClass: PsiClass): List<Element> {
|
||||
val members = ArrayList<Element>()
|
||||
for (e in psiClass.getChildren()) {
|
||||
if (psiClass is PsiAnonymousClass && psiClass.getBaseClassReference() == e) {
|
||||
continue
|
||||
}
|
||||
val allChildren = psiClass.getChildren().toList()
|
||||
for (e in allChildren.subList(allChildren.indexOf(psiClass.getLBrace()), allChildren.size)) {
|
||||
val converted = convertMember(e, psiClass)
|
||||
if (converted != null) members.add(converted)
|
||||
}
|
||||
return members
|
||||
}
|
||||
|
||||
private fun getDocComment(element: PsiDocCommentOwner): Comment? {
|
||||
val psiDocComment = element.getDocComment()
|
||||
return if (psiDocComment != null) Comment(psiDocComment.getText()!!) else null
|
||||
private fun getComments(member: PsiMember): MemberComments {
|
||||
var relevantChildren = member.getChildren().toList()
|
||||
if (member is PsiClass) {
|
||||
val leftBraceIndex = relevantChildren.indexOf(member.getLBrace())
|
||||
relevantChildren = relevantChildren.subList(0, leftBraceIndex)
|
||||
}
|
||||
val whiteSpacesAndComments = relevantChildren
|
||||
.filter { it is PsiWhiteSpace || it is PsiComment }
|
||||
.map { convertElement(it) }
|
||||
return MemberComments(whiteSpacesAndComments)
|
||||
}
|
||||
|
||||
private fun convertMember(e: PsiElement?, containingClass: PsiClass): Element? = when(e) {
|
||||
@@ -109,7 +113,6 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
is PsiField -> convertField(e, containingClass)
|
||||
is PsiClass -> convertClass(e)
|
||||
is PsiClassInitializer -> convertInitializer(e)
|
||||
is PsiDocComment -> null
|
||||
else -> convertElement(e)
|
||||
}
|
||||
|
||||
@@ -169,7 +172,8 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
}
|
||||
}
|
||||
members.add(Constructor(this, Identifier.Empty, null, Collections.emptySet<Modifier>(),
|
||||
//TODO: comments?
|
||||
members.add(Constructor(this, Identifier.Empty, MemberComments.Empty, Collections.emptySet<Modifier>(),
|
||||
ClassType(name, Collections.emptyList<Element>(), false, this),
|
||||
TypeParameterList.Empty,
|
||||
ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)),
|
||||
@@ -178,14 +182,14 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
if (psiClass.isInterface()) {
|
||||
return Trait(this, name, getDocComment(psiClass), modifiers, typeParameters, extendsTypes, Collections.emptyList<Expression>(), implementsTypes, members)
|
||||
return Trait(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, Collections.emptyList<Expression>(), implementsTypes, members)
|
||||
}
|
||||
|
||||
if (psiClass.isEnum()) {
|
||||
return Enum(this, name, getDocComment(psiClass), modifiers, typeParameters, Collections.emptyList<Type>(), Collections.emptyList<Expression>(), implementsTypes, members)
|
||||
return Enum(this, name, getComments(psiClass), modifiers, typeParameters, Collections.emptyList<Type>(), Collections.emptyList<Expression>(), implementsTypes, members)
|
||||
}
|
||||
|
||||
return Class(this, name, getDocComment(psiClass), modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, members)
|
||||
return Class(this, name, getComments(psiClass), modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, members)
|
||||
}
|
||||
|
||||
private fun convertInitializer(i: PsiClassInitializer): Initializer {
|
||||
@@ -200,7 +204,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
val modifiers = convertModifierList(field.getModifierList())
|
||||
if (field is PsiEnumConstant) {
|
||||
return EnumConstant(Identifier(field.getName()!!),
|
||||
getDocComment(field),
|
||||
getComments(field),
|
||||
modifiers,
|
||||
convertType(field.getType()),
|
||||
convertElement(field.getArgumentList()))
|
||||
@@ -212,7 +216,7 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
return Field(Identifier(field.getName()!!),
|
||||
getDocComment(field),
|
||||
getComments(field),
|
||||
modifiers,
|
||||
kType,
|
||||
convertExpression(field.getInitializer(), field.getType()),
|
||||
@@ -252,11 +256,11 @@ public class Converter(val project: Project, val settings: ConverterSettings) {
|
||||
}
|
||||
|
||||
if (method.isConstructor()) {
|
||||
return Constructor(this, identifier, getDocComment(method), modifiers, returnType, typeParameterList, params,
|
||||
return Constructor(this, identifier, getComments(method), modifiers, returnType, typeParameterList, params,
|
||||
Block(body.statements), isConstructorPrimary(method))
|
||||
}
|
||||
|
||||
return Function(this, identifier, getDocComment(method), modifiers, returnType, typeParameterList, params, body)
|
||||
return Function(this, identifier, getComments(method), modifiers, returnType, typeParameterList, params, body)
|
||||
}
|
||||
|
||||
private fun createFunctionParameters(method: PsiMethod): ParameterList {
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.util.Collections
|
||||
public class AnonymousClass(converter: Converter, members: List<Element>)
|
||||
: Class(converter,
|
||||
Identifier("anonClass"),
|
||||
null,
|
||||
MemberComments.Empty,
|
||||
Collections.emptySet<Modifier>(),
|
||||
TypeParameterList.Empty,
|
||||
Collections.emptyList<Type>(),
|
||||
|
||||
@@ -24,13 +24,13 @@ import java.util.ArrayList
|
||||
|
||||
public open class Class(val converter: Converter,
|
||||
val name: Identifier,
|
||||
docComment: Comment?,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
val typeParameterList: TypeParameterList,
|
||||
val extendsTypes: List<Type>,
|
||||
val baseClassParams: List<Expression>,
|
||||
val implementsTypes: List<Type>,
|
||||
val members: List<Element>) : Member(docComment, modifiers) {
|
||||
val members: List<Element>) : Member(comments, modifiers) {
|
||||
open val TYPE: String
|
||||
get() = "class"
|
||||
|
||||
@@ -49,7 +49,7 @@ public open class Class(val converter: Converter,
|
||||
return ""
|
||||
}
|
||||
|
||||
fun secondaryConstructorsAsStaticInitFunction(): MemberList {
|
||||
fun secondaryConstructorsAsStaticInitFunctions(): MemberList {
|
||||
return MemberList(classMembers.secondaryConstructors.elements.map { if (it is Constructor) constructorToInit(it) else it })
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public open class Class(val converter: Converter,
|
||||
val constructorTypeParameters = ArrayList<TypeParameter>()
|
||||
constructorTypeParameters.addAll(typeParameterList.parameters)
|
||||
constructorTypeParameters.addAll(f.typeParameterList.parameters)
|
||||
return Function(converter, Identifier("init"), null, modifiers,
|
||||
return Function(converter, Identifier("init"), MemberComments.Empty, modifiers,
|
||||
ClassType(name, constructorTypeParameters, false, converter),
|
||||
TypeParameterList(constructorTypeParameters), f.params, block)
|
||||
}
|
||||
@@ -109,16 +109,16 @@ public open class Class(val converter: Converter,
|
||||
}
|
||||
|
||||
fun classObjectToKotlin(): String {
|
||||
val secondaryConstructorsAsStaticInitFunction = secondaryConstructorsAsStaticInitFunction()
|
||||
val secondaryConstructorsAsStaticInitFunctions = secondaryConstructorsAsStaticInitFunctions()
|
||||
val staticMembers = classMembers.staticMembers
|
||||
if (secondaryConstructorsAsStaticInitFunction.isEmpty() && staticMembers.isEmpty()) {
|
||||
if (secondaryConstructorsAsStaticInitFunctions.isEmpty() && staticMembers.isEmpty()) {
|
||||
return ""
|
||||
}
|
||||
return "\nclass object {${secondaryConstructorsAsStaticInitFunction.toKotlin()}${staticMembers.toKotlin()}}"
|
||||
return "\nclass object {${secondaryConstructorsAsStaticInitFunctions.toKotlin()}${staticMembers.toKotlin()}}"
|
||||
}
|
||||
|
||||
override fun toKotlin(): String =
|
||||
docCommentToKotlin() +
|
||||
commentsToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
TYPE + " " + name.toKotlin() +
|
||||
typeParameterList.toKotlin() +
|
||||
@@ -126,13 +126,4 @@ public open class Class(val converter: Converter,
|
||||
implementTypesToKotlin() +
|
||||
typeParameterList.whereToKotlin().withPrefix(" ") +
|
||||
bodyToKotlin()
|
||||
|
||||
|
||||
private fun getStatic(members: List<Node>): List<Node> {
|
||||
return members.filter { it is Member && it.isStatic() }
|
||||
}
|
||||
|
||||
private fun getNonStatic(members: List<Node>): List<Node> {
|
||||
return members.filterNot { it is Member && it.isStatic() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ import org.jetbrains.jet.j2k.ast.types.Type
|
||||
import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
public class Constructor(converter: Converter,
|
||||
identifier: Identifier,
|
||||
docComment: Comment?,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
typeParameters: TypeParameterList,
|
||||
params: Element,
|
||||
block: Block,
|
||||
val isPrimary: Boolean) : Function(converter, identifier, docComment, modifiers,
|
||||
`type`, typeParameters, params, block) {
|
||||
identifier: Identifier,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
typeParameters: TypeParameterList,
|
||||
params: Element,
|
||||
block: Block,
|
||||
val isPrimary: Boolean) : Function(converter, identifier, comments, modifiers,
|
||||
`type`, typeParameters, params, block) {
|
||||
|
||||
public fun primarySignatureToKotlin(): String {
|
||||
return "(" + params.toKotlin() + ")"
|
||||
|
||||
@@ -21,14 +21,14 @@ import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public class Enum(converter: Converter,
|
||||
name: Identifier,
|
||||
docComments: Comment?,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameterList: TypeParameterList,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Element>) : Class(converter, name, docComments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
members: List<Element>) : Class(converter, name, comments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
|
||||
override fun primaryConstructorSignatureToKotlin(): String {
|
||||
val s: String = super.primaryConstructorSignatureToKotlin()
|
||||
|
||||
@@ -19,10 +19,10 @@ package org.jetbrains.jet.j2k.ast
|
||||
import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public open class EnumConstant(identifier: Identifier,
|
||||
docComment: Comment?,
|
||||
members: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
`type`: Type,
|
||||
params: Element) : Field(identifier, docComment, modifiers, `type`.convertedToNotNull(), params, 0) {
|
||||
params: Element) : Field(identifier, members, modifiers, `type`.convertedToNotNull(), params, 0) {
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
if (initializer.toKotlin().isEmpty()) {
|
||||
|
||||
@@ -21,11 +21,11 @@ import org.jetbrains.jet.j2k.*
|
||||
import java.util.ArrayList
|
||||
|
||||
public open class Field(val identifier: Identifier,
|
||||
docComment: Comment?,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
val `type`: Type,
|
||||
val initializer: Element,
|
||||
val writingAccesses: Int) : Member(docComment, modifiers) {
|
||||
val writingAccesses: Int) : Member(comments, modifiers) {
|
||||
|
||||
fun modifiersToKotlin(): String {
|
||||
val modifierList = ArrayList<Modifier>()
|
||||
@@ -44,7 +44,7 @@ public open class Field(val identifier: Identifier,
|
||||
public fun isVal(): Boolean = modifiers.contains(Modifier.FINAL)
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
val declaration: String = docCommentToKotlin() +
|
||||
val declaration: String = commentsToKotlin() +
|
||||
modifiersToKotlin() + identifier.toKotlin() + " : " + `type`.toKotlin()
|
||||
if (initializer.isEmpty()) {
|
||||
return declaration + ((if (isVal() && !isStatic() && writingAccesses != 0)
|
||||
|
||||
@@ -23,12 +23,12 @@ import org.jetbrains.jet.j2k.Converter
|
||||
|
||||
public open class Function(val converter: Converter,
|
||||
val name: Identifier,
|
||||
docComment: Comment?,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
val `type`: Type,
|
||||
val typeParameterList: TypeParameterList,
|
||||
val params: Element,
|
||||
var block: Block?) : Member(docComment, modifiers) {
|
||||
var block: Block?) : Member(comments, modifiers) {
|
||||
|
||||
private fun modifiersToKotlin(): String {
|
||||
val resultingModifiers = ArrayList<Modifier>()
|
||||
@@ -64,7 +64,7 @@ public open class Function(val converter: Converter,
|
||||
private fun returnTypeToKotlin() = if (!`type`.isUnit()) " : " + `type`.toKotlin() + " " else " "
|
||||
|
||||
public override fun toKotlin(): String {
|
||||
return docCommentToKotlin() +
|
||||
return commentsToKotlin() +
|
||||
modifiersToKotlin() +
|
||||
"fun ${typeParameterList.toKotlin().withSuffix(" ")}${name.toKotlin()}" +
|
||||
"(${params.toKotlin()})" +
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.jetbrains.jet.j2k.ast
|
||||
|
||||
|
||||
//TODO: is a member?
|
||||
public class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(null, modifiers) {
|
||||
public class Initializer(val block: Block, modifiers: Set<Modifier>) : Member(MemberComments.Empty, modifiers) {
|
||||
public override fun toKotlin(): String {
|
||||
return block.toKotlin()
|
||||
}
|
||||
|
||||
@@ -18,14 +18,20 @@ package org.jetbrains.jet.j2k.ast
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
public abstract class Member(val docComment: Comment?, val modifiers: Set<Modifier>) : Element {
|
||||
public class MemberComments(elements: List<Element>) : WhiteSpaceSeparatedElementList(elements, WhiteSpace.NoSpace) {
|
||||
class object {
|
||||
public val Empty: MemberComments = MemberComments(ArrayList())
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Member(val comments: MemberComments, val modifiers: Set<Modifier>) : Element {
|
||||
fun accessModifier(): Modifier? {
|
||||
return modifiers.find { m -> m == Modifier.PUBLIC || m == Modifier.PROTECTED || m == Modifier.PRIVATE }
|
||||
}
|
||||
|
||||
public fun isAbstract(): Boolean = modifiers.contains(Modifier.ABSTRACT)
|
||||
public fun isStatic(): Boolean = modifiers.contains(Modifier.STATIC)
|
||||
public fun docCommentToKotlin(): String = if (docComment != null) docComment.toKotlin() + "\n" else ""
|
||||
public fun commentsToKotlin(): String = comments.toKotlin()
|
||||
}
|
||||
|
||||
//member itself and all the elements before it in the code (comments, whitespaces)
|
||||
|
||||
@@ -21,13 +21,13 @@ import org.jetbrains.jet.j2k.ast.types.Type
|
||||
|
||||
public class Trait(converter: Converter,
|
||||
name: Identifier,
|
||||
docComment: Comment?,
|
||||
comments: MemberComments,
|
||||
modifiers: Set<Modifier>,
|
||||
typeParameterList: TypeParameterList,
|
||||
extendsTypes: List<Type>,
|
||||
baseClassParams: List<Expression>,
|
||||
implementsTypes: List<Type>,
|
||||
members: List<Element>) : Class(converter, name, docComment, modifiers, typeParameterList,
|
||||
members: List<Element>) : Class(converter, name, comments, modifiers, typeParameterList,
|
||||
extendsTypes, baseClassParams, implementsTypes, members) {
|
||||
|
||||
override val TYPE: String
|
||||
|
||||
@@ -37,6 +37,7 @@ public class WhiteSpace(val text: String) : Element {
|
||||
|
||||
class object {
|
||||
public val NewLine: WhiteSpace = WhiteSpace("\n")
|
||||
public val NoSpace: WhiteSpace = WhiteSpace("")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,4 +16,44 @@ class C() {
|
||||
public fun foo() {
|
||||
/* This is a function comment */
|
||||
}
|
||||
|
||||
//simple one line comment for function
|
||||
fun f1() {
|
||||
}
|
||||
|
||||
//simple one line comment for field
|
||||
var j: Int = 0
|
||||
|
||||
//double c style
|
||||
//comment before function
|
||||
fun f2() {
|
||||
}
|
||||
|
||||
//double c style
|
||||
//comment before field
|
||||
var k: Int = 0
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
fun f3() {
|
||||
}
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
var l: Int = 0
|
||||
|
||||
/*two*/ /*comments*//*line*/
|
||||
var z: Int = 0
|
||||
}
|
||||
@@ -7,17 +7,56 @@ This is a block comment
|
||||
|
||||
|
||||
class C {
|
||||
// This is a class comment
|
||||
// This is a class comment
|
||||
|
||||
/**
|
||||
/**
|
||||
* This is a field doc comment.
|
||||
*/
|
||||
private int i;
|
||||
private int i;
|
||||
|
||||
/**
|
||||
/**
|
||||
* This is a function doc comment.
|
||||
*/
|
||||
public void foo() {
|
||||
/* This is a function comment */
|
||||
}
|
||||
public void foo() {
|
||||
/* This is a function comment */
|
||||
}
|
||||
|
||||
//simple one line comment for function
|
||||
void f1() {
|
||||
}
|
||||
|
||||
//simple one line comment for field
|
||||
int j;
|
||||
|
||||
//double c style
|
||||
//comment before function
|
||||
void f2() {
|
||||
}
|
||||
|
||||
//double c style
|
||||
//comment before field
|
||||
int k;
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
void f3() {}
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
int l;
|
||||
|
||||
/*two*/ /*comments*//*line*/
|
||||
int z;
|
||||
}
|
||||
|
||||
@@ -16,4 +16,44 @@ open class C() {
|
||||
public open fun foo() {
|
||||
/* This is a function comment */
|
||||
}
|
||||
|
||||
//simple one line comment for function
|
||||
open fun f1() {
|
||||
}
|
||||
|
||||
//simple one line comment for field
|
||||
var j: Int = 0
|
||||
|
||||
//double c style
|
||||
//comment before function
|
||||
open fun f2() {
|
||||
}
|
||||
|
||||
//double c style
|
||||
//comment before field
|
||||
var k: Int = 0
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
open fun f3() {
|
||||
}
|
||||
|
||||
//combination
|
||||
/** of
|
||||
*/
|
||||
//
|
||||
/**
|
||||
* different
|
||||
*/
|
||||
//comments
|
||||
var l: Int = 0
|
||||
|
||||
/*two*/ /*comments*//*line*/
|
||||
var z: Int = 0
|
||||
}
|
||||
Reference in New Issue
Block a user