Refactor dependencies, so jet-as-java might be used in cli.

This commit is contained in:
Maxim Shafirov
2012-03-29 21:18:35 +04:00
parent b29c31ad2d
commit 060be0a8f8
37 changed files with 263 additions and 140 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
<orderEntry type="module" module-name="backend" />
<orderEntry type="module" module-name="frontend" />
<orderEntry type="module" module-name="frontend.java" />
<orderEntry type="library" name="idea-full" level="project" />
<orderEntry type="library" scope="PROVIDED" name="intellij-core" level="project" />
</component>
</module>
@@ -32,6 +32,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.codegen.JetTypeMapper;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.FqName;
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
import org.jetbrains.jet.util.QualifiedNamesUtil;
@@ -51,7 +52,7 @@ public class JavaElementFinder extends PsiElementFinder {
psiManager = PsiManager.getInstance(project);
// Monitoring for files instead of collecting them each time
VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileAdapter() {
VirtualFileManager.getInstance().addVirtualFileListener(new VirtualFileListener() {
@Override
public void fileCreated(VirtualFileEvent event) {
invalidateJetFilesCache();
@@ -71,6 +72,24 @@ public class JavaElementFinder extends PsiElementFinder {
public void fileCopied(VirtualFileCopyEvent event) {
invalidateJetFilesCache();
}
@Override
public void propertyChanged(VirtualFilePropertyEvent event) {}
@Override
public void contentsChanged(VirtualFileEvent event) {}
@Override
public void beforePropertyChange(VirtualFilePropertyEvent event) {}
@Override
public void beforeContentsChange(VirtualFileEvent event) {}
@Override
public void beforeFileDeletion(VirtualFileEvent event) {}
@Override
public void beforeFileMovement(VirtualFileMoveEvent event) {}
});
}
@@ -230,7 +249,7 @@ public class JavaElementFinder extends PsiElementFinder {
List<JetFile> cachedFiles = jetFiles.get(scope);
if (cachedFiles == null) {
cachedFiles = JetFileUtil.collectJetFiles(project, scope);
cachedFiles = JetFilesProvider.getInstance(project).allInScope(scope);
jetFiles.put(scope, cachedFiles);
}
@@ -1,67 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.asJava;
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetFileType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author Nikolay Krasko
*/
public final class JetFileUtil {
private JetFileUtil() {}
public static List<JetFile> collectJetFiles(final Project project, @NotNull final GlobalSearchScope scope) {
final List<JetFile> answer = new ArrayList<JetFile>();
final FileTypeManager fileTypeManager = FileTypeManager.getInstance();
List<VirtualFile> contentRoots = Arrays.asList(ProjectRootManager.getInstance(project).getContentRoots());
final PsiManager manager = PsiManager.getInstance(project);
CompilerPathsEx.visitFiles(contentRoots, new CompilerPathsEx.FileVisitor() {
@Override
protected void acceptFile(VirtualFile file, String fileRoot, String filePath) {
final FileType fileType = fileTypeManager.getFileTypeByFile(file);
if (fileType != JetFileType.INSTANCE) return;
if (scope.accept(file)) {
final PsiFile psiFile = manager.findFile(file);
if (psiFile instanceof JetFile) {
answer.add((JetFile) psiFile);
}
}
}
});
return answer;
}
}
@@ -50,9 +50,9 @@ import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.resolve.FqName;
import org.jetbrains.jet.lang.resolve.java.AnalyzeExhaust;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.lang.resolve.java.JetFilesProvider;
import org.jetbrains.jet.lang.resolve.java.JetJavaMirrorMarker;
import org.jetbrains.jet.plugin.JetLanguage;
import org.jetbrains.jet.plugin.compiler.WholeProjectAnalyzerFacade;
import org.jetbrains.jet.util.QualifiedNamesUtil;
import javax.swing.*;
@@ -158,7 +158,8 @@ public class JetLightClass extends AbstractLightClass implements JetJavaMirrorMa
// The context must reflect _all files in the module_. not only the current file
// Otherwise, the analyzer gets confused and can't, for example, tell which files come as sources and which
// must be loaded from .class files
AnalyzeExhaust context = AnalyzerFacadeForJVM.shallowAnalyzeFiles(WholeProjectAnalyzerFacade.WHOLE_PROJECT_DECLARATION_PROVIDER.fun(file));
AnalyzeExhaust context = AnalyzerFacadeForJVM.shallowAnalyzeFiles(
JetFilesProvider.getInstance(project).sampleToAllFilesInModule().fun(file));
final GenerationState state = new GenerationState(project, builderFactory, context, Collections.singletonList(file)) {
@Override
@@ -1,101 +0,0 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.plugin.compiler;
import com.google.common.collect.Sets;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.asJava.JetFileUtil;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.AnalyzeExhaust;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.plugin.JetFileType;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* @author abreslav
*/
public final class WholeProjectAnalyzerFacade {
/** Forbid creating */
private WholeProjectAnalyzerFacade() {}
/**
* Will collect all root-namespaces in all kotlin files in the project.
*/
public static final Function<JetFile, Collection<JetFile>> WHOLE_PROJECT_DECLARATION_PROVIDER = new Function<JetFile, Collection<JetFile>>() {
@Override
public Collection<JetFile> fun(final JetFile rootFile) {
final Project project = rootFile.getProject();
final Set<JetFile> files = Sets.newLinkedHashSet();
Module rootModule = ModuleUtil.findModuleForPsiElement(rootFile);
if (rootModule != null) {
Set<Module> allModules = new HashSet<Module>();
ModuleUtil.getDependencies(rootModule, allModules);
for (Module module : allModules) {
ModuleRootManager.getInstance(module).getFileIndex().iterateContent(new ContentIterator() {
@Override
public boolean processFile(VirtualFile file) {
if (file.isDirectory()) return true;
final FileType fileType = FileTypeManager.getInstance().getFileTypeByFile(file);
if (fileType != JetFileType.INSTANCE) return true;
PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
if (psiFile instanceof JetFile) {
if (rootFile.getOriginalFile() != psiFile) {
files.add((JetFile) psiFile);
}
}
return true;
}
});
}
}
files.add(rootFile);
return files;
}
};
@NotNull
public static AnalyzeExhaust analyzeProjectWithCacheOnAFile(@NotNull JetFile file) {
return AnalyzerFacadeForJVM.analyzeFileWithCache(file, WHOLE_PROJECT_DECLARATION_PROVIDER);
}
@NotNull
public static AnalyzeExhaust analyzeProjectWithCache(@NotNull Project project, @NotNull GlobalSearchScope scope) {
return AnalyzerFacadeForJVM.analyzeProjectWithCache(project, JetFileUtil.collectJetFiles(project, scope));
}
}