Converted JetExpressionImpl to Kotlin, got rid of code duplication

This commit is contained in:
Valentin Kipyatkov
2015-05-29 19:45:40 +03:00
parent b012f22edc
commit a5cb1e8c2b
3 changed files with 57 additions and 65 deletions
@@ -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, D> R accept(@NotNull JetVisitor<R, D> 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);
}
}
@@ -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 <R, D> accept(visitor: JetVisitor<R, D>, data: D) = visitor.visitExpression(this, data)
protected fun findExpressionUnder(type: JetNodeType): JetExpression? {
val containerNode = findChildByType<JetContainerNode>(type) ?: return null
return containerNode.findChildByClass<JetExpression>(javaClass())
}
override fun replace(newElement: PsiElement): PsiElement {
return replaceExpression(this, newElement, { super<JetElementImpl>.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)
}
}
}
@@ -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<T extends StubElement> extends JetElementImplStub<T> implements JetExpression {
public JetExpressionImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
@@ -40,15 +38,19 @@ public abstract class JetExpressionImplStub<T extends StubElement> 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<PsiElement, PsiElement>() {
@Override
public PsiElement invoke(PsiElement element) {
return rawReplace(element);
}
});
}
@NotNull
private PsiElement rawReplace(@NotNull PsiElement newElement) {
return super.replace(newElement);
}
}