Don't create stubs for properties inside function initializers (EA-36861)
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.navigation.ItemPresentationProviders;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.stubs.IStubElementType;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
@@ -36,7 +37,7 @@ import java.util.List;
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFunctionStub> implements JetFunction {
|
||||
public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFunctionStub> implements JetFunction, JetWithExpressionInitializer {
|
||||
public JetNamedFunction(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
}
|
||||
@@ -77,6 +78,12 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return findChildByType(JetTokens.EQ);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetExpression getInitializer() {
|
||||
return PsiTreeUtil.getNextSiblingOfType(getEqualsToken(), JetExpression.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns full qualified name for function "package_fqn.function_name"
|
||||
* Not null for top level functions.
|
||||
@@ -103,6 +110,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public IStubElementType getElementType() {
|
||||
return JetStubElementTypes.FUNCTION;
|
||||
@@ -113,6 +121,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return ItemPresentationProviders.getItemPresentation(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetParameterList getValueParameterList() {
|
||||
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
|
||||
@@ -136,6 +145,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return getReturnTypeRef() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetTypeReference getReceiverTypeRef() {
|
||||
PsiElement child = getFirstChild();
|
||||
@@ -151,6 +161,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetTypeReference getReturnTypeRef() {
|
||||
boolean colonPassed = false;
|
||||
@@ -175,6 +186,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLocal() {
|
||||
PsiElement parent = getParent();
|
||||
return !(parent instanceof JetFile || parent instanceof JetClassBody || parent instanceof JetNamespaceBody);
|
||||
|
||||
@@ -39,7 +39,7 @@ import static org.jetbrains.jet.lexer.JetTokens.*;
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStub> {
|
||||
public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStub> implements JetWithExpressionInitializer {
|
||||
public JetProperty(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
}
|
||||
@@ -150,10 +150,10 @@ public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStu
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetExpression getInitializer() {
|
||||
PsiElement eq = findChildByType(EQ);
|
||||
return PsiTreeUtil.getNextSiblingOfType(eq, JetExpression.class);
|
||||
return PsiTreeUtil.getNextSiblingOfType(findChildByType(EQ), JetExpression.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.jet.lang.psi;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
@@ -29,7 +30,8 @@ import java.util.List;
|
||||
/**
|
||||
* @author max
|
||||
*/
|
||||
public class JetPropertyAccessor extends JetDeclarationImpl implements JetDeclarationWithBody, JetModifierListOwner {
|
||||
public class JetPropertyAccessor extends JetDeclarationImpl
|
||||
implements JetDeclarationWithBody, JetModifierListOwner, JetWithExpressionInitializer {
|
||||
public JetPropertyAccessor(@NotNull ASTNode node) {
|
||||
super(node);
|
||||
}
|
||||
@@ -106,4 +108,10 @@ public class JetPropertyAccessor extends JetDeclarationImpl implements JetDeclar
|
||||
}
|
||||
return findChildByType(JetTokens.SET_KEYWORD);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JetExpression getInitializer() {
|
||||
return PsiTreeUtil.getNextSiblingOfType(findChildByType(JetTokens.EQ), JetExpression.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2012 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.psi.PsiElement;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author Nikolay Krasko
|
||||
*/
|
||||
public interface JetWithExpressionInitializer extends PsiElement {
|
||||
@Nullable
|
||||
JetExpression getInitializer();
|
||||
}
|
||||
+12
@@ -46,10 +46,12 @@ public abstract class JetStubElementType<StubT extends StubElement, PsiT extends
|
||||
public boolean shouldCreateStub(ASTNode node) {
|
||||
PsiElement psi = node.getPsi();
|
||||
|
||||
// Do not create stubs inside function literals
|
||||
if (PsiTreeUtil.getParentOfType(psi, JetFunctionLiteral.class) != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't create stubs if declaration is inside function or property accessor with block
|
||||
JetBlockExpression blockExpression = PsiTreeUtil.getParentOfType(psi, JetBlockExpression.class);
|
||||
@SuppressWarnings("unchecked") JetDeclarationWithBody stubStopElement =
|
||||
PsiTreeUtil.getParentOfType(blockExpression, JetFunction.class, JetPropertyAccessor.class);
|
||||
@@ -58,6 +60,16 @@ public abstract class JetStubElementType<StubT extends StubElement, PsiT extends
|
||||
return false;
|
||||
}
|
||||
|
||||
// Don't create stubs if declaration is inside other declaration with expression initializer
|
||||
@SuppressWarnings("unchecked") JetWithExpressionInitializer withInitializer =
|
||||
PsiTreeUtil.getParentOfType(psi, JetWithExpressionInitializer.class, true, JetBlockExpression.class);
|
||||
if (withInitializer != null) {
|
||||
JetExpression initializer = withInitializer.getInitializer();
|
||||
if (PsiTreeUtil.isAncestor(initializer, psi, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return super.shouldCreateStub(node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// TRUE
|
||||
val test : Int
|
||||
get() = <caret>12
|
||||
@@ -34,6 +34,10 @@ public class OutOfBlockModificationTest extends LightCodeInsightFixtureTestCase
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInAntonymsObjectDeclaration() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInClass() {
|
||||
doTest();
|
||||
}
|
||||
@@ -58,7 +62,7 @@ public class OutOfBlockModificationTest extends LightCodeInsightFixtureTestCase
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testInAntonymsObjectDeclaration() {
|
||||
public void testInPropertyAccessorWithInference() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,13 @@ public class JetStubsTest extends LightCodeInsightFixtureTestCase {
|
||||
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n");
|
||||
}
|
||||
|
||||
public void testNotStorePropertyFromInitializer() {
|
||||
doBuildTest("fun DoubleArray.some() = for (element in this) println(element)",
|
||||
"PsiJetFileStubImpl[package=]\n" +
|
||||
" FUN:PsiJetFunctionStubImpl[top ext name=some]\n" +
|
||||
" VALUE_PARAMETER_LIST:PsiJetParameterListStubImpl\n");
|
||||
}
|
||||
|
||||
public void testSimpleEnumBuild() {
|
||||
doBuildTest("enum class Test { First\n Second\n }",
|
||||
"PsiJetFileStubImpl[package=]\n" +
|
||||
|
||||
Reference in New Issue
Block a user