Finding and opening a Java source file

Hi all

I'm writing a plugin that will parse a file to find a package and class statement as follows:

$class=my.package.classname

I then want to open the sourcefile that corresponds with the particular class.

I am currently doing the following:


This works as long as the source file can be found in my source directory. However, the source could potentially be found a source directory defined in a library (global or project).

Do I need to loop through all of the VirtualFileSystems looking for the file, or is there a better way?

Thanks

Todd Breiholz

3 comments
Comment actions Permalink

Just replace

with


since findClass() will return .class version if found in library and
getNavigationElement() will convert it to it's source equivalent if
avaliable.

--

Best regards,
Maxim Shafirov
JetBrains, Inc / IntelliJ Software
http://www.intellij.com
"Develop with pleasure!"


"Todd Breiholz" <no_mail@jetbrains.com> wrote in message
news:2583560.1062718998612.JavaMail.itn@is.intellij.net...

Hi all

>

I'm writing a plugin that will parse a file to find a package and class

statement as follows:
>

$class=my.package.classname

>

I then want to open the sourcefile that corresponds with the particular

class.
>

I am currently doing the following:

>

     private void openFile(String className) {
>         PsiClass aClass =
PsiManager.getInstance(project).findClass(className);
>         PsiFile containingFile = aClass.getContainingFile();
>         containingFile.getVirtualFile();
>         FileEditorManager fileEditorManager =
FileEditorManager.getInstance(project);
>         OpenFileDescriptor ofd = new
OpenFileDescriptor(containingFile.getVirtualFile(), 0);
>         fileEditorManager.openFile(ofd, ScrollType.CENTER, true);
>     }
> ]]>

>

This works as long as the source file can be found in my source directory.

However, the source could potentially be found a source directory defined in
a library (global or project).
>

Do I need to loop through all of the VirtualFileSystems looking for the

file, or is there a better way?
>

Thanks

>

Todd Breiholz



0
Comment actions Permalink

If i can understand you, you want to make somethink like that:
http://www.intellij.org/twiki/bin/view/Main/FileAssociations

It have sources and you can see how open it...

Thanks!

--
Alexey Efimov, Software Engineer
Sputnik Labs,
http://www.spklabs.com
"Todd Breiholz" <no_mail@jetbrains.com> wrote in message
news:2583560.1062718998612.JavaMail.itn@is.intellij.net...

Hi all

>

I'm writing a plugin that will parse a file to find a package and class

statement as follows:
>

$class=my.package.classname

>

I then want to open the sourcefile that corresponds with the particular

class.
>

I am currently doing the following:

>

     private void openFile(String className) {
>         PsiClass aClass =
PsiManager.getInstance(project).findClass(className);
>         PsiFile containingFile = aClass.getContainingFile();
>         containingFile.getVirtualFile();
>         FileEditorManager fileEditorManager =
FileEditorManager.getInstance(project);
>         OpenFileDescriptor ofd = new
OpenFileDescriptor(containingFile.getVirtualFile(), 0);
>         fileEditorManager.openFile(ofd, ScrollType.CENTER, true);
>     }
> ]]>

>

This works as long as the source file can be found in my source directory.

However, the source could potentially be found a source directory defined in
a library (global or project).
>

Do I need to loop through all of the VirtualFileSystems looking for the

file, or is there a better way?
>

Thanks

>

Todd Breiholz



0
Comment actions Permalink

Thanks Maxim

That works perfectly!

Todd Breiholz

0

Please sign in to leave a comment.