Converted JetExpressionImpl to Kotlin, got rid of code duplication
This commit is contained in:
@@ -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.IStubElementType;
|
||||||
import com.intellij.psi.stubs.StubElement;
|
import com.intellij.psi.stubs.StubElement;
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import com.intellij.util.IncorrectOperationException;
|
||||||
|
import kotlin.jvm.functions.Function1;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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 abstract class JetExpressionImplStub<T extends StubElement> extends JetElementImplStub<T> implements JetExpression {
|
||||||
public JetExpressionImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
|
public JetExpressionImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
|
||||||
super(stub, nodeType);
|
super(stub, nodeType);
|
||||||
@@ -40,15 +38,19 @@ public abstract class JetExpressionImplStub<T extends StubElement> extends JetEl
|
|||||||
return visitor.visitExpression(this, data);
|
return visitor.visitExpression(this, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
//NOTE: duplicate with JetExpressionImpl
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
|
public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException {
|
||||||
PsiElement parent = getParent();
|
return JetExpressionImpl.Companion.replaceExpression(this, newElement, new Function1<PsiElement, PsiElement>() {
|
||||||
if (parent instanceof JetExpression && newElement instanceof JetExpression &&
|
@Override
|
||||||
JetPsiUtil.areParenthesesNecessary((JetExpression) newElement, this, (JetExpression) parent)) {
|
public PsiElement invoke(PsiElement element) {
|
||||||
return super.replace(createExpressionByPattern(JetPsiFactory(this), "($0)", newElement));
|
return rawReplace(element);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private PsiElement rawReplace(@NotNull PsiElement newElement) {
|
||||||
return super.replace(newElement);
|
return super.replace(newElement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user