J2K: Convert constructor PSI classes to Kotlin

This commit is contained in:
Alexey Sedunov
2015-06-09 16:34:38 +03:00
parent 7ed02dcd58
commit 1ab90c7814
3 changed files with 87 additions and 246 deletions
@@ -14,188 +14,79 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.psi; package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode
import com.intellij.navigation.ItemPresentation; import com.intellij.navigation.ItemPresentationProviders
import com.intellij.navigation.ItemPresentationProviders; import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElement; import com.intellij.util.IncorrectOperationException
import com.intellij.psi.stubs.IStubElementType; import org.jetbrains.kotlin.lexer.JetTokens
import com.intellij.util.IncorrectOperationException; import org.jetbrains.kotlin.name.Name
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.psi.stubs.elements.JetPlaceHolderStubElementType
import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import java.util.Collections; public abstract class JetConstructor<T : JetConstructor<T>> : JetDeclarationStub<KotlinPlaceHolderStub<T>>, JetFunction {
import java.util.List; protected constructor(node: ASTNode) : super(node)
protected constructor(stub: KotlinPlaceHolderStub<T>, nodeType: JetPlaceHolderStubElementType<T>) : super(stub, nodeType)
public abstract class JetConstructor<T extends JetConstructor<T>> extends JetDeclarationStub<KotlinPlaceHolderStub<T>> implements JetFunction { public abstract fun getClassOrObject(): JetClassOrObject
protected JetConstructor(@NotNull ASTNode node) {
super(node);
}
protected JetConstructor(@NotNull KotlinPlaceHolderStub<T> stub, @NotNull IStubElementType nodeType) { override fun isLocal() = false
super(stub, nodeType);
}
@NotNull override fun getValueParameterList() = getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST)
public abstract JetClassOrObject getClassOrObject();
@Override override fun getValueParameters() = getValueParameterList()?.getParameters() ?: emptyList()
public boolean isLocal() {
return false;
}
@Override override fun getReceiverTypeReference() = null
@Nullable
public JetParameterList getValueParameterList() {
return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
}
@Override override fun getTypeReference() = null
@NotNull
public List<JetParameter> getValueParameters() {
JetParameterList list = getValueParameterList();
return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
}
@Nullable throws(IncorrectOperationException::class)
@Override override fun setTypeReference(typeRef: JetTypeReference?) = throw IncorrectOperationException("setTypeReference to constructor")
public JetTypeReference getReceiverTypeReference() {
return null;
}
@Nullable override fun getColon() = findChildByType<PsiElement>(JetTokens.COLON)
@Override
public JetTypeReference getTypeReference() {
return null;
}
@Nullable override fun getBodyExpression(): JetBlockExpression? = null
@Override
public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
return null;
}
@Nullable override fun getEqualsToken() = null
@Override
public PsiElement getColon() {
return findChildByType(JetTokens.COLON);
}
@Nullable override fun hasBlockBody() = true
@Override
public JetBlockExpression getBodyExpression() {
return null;
}
@Nullable override fun hasBody() = getBodyExpression() != null
@Override
public PsiElement getEqualsToken() {
return null;
}
@Override override fun hasDeclaredReturnType() = false
public boolean hasBlockBody() {
return true;
}
@Override override fun getTypeParameterList() = null
public boolean hasBody() {
return getBodyExpression() != null;
}
@Override override fun getTypeConstraintList() = null
public boolean hasDeclaredReturnType() {
return false;
}
@Nullable override fun getTypeConstraints() = emptyList<JetTypeConstraint>()
@Override
public JetTypeParameterList getTypeParameterList() {
return null;
}
@Nullable override fun getTypeParameters() = emptyList<JetTypeParameter>()
@Override
public JetTypeConstraintList getTypeConstraintList() {
return null;
}
@NotNull override fun getName(): String = getClassOrObject().getName()!!
@Override
public List<JetTypeConstraint> getTypeConstraints() {
return Collections.emptyList();
}
@NotNull override fun getNameAsSafeName() = Name.identifier(getName())
@Override
public List<JetTypeParameter> getTypeParameters() {
return Collections.emptyList();
}
@Override override fun getFqName() = null
@NotNull
public String getName() {
return getClassOrObject().getName();
}
@NotNull override fun getNameAsName() = getNameAsSafeName()
@Override
public Name getNameAsSafeName() {
return Name.identifier(getName());
}
@Nullable override fun getNameIdentifier() = null
@Override
public FqName getFqName() {
return null;
}
@Nullable throws(IncorrectOperationException::class)
@Override override fun setName(name: String): PsiElement = throw IncorrectOperationException("setName to constructor")
public Name getNameAsName() {
return getNameAsSafeName();
}
@Nullable override fun getPresentation() = ItemPresentationProviders.getItemPresentation(this)
@Override
public PsiElement getNameIdentifier() {
return null;
}
@Override public open fun getConstructorKeyword(): PsiElement? = findChildByType(JetTokens.CONSTRUCTOR_KEYWORD)
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
throw new IncorrectOperationException("setName to constructor");
}
@Override public fun hasConstructorKeyword(): Boolean = getStub() != null || getConstructorKeyword() != null
public ItemPresentation getPresentation() {
return ItemPresentationProviders.getItemPresentation(this);
}
@Nullable override fun getTextOffset(): Int {
public PsiElement getConstructorKeyword() { return getConstructorKeyword()?.getTextOffset()
return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD); ?: getValueParameterList()?.getTextOffset()
} ?: super<JetDeclarationStub>.getTextOffset()
public boolean hasConstructorKeyword() {
if (getStub() != null) return true;
return getConstructorKeyword() != null;
}
@Override
public int getTextOffset() {
PsiElement keyword = getConstructorKeyword();
if (keyword != null) return keyword.getTextOffset();
JetParameterList parameterList = getValueParameterList();
if (parameterList != null) return parameterList.getTextOffset();
return super.getTextOffset();
} }
} }
@@ -14,55 +14,35 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.psi; package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken; import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.psi.addRemoveModifier.*
import org.jetbrains.kotlin.psi.addRemoveModifier.AddRemoveModifierPackage; import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub; import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
public class JetPrimaryConstructor extends JetConstructor<JetPrimaryConstructor> { public class JetPrimaryConstructor : JetConstructor<JetPrimaryConstructor> {
public JetPrimaryConstructor(@NotNull ASTNode node) { public constructor(node: ASTNode) : super(node)
super(node); public constructor(stub: KotlinPlaceHolderStub<JetPrimaryConstructor>) : super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR)
}
public JetPrimaryConstructor(@NotNull KotlinPlaceHolderStub<JetPrimaryConstructor> stub) { override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitPrimaryConstructor(this, data)
super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR);
}
@Override public fun getContainingClassOrObject(): JetClassOrObject = getParent() as JetClassOrObject
public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
return visitor.visitPrimaryConstructor(this, data);
}
@NotNull override fun getClassOrObject() = getContainingClassOrObject()
public JetClassOrObject getContainingClassOrObject() {
return (JetClassOrObject) getParent();
}
@NotNull override fun addModifier(modifier: JetModifierKeywordToken) {
@Override val modifierList = getModifierList()
public JetClassOrObject getClassOrObject() {
return getContainingClassOrObject();
}
@Override
public void addModifier(@NotNull JetModifierKeywordToken modifier) {
JetModifierList modifierList = getModifierList();
if (modifierList != null) { if (modifierList != null) {
AddRemoveModifierPackage.addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD); addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD)
} }
else { else {
if (modifier == JetTokens.PUBLIC_KEYWORD) return; if (modifier == JetTokens.PUBLIC_KEYWORD) return
val parameterList = getValueParameterList()!!
JetParameterList parameterList = getValueParameterList(); val newModifierList = JetPsiFactory(getProject()).createModifierList(modifier)
assert parameterList != null; addBefore(newModifierList, parameterList)
JetPsiFactory psiFactory = new JetPsiFactory(getProject());
JetModifierList newModifierList = psiFactory.createModifierList(modifier);
addBefore(newModifierList, parameterList);
} }
} }
} }
@@ -14,70 +14,40 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.psi; package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
public class JetSecondaryConstructor extends JetConstructor<JetSecondaryConstructor> { public class JetSecondaryConstructor : JetConstructor<JetSecondaryConstructor> {
public JetSecondaryConstructor(@NotNull ASTNode node) { public constructor(node: ASTNode) : super(node)
super(node); public constructor(stub: KotlinPlaceHolderStub<JetSecondaryConstructor>) : super(stub, JetStubElementTypes.SECONDARY_CONSTRUCTOR)
}
public JetSecondaryConstructor(@NotNull KotlinPlaceHolderStub<JetSecondaryConstructor> stub) { override fun <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitSecondaryConstructor(this, data)
super(stub, JetStubElementTypes.SECONDARY_CONSTRUCTOR);
}
@Override override fun getClassOrObject() = getParent().getParent() as JetClassOrObject
public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
return visitor.visitSecondaryConstructor(this, data);
}
@Override override fun getBodyExpression() = findChildByClass(javaClass<JetBlockExpression>())
@NotNull
public JetClassOrObject getClassOrObject() {
return (JetClassOrObject) getParent().getParent();
}
@Nullable override fun getConstructorKeyword() = notNullChild<PsiElement>(super.getConstructorKeyword())
@Override
public JetBlockExpression getBodyExpression() {
return findChildByClass(JetBlockExpression.class);
}
@Override public fun getDelegationCall(): JetConstructorDelegationCall = findNotNullChildByClass(javaClass<JetConstructorDelegationCall>())
@NotNull
public PsiElement getConstructorKeyword() {
//noinspection ConstantConditions
return notNullChild(super.getConstructorKeyword());
}
@NotNull public fun hasImplicitDelegationCall(): Boolean = getDelegationCall().isImplicit()
public JetConstructorDelegationCall getDelegationCall() {
return findNotNullChildByClass(JetConstructorDelegationCall.class);
}
public boolean hasImplicitDelegationCall() { public fun replaceImplicitDelegationCallWithExplicit(isThis: Boolean): JetConstructorDelegationCall {
return getDelegationCall().isImplicit(); val psiFactory = JetPsiFactory(getProject())
} val current = getDelegationCall()
@NotNull assert(current.isImplicit()) { "Method should not be called with explicit delegation call: " + getText() }
public JetConstructorDelegationCall replaceImplicitDelegationCallWithExplicit(boolean isThis) { current.delete()
JetPsiFactory psiFactory = new JetPsiFactory(getProject());
JetConstructorDelegationCall current = getDelegationCall();
assert current.isImplicit() val colon = addAfter(psiFactory.createColon(), getValueParameterList())
: "Method should not be called with explicit delegation call: " + getText();
current.delete();
PsiElement colon = addAfter(psiFactory.createColon(), getValueParameterList()); val delegationName = if (isThis) "this" else "super"
String delegationName = isThis ? "this" : "super"; return addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon) as JetConstructorDelegationCall
return (JetConstructorDelegationCall) addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon);
} }
} }