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.resolve;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.name.FqName;
021 import org.jetbrains.kotlin.parsing.JetScriptDefinition;
022 import org.jetbrains.kotlin.parsing.JetScriptDefinitionProvider;
023 import org.jetbrains.kotlin.psi.JetFile;
024 import org.jetbrains.kotlin.psi.JetPackageDirective;
025 import org.jetbrains.kotlin.psi.JetScript;
026
027 public class ScriptNameUtil {
028 private ScriptNameUtil() {
029 }
030
031 @NotNull
032 public static FqName classNameForScript(JetScript script) {
033 JetFile file = script.getContainingJetFile();
034 JetScriptDefinition scriptDefinition = JetScriptDefinitionProvider.getInstance(file.getProject()).findScriptDefinition(file);
035
036 String name = file.getName();
037 int index = name.lastIndexOf('/');
038 if(index != -1)
039 name = name.substring(index+1);
040 if(name.endsWith(scriptDefinition.getExtension()))
041 name = name.substring(0, name.length()-scriptDefinition.getExtension().length());
042 else {
043 index = name.indexOf('.');
044 if(index != -1)
045 name = name.substring(0,index);
046 }
047 name = Character.toUpperCase(name.charAt(0)) + (name.length() == 0 ? "" : name.substring(1));
048 name = name.replace('.', '_');
049 JetPackageDirective directive = file.getPackageDirective();
050 if(directive != null && directive.getQualifiedName().length() > 0) {
051 name = directive.getQualifiedName() + "." + name;
052 }
053 return new FqName(name);
054 }
055 }