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
017package org.jetbrains.jet.lang.psi.stubs.elements;
018
019import com.intellij.lang.ASTNode;
020import com.intellij.psi.PsiElement;
021import com.intellij.psi.stubs.IStubElementType;
022import com.intellij.psi.stubs.StubElement;
023import com.intellij.psi.util.PsiTreeUtil;
024import org.jetbrains.annotations.NonNls;
025import org.jetbrains.annotations.NotNull;
026import org.jetbrains.jet.lang.psi.*;
027import org.jetbrains.jet.plugin.JetLanguage;
028
029public 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    @Override
038    public String getExternalId() {
039        return "jet." + toString();
040    }
041
042    @Override
043    public boolean shouldCreateStub(ASTNode node) {
044        PsiElement psi = node.getPsi();
045
046        // Do not create stubs inside function literals
047        if (PsiTreeUtil.getParentOfType(psi, JetFunctionLiteral.class) != null) {
048            return false;
049        }
050
051        // Don't create stubs if declaration is inside function or property accessor with block
052        JetBlockExpression blockExpression = PsiTreeUtil.getParentOfType(psi, JetBlockExpression.class);
053        if (blockExpression != null) {
054            return false;
055        }
056
057        // Don't create stubs if declaration is inside other declaration with expression initializer
058        @SuppressWarnings("unchecked") JetWithExpressionInitializer withInitializer =
059                PsiTreeUtil.getParentOfType(psi, JetWithExpressionInitializer.class, true, JetBlockExpression.class);
060        if (withInitializer != null) {
061            JetExpression initializer = withInitializer.getInitializer();
062            if (PsiTreeUtil.isAncestor(initializer, psi, true)) {
063                return false;
064            }
065        }
066
067        // Don't create stubs if declaration is inside property delegate
068        @SuppressWarnings("unchecked") JetPropertyDelegate delegate =
069                PsiTreeUtil.getParentOfType(psi, JetPropertyDelegate.class, true, JetBlockExpression.class);
070        if (delegate != null) {
071            JetExpression delegateExpression = delegate.getExpression();
072            if (PsiTreeUtil.isAncestor(delegateExpression, psi, true)) {
073                return false;
074            }
075        }
076
077        return super.shouldCreateStub(node);
078    }
079}