2014-08-27 6 views
1

내가 같은 클래스 이름을 가지고 가정 텍스트 편집기에서 지정된 클래스의 소스 파일로 이동 텍스트 편집기에서 해당 클래스를 선택하십시오. 지금까지 내가 편집기에서 파일을 여는 방법을 알고 : 나는 또한 모듈의 소스 루트를 확보하는 방법을 알고IntellijIdea 플러그인 DEV는 :</p> <p>org.myPackage.MyClass</p> <p>내가의 소스 파일을 탐색 할 :

FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); 
VirtualFile vf = LocalFileSystem.getInstance().findFileByPath(myPath); 
fileEditorManager.openFile(vf, true, true); 

, 그래서 나는 지금까지 무엇을하고 있어요 같은 것으로 MYPATH을 설정하는 것입니다 :

myPath = mainModuleSourceRoot + substituteDotsForSlash("org.myPackage.MyClass") 

그러나 나는 더 "IntellijIdea - 플러그인 지향"주어진 클래스의 소스 파일을 여는 (쉽게, 아마도 더 강력한) 방법이 있는지 알고 싶습니다.

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


편집 대답을 나는 마침내 같은했다

:

답변

0

나는 이런 식으로 일을 할 수 있었다
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; 
    }