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;
018    
019    import com.intellij.extapi.psi.StubBasedPsiElementBase;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.lang.Language;
022    import com.intellij.psi.PsiElement;
023    import com.intellij.psi.PsiElementVisitor;
024    import com.intellij.psi.PsiFile;
025    import com.intellij.psi.StubBasedPsiElement;
026    import com.intellij.psi.stubs.IStubElementType;
027    import com.intellij.psi.stubs.StubElement;
028    import org.jetbrains.annotations.NotNull;
029    import org.jetbrains.jet.plugin.JetLanguage;
030    
031    public class JetElementImplStub<T extends StubElement> extends StubBasedPsiElementBase<T>
032            implements JetElement, StubBasedPsiElement<T> {
033        public JetElementImplStub(@NotNull T stub, @NotNull IStubElementType nodeType) {
034            super(stub, nodeType);
035        }
036    
037        public JetElementImplStub(@NotNull ASTNode node) {
038            super(node);
039        }
040    
041        @NotNull
042        @Override
043        public Language getLanguage() {
044            return JetLanguage.INSTANCE;
045        }
046    
047        @Override
048        public String toString() {
049            return getElementType().toString();
050        }
051    
052        @Override
053        public final void accept(@NotNull PsiElementVisitor visitor) {
054            if (visitor instanceof JetVisitor) {
055                accept((JetVisitor) visitor, null);
056            }
057            else {
058                visitor.visitElement(this);
059            }
060        }
061    
062        @NotNull
063        @Override
064        public JetFile getContainingJetFile() {
065            PsiFile file = getContainingFile();
066            assert file instanceof JetFile : "JetElement not inside JetFile: " + file + " " + file.getText();
067            return (JetFile) file;
068        }
069    
070        @Override
071        public <D> void acceptChildren(@NotNull JetTreeVisitor<D> visitor, D data) {
072            PsiElement child = getFirstChild();
073            while (child != null) {
074                if (child instanceof JetElement) {
075                    ((JetElement) child).accept(visitor, data);
076                }
077                child = child.getNextSibling();
078            }
079        }
080    
081        @Override
082        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
083            return visitor.visitJetElement(this, data);
084        }
085    }