假设我有一个类名:
org.myPackage.MyClass
我想在文本编辑器中导航到该类的源文件。到目前为止,我知道如何在编辑器中打开文件:
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(myPath);
fileEditorManager.openFile(vf, true, true);
我也知道如何获取模块的源根,所以我到目前为止所做的就是将myPath设置为:
myPath = mainModuleSourceRoot + substituteDotsForSlash("org.myPackage.MyClass")
但是我想知道是否有更多的" IntellijIdea-Plugin导向" (更简单,也许更强大)打开给定类的源文件的方式。
答案 0 :(得分:0)
我能够这样做:
GlobalSearchScope scope = GlobalSearchScope.allScope(project);
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass("org.myPackage.MyClass", scope);
if ( psiClass != null ) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
fileEditorManager.openFile(psiClass.getContainingFile().getVirtualFile(), true, true);
} else {
//handle the class not found
}
在这里找到答案:https://code.google.com/p/ide-examples/wiki/IntelliJIdeaPsiCookbook#Find_a_Class
编辑回答
我终于做了类似的事情:
GlobalSearchScope scope = GlobalSearchScope.allScope(project);
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
if (psiClass != null) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
//Open the file containing the class
VirtualFile vf = psiClass.getContainingFile().getVirtualFile();
//Jump there
new OpenFileDescriptor(project, vf, 1, 0).navigateInEditor(project, false);
} else {
//Handle file not found here....
return;
}