gpt4 book ai didi

java - IntellijIdea 插件开发 : Navigate to the source file of a given class in the text editor

转载 作者:行者123 更新时间:2023-11-29 08:45:45 26 4
gpt4 key购买 nike

假设我有一个像这样的类名:

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”(更简单,也许更健壮)的方法来打开给定类的源文件。

最佳答案

我可以这样做:

    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;
}

关于java - IntellijIdea 插件开发 : Navigate to the source file of a given class in the text editor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25527832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com