diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.java deleted file mode 100644 index ac177196a05..00000000000 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2010-2015 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.kotlin.psi; - -import com.intellij.lang.ASTNode; -import com.intellij.psi.PsiElement; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.JetNodeType; - -import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; -import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern; - -public abstract class JetExpressionImpl extends JetElementImpl implements JetExpression { - public JetExpressionImpl(@NotNull ASTNode node) { - super(node); - } - - @Override - public R accept(@NotNull JetVisitor visitor, D data) { - return visitor.visitExpression(this, data); - } - - protected JetExpression findExpressionUnder(JetNodeType type) { - JetContainerNode containerNode = (JetContainerNode) findChildByType(type); - if (containerNode == null) return null; - - return containerNode.findChildByClass(JetExpression.class); - } - - //NOTE: duplicate with JetExpressionImplStub - @NotNull - @Override - public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { - PsiElement parent = getParent(); - if (parent instanceof JetExpression && newElement instanceof JetExpression && - JetPsiUtil.areParenthesesNecessary((JetExpression) newElement, this, (JetExpression) parent)) { - return super.replace(createExpressionByPattern(JetPsiFactory(this), "($0)", newElement)); - } - return super.replace(newElement); - } -} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.kt new file mode 100644 index 00000000000..294512455c3 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImpl.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2010-2015 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.kotlin.psi + +import com.intellij.lang.ASTNode +import com.intellij.psi.PsiElement +import com.intellij.util.IncorrectOperationException +import org.jetbrains.kotlin.JetNodeType + +public abstract class JetExpressionImpl(node: ASTNode) : JetElementImpl(node), JetExpression { + + override fun accept(visitor: JetVisitor, data: D) = visitor.visitExpression(this, data) + + protected fun findExpressionUnder(type: JetNodeType): JetExpression? { + val containerNode = findChildByType(type) ?: return null + return containerNode.findChildByClass(javaClass()) + } + + override fun replace(newElement: PsiElement): PsiElement { + return replaceExpression(this, newElement, { super.replace(it) }) + } + + companion object { + fun replaceExpression(expression: JetExpression, newElement: PsiElement, rawReplaceHandler: (PsiElement) -> PsiElement): PsiElement { + val parent = expression.getParent() + if (parent is JetExpression && newElement is JetExpression && JetPsiUtil.areParenthesesNecessary(newElement, expression, parent)) { + return rawReplaceHandler(JetPsiFactory(expression).createExpressionByPattern("($0)", newElement)) + } + return rawReplaceHandler(newElement) + } + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImplStub.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImplStub.java index 53cd6eaba3d..be48b989f2c 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImplStub.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetExpressionImplStub.java @@ -21,11 +21,9 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.StubElement; import com.intellij.util.IncorrectOperationException; +import kotlin.jvm.functions.Function1; import org.jetbrains.annotations.NotNull; -import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory; -import static org.jetbrains.kotlin.psi.PsiPackage.createExpressionByPattern; - public abstract class JetExpressionImplStub extends JetElementImplStub implements JetExpression { public JetExpressionImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) { super(stub, nodeType); @@ -40,15 +38,19 @@ public abstract class JetExpressionImplStub extends JetEl return visitor.visitExpression(this, data); } - //NOTE: duplicate with JetExpressionImpl @NotNull @Override public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { - PsiElement parent = getParent(); - if (parent instanceof JetExpression && newElement instanceof JetExpression && - JetPsiUtil.areParenthesesNecessary((JetExpression) newElement, this, (JetExpression) parent)) { - return super.replace(createExpressionByPattern(JetPsiFactory(this), "($0)", newElement)); - } + return JetExpressionImpl.Companion.replaceExpression(this, newElement, new Function1() { + @Override + public PsiElement invoke(PsiElement element) { + return rawReplace(element); + } + }); + } + + @NotNull + private PsiElement rawReplace(@NotNull PsiElement newElement) { return super.replace(newElement); } }