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.k2js.utils; 018 019 import com.intellij.openapi.project.Project; 020 import com.intellij.openapi.util.io.FileUtil; 021 import com.intellij.openapi.util.text.StringUtil; 022 import com.intellij.openapi.vfs.CharsetToolkit; 023 import com.intellij.psi.PsiFile; 024 import com.intellij.psi.PsiFileFactory; 025 import com.intellij.psi.impl.PsiFileFactoryImpl; 026 import com.intellij.testFramework.LightVirtualFile; 027 import org.jetbrains.annotations.NotNull; 028 import org.jetbrains.jet.lang.psi.JetFile; 029 import org.jetbrains.jet.plugin.JetLanguage; 030 031 import java.io.File; 032 import java.io.IOException; 033 import java.util.ArrayList; 034 import java.util.List; 035 036 public final class JetFileUtils { 037 038 private JetFileUtils() { 039 } 040 041 @NotNull 042 private static String loadFile(@NotNull String path) throws IOException { 043 return doLoadFile(path); 044 } 045 046 @NotNull 047 private static String doLoadFile(@NotNull String path) throws IOException { 048 String text = FileUtil.loadFile(new File(path), CharsetToolkit.UTF8).trim(); 049 text = StringUtil.convertLineSeparators(text); 050 return text; 051 } 052 053 @NotNull 054 public static JetFile createPsiFile(@NotNull String name, 055 @NotNull String text, 056 @NotNull Project project) { 057 String fileName = name.endsWith(".kt") ? name : name + ".jet"; 058 return (JetFile) createFile(fileName, text, project); 059 } 060 061 @NotNull 062 private static JetFile loadPsiFile(@NotNull String name, @NotNull Project project) { 063 try { 064 return createPsiFile(name, loadFile(name), project); 065 } catch (IOException e) { 066 throw new RuntimeException(e); 067 } 068 } 069 070 @NotNull 071 private static PsiFile createFile(@NotNull String name, @NotNull String text, @NotNull Project project) { 072 LightVirtualFile virtualFile = new LightVirtualFile(name, JetLanguage.INSTANCE, text); 073 virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET); 074 PsiFile result = ((PsiFileFactoryImpl) PsiFileFactory.getInstance(project)) 075 .trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false); 076 assert result != null; 077 return result; 078 } 079 080 081 @NotNull 082 public static List<JetFile> createPsiFileList(@NotNull List<String> inputFiles, 083 @NotNull Project project) { 084 List<JetFile> psiFiles = new ArrayList<JetFile>(); 085 for (String inputFile : inputFiles) { 086 psiFiles.add(loadPsiFile(inputFile, project)); 087 } 088 return psiFiles; 089 } 090 }