VirtualFileFinder update to search files without kotlin headers

This commit is contained in:
Mikhael Bogdanov
2014-02-26 11:44:21 +04:00
parent 629a07c748
commit dabacd8cae
4 changed files with 54 additions and 8 deletions
@@ -98,7 +98,7 @@ public class InlineCodegenUtil {
@Nullable
private static VirtualFile findVirtualFile(@NotNull Project project, @NotNull FqName containerFqName) {
VirtualFileFinder fileFinder = ServiceManager.getService(project, VirtualFileFinder.class);
VirtualFile virtualFile = fileFinder.find(containerFqName);
VirtualFile virtualFile = fileFinder.findVirtualFile(containerFqName);
if (virtualFile == null) {
return null;
}
@@ -36,7 +36,26 @@ public class CliVirtualFileFinder extends VirtualFileKotlinClassFinder implement
@Override
public VirtualFile findVirtualFile(@NotNull FqName className) {
for (VirtualFile root : classPath) {
VirtualFile fileInRoot = findFileInRoot(className.asString(), root);
VirtualFile fileInRoot = findKotlinFile(className, root);
if (fileInRoot != null) {
return fileInRoot;
}
}
return null;
}
private VirtualFile findKotlinFile(@NotNull FqName className, @NotNull VirtualFile root) {
VirtualFile vFile = findFileInRoot(className.asString(), root, '.');
if (vFile != null && createKotlinClass(vFile).getClassHeader() != null) {
return vFile;
}
return null;
}
@Override
public VirtualFile findVirtualFile(@NotNull String internalName) {
for (VirtualFile root : classPath) {
VirtualFile fileInRoot = findFileInRoot(internalName, root, '/');
if (fileInRoot != null) {
return fileInRoot;
}
@@ -46,12 +65,12 @@ public class CliVirtualFileFinder extends VirtualFileKotlinClassFinder implement
//NOTE: copied with some changes from CoreJavaFileManager
@Nullable
private VirtualFile findFileInRoot(@NotNull String qName, @NotNull VirtualFile root) {
private static VirtualFile findFileInRoot(@NotNull String qName, @NotNull VirtualFile root, char separator) {
String pathRest = qName;
VirtualFile cur = root;
while (true) {
int dot = pathRest.indexOf('.');
int dot = pathRest.indexOf(separator);
if (dot < 0) break;
String pathComponent = pathRest.substring(0, dot);
@@ -69,11 +88,9 @@ public class CliVirtualFileFinder extends VirtualFileKotlinClassFinder implement
//TODO: log
return null;
}
//NOTE: currently we use VirtualFileFinder to find Kotlin binaries only
if (createKotlinClass(vFile).getClassHeader() != null) {
return vFile;
}
return vFile;
}
return null;
}
}
@@ -34,4 +34,8 @@ public interface VirtualFileFinder extends KotlinClassFinder {
// TODO: support scope
@Nullable
VirtualFile findVirtualFile(@NotNull FqName className);
/*Also finds files without kotlin header*/
@Nullable
VirtualFile findVirtualFile(@NotNull String internalName);
}
@@ -16,9 +16,12 @@
package org.jetbrains.jet.plugin.vfilefinder;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.impl.file.impl.JavaFileManager;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import org.jetbrains.annotations.NotNull;
@@ -52,4 +55,26 @@ public final class IDEVirtualFileFinder extends VirtualFileKotlinClassFinder imp
}
return files.iterator().next();
}
@Override
public VirtualFile findVirtualFile(@NotNull String internalName) {
JavaFileManager fileFinder = ServiceManager.getService(project, JavaFileManager.class);
String qName = internalName.replace('/', '.');
PsiClass psiClass = fileFinder.findClass(qName, GlobalSearchScope.allScope(project));
if (psiClass == null) {
int dollarIndex = qName.indexOf('$');
assert dollarIndex > 0 : "Only inner classes could be found with this patch: " + internalName;
String newName = qName.substring(0, dollarIndex);
psiClass = fileFinder.findClass(newName, GlobalSearchScope.allScope(project));
if (psiClass != null) {
int i = qName.lastIndexOf('.');
return psiClass.getContainingFile().getVirtualFile().getParent().findChild(qName.substring(i + 1) + ".class");
}
}
if (psiClass != null) {
return psiClass.getContainingFile().getVirtualFile();
}
return null;
}
}