001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.psi.stubs.elements;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.psi.stubs.IStubElementType;
022 import com.intellij.psi.stubs.StubElement;
023 import com.intellij.psi.util.PsiTreeUtil;
024 import org.jetbrains.annotations.NonNls;
025 import org.jetbrains.annotations.NotNull;
026 import org.jetbrains.jet.lang.psi.*;
027 import org.jetbrains.jet.plugin.JetLanguage;
028
029 public abstract class JetStubElementType<StubT extends StubElement, PsiT extends PsiElement> extends IStubElementType<StubT, PsiT> {
030
031 public JetStubElementType(@NotNull @NonNls String debugName) {
032 super(debugName, JetLanguage.INSTANCE);
033 }
034
035 public abstract PsiT createPsiFromAst(@NotNull ASTNode node);
036
037 @NotNull
038 @Override
039 public String getExternalId() {
040 return "jet." + toString();
041 }
042
043 @Override
044 public boolean shouldCreateStub(ASTNode node) {
045 PsiElement psi = node.getPsi();
046
047 // Do not create stubs inside function literals
048 if (PsiTreeUtil.getParentOfType(psi, JetFunctionLiteral.class) != null) {
049 return false;
050 }
051
052 // Don't create stubs if declaration is inside function or property accessor with block
053 JetBlockExpression blockExpression = PsiTreeUtil.getParentOfType(psi, JetBlockExpression.class);
054 if (blockExpression != null) {
055 return false;
056 }
057
058 // Don't create stubs if declaration is inside other declaration with expression initializer
059 @SuppressWarnings("unchecked") JetWithExpressionInitializer withInitializer =
060 PsiTreeUtil.getParentOfType(psi, JetWithExpressionInitializer.class, true, JetBlockExpression.class);
061 if (withInitializer != null) {
062 JetExpression initializer = withInitializer.getInitializer();
063 if (PsiTreeUtil.isAncestor(initializer, psi, true)) {
064 return false;
065 }
066 }
067
068 // Don't create stubs if declaration is inside property delegate
069 @SuppressWarnings("unchecked") JetPropertyDelegate delegate =
070 PsiTreeUtil.getParentOfType(psi, JetPropertyDelegate.class, true, JetBlockExpression.class);
071 if (delegate != null) {
072 JetExpression delegateExpression = delegate.getExpression();
073 if (PsiTreeUtil.isAncestor(delegateExpression, psi, true)) {
074 return false;
075 }
076 }
077
078 return super.shouldCreateStub(node);
079 }
080 }