001 /* 002 * Copyright 2010-2015 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.kotlin.psi; 018 019 import com.intellij.lang.ASTNode; 020 import com.intellij.psi.util.PsiTreeUtil; 021 import org.jetbrains.annotations.NotNull; 022 import org.jetbrains.kotlin.name.FqName; 023 import org.jetbrains.kotlin.name.Name; 024 import org.jetbrains.kotlin.psi.stubs.KotlinScriptStub; 025 import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes; 026 import org.jetbrains.kotlin.script.KotlinScriptDefinition; 027 import org.jetbrains.kotlin.script.KotlinScriptDefinitionProvider; 028 029 import java.util.List; 030 031 public class KtScript extends KtNamedDeclarationStub<KotlinScriptStub> implements KtDeclarationContainer { 032 033 public KtScript(@NotNull ASTNode node) { 034 super(node); 035 } 036 037 public KtScript(@NotNull KotlinScriptStub stub) { 038 super(stub, KtStubElementTypes.SCRIPT); 039 } 040 041 @NotNull 042 @Override 043 public FqName getFqName() { 044 KotlinScriptStub stub = getStub(); 045 if (stub != null) { 046 return stub.getFqName(); 047 } 048 KtFile containingKtFile = getContainingKtFile(); 049 KotlinScriptDefinition kotlinScriptDefinition = 050 KotlinScriptDefinitionProvider.getInstance(getProject()).findScriptDefinition(containingKtFile); 051 assert kotlinScriptDefinition != null : "Should not parse a script without definition"; 052 return containingKtFile.getPackageFqName().child(kotlinScriptDefinition.getScriptName(this)); 053 } 054 055 @Override 056 public String getName() { 057 return getFqName().shortName().asString(); 058 } 059 060 @NotNull 061 public KtBlockExpression getBlockExpression() { 062 return findNotNullChildByClass(KtBlockExpression.class); 063 } 064 065 @Override 066 @NotNull 067 public List<KtDeclaration> getDeclarations() { 068 return PsiTreeUtil.getChildrenOfTypeAsList(getBlockExpression(), KtDeclaration.class); 069 } 070 071 @Override 072 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) { 073 return visitor.visitScript(this, data); 074 } 075 }