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.PsiFileBase;
020    import com.intellij.lang.ASTNode;
021    import com.intellij.lang.FileASTNode;
022    import com.intellij.openapi.fileTypes.FileType;
023    import com.intellij.psi.FileViewProvider;
024    import com.intellij.psi.PsiElementVisitor;
025    import com.intellij.psi.util.PsiTreeUtil;
026    import org.jetbrains.annotations.NotNull;
027    import org.jetbrains.annotations.Nullable;
028    import org.jetbrains.jet.JetNodeTypes;
029    import org.jetbrains.jet.lang.psi.stubs.PsiJetFileStub;
030    import org.jetbrains.jet.plugin.JetFileType;
031    import org.jetbrains.jet.plugin.JetLanguage;
032    
033    import java.util.Collections;
034    import java.util.List;
035    
036    public class JetFile extends PsiFileBase implements JetDeclarationContainer, JetElement {
037    
038        private final boolean isCompiled;
039    
040        public JetFile(FileViewProvider viewProvider, boolean compiled) {
041            super(viewProvider, JetLanguage.INSTANCE);
042            this.isCompiled = compiled;
043        }
044    
045        @Override
046        public FileASTNode getNode() {
047            return super.getNode();
048        }
049    
050        public boolean isCompiled() {
051            return isCompiled;
052        }
053    
054        @Override
055        @NotNull
056        public FileType getFileType() {
057            return JetFileType.INSTANCE;
058        }
059    
060        @Override
061        public String toString() {
062            return "JetFile: " + getName();
063        }
064    
065        @NotNull
066        @Override
067        public List<JetDeclaration> getDeclarations() {
068            return PsiTreeUtil.getChildrenOfTypeAsList(this, JetDeclaration.class);
069        }
070    
071        @Nullable
072        public JetImportList getImportList() {
073            return findChildByClass(JetImportList.class);
074        }
075    
076        @NotNull
077        public List<JetImportDirective> getImportDirectives() {
078            JetImportList importList = getImportList();
079            return importList != null ? importList.getImports() : Collections.<JetImportDirective>emptyList();
080        }
081    
082        @Nullable
083        public JetImportDirective findImportByAlias(@NotNull String name) {
084            for (JetImportDirective directive : getImportDirectives()) {
085                if (name.equals(directive.getAliasName())) {
086                    return directive;
087                }
088            }
089            return null;
090        }
091    
092        // scripts has no namespace header
093        @Nullable
094        public JetNamespaceHeader getNamespaceHeader() {
095            ASTNode ast = getNode().findChildByType(JetNodeTypes.NAMESPACE_HEADER);
096            return ast != null ? (JetNamespaceHeader) ast.getPsi() : null;
097        }
098    
099        @Nullable
100        public String getPackageName() {
101            PsiJetFileStub stub = (PsiJetFileStub) getStub();
102            if (stub != null) {
103                return stub.getPackageName();
104            }
105    
106            JetNamespaceHeader statement = getNamespaceHeader();
107            return statement != null ? statement.getQualifiedName() : null;
108        }
109    
110        @Nullable
111        public JetScript getScript() {
112            return PsiTreeUtil.getChildOfType(this, JetScript.class);
113        }
114    
115        public boolean isScript() {
116            PsiJetFileStub stub = (PsiJetFileStub) getStub();
117            if (stub != null) {
118                return stub.isScript();
119            }
120    
121            return getScript() != null;
122        }
123    
124        @NotNull
125        @Override
126        public String getName() {
127            return super.getName(); // TODO
128        }
129    
130        @Override
131        public void accept(@NotNull PsiElementVisitor visitor) {
132            if (visitor instanceof JetVisitorVoid) {
133                accept((JetVisitorVoid) visitor);
134            }
135            else {
136                visitor.visitFile(this);
137            }
138        }
139    
140        @Override
141        public <D> void acceptChildren(@NotNull JetTreeVisitor<D> visitor, D data) {
142            JetPsiUtil.visitChildren(this, visitor, data);
143        }
144    
145        @Override
146        public void accept(@NotNull JetVisitorVoid visitor) {
147            visitor.visitJetFile(this);
148        }
149    
150        @Override
151        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
152            return visitor.visitJetFile(this, data);
153        }
154    }