From 28f169df4a6cc2903bc05c89c3e27bec3d977fff Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Fri, 28 Mar 2014 18:30:55 +0400 Subject: [PATCH] Introduce JetExpressionImplStub --- .../jet/lang/psi/JetExpressionImpl.java | 2 + .../jet/lang/psi/JetExpressionImplStub.java | 51 +++++++++++++++++++ .../lang/psi/JetNameReferenceExpression.java | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImplStub.java diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImpl.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImpl.java index 31ee9b2bdd2..f1f1a1cbed1 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImpl.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImpl.java @@ -39,6 +39,8 @@ public abstract class JetExpressionImpl extends JetElementImpl implements JetExp return containerNode.findChildByClass(JetExpression.class); } + //NOTE: duplicate with JetExpressionImplStub + @NotNull @Override public PsiElement replace(@NotNull PsiElement newElement) throws IncorrectOperationException { PsiElement parent = getParent(); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImplStub.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImplStub.java new file mode 100644 index 00000000000..20fa3e487da --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetExpressionImplStub.java @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2014 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.jet.lang.psi; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.PsiElement; +import com.intellij.psi.stubs.IStubElementType; +import com.intellij.psi.stubs.StubElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; + +public abstract class JetExpressionImplStub extends JetElementImplStub implements JetExpression { + public JetExpressionImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) { + super(stub, nodeType); + } + + public JetExpressionImplStub(@NotNull ASTNode node) { + super(node); + } + + @Override + public R accept(@NotNull JetVisitor visitor, D data) { + 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(JetPsiFactory.createExpression(getProject(), "(" + newElement.getText() + ")")); + } + return super.replace(newElement); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java index f48bc44d807..8cc8a4941e6 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java @@ -29,7 +29,7 @@ import org.jetbrains.jet.lexer.JetTokens; import static org.jetbrains.jet.lexer.JetTokens.*; -public class JetNameReferenceExpression extends JetElementImplStub implements JetSimpleNameExpression { +public class JetNameReferenceExpression extends JetExpressionImplStub implements JetSimpleNameExpression { private static final TokenSet NAME_REFERENCE_EXPRESSIONS = TokenSet.create(IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD);