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