FileEditorManager and VirtualFile

Hi,

I'm using 4.0.1 of IDEA. I'm trying to get the class name of a
selectedTextEditor. However, it appears that the getSelectedFile on
FileEditorManager is no longer available, only getSelectedFiles which
returns an array of VirtualFile(s). It strikes me as a bit of a kludge
to get vFile[0] to get the selected file and hence the VirtualFile. Is
there a better way?

-=david=-

4 comments

David Harrigan wrote:

Hi,

I'm using 4.0.1 of IDEA. I'm trying to get the class name of a
selectedTextEditor. However, it appears that the getSelectedFile on
FileEditorManager is no longer available, only getSelectedFiles which
returns an array of VirtualFile(s). It strikes me as a bit of a kludge
to get vFile[0] to get the selected file and hence the VirtualFile. Is
there a better way?

-=david=-


Actually, I should add that I'm trying to get the FQN of the class,
e.g., a.b.c.ClassName

-=david=-

0

Hi,

This looks like following:

Editor editor =
FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor!=null) {
PsiFile psifile = PsiDocumentManager.getInstance(project).getPsiFile(
editor.getDocument()
);
if (psifile instanceof PsiJavaFile) {
PsiClass[] clazzes = ((PsiJavaClass)psifile).getClasses();
if (clazzes.length > 0) {
// note, there could be more then one class in the file
// one needs to find public class or class with name = name of
file w/o extension
// so adjust the index accordingly
System.out.println( clazzes[0].getQualifiedName() );
}
}
}

David Harrigan wrote:

David Harrigan wrote:

>> Hi,
>>
>> I'm using 4.0.1 of IDEA. I'm trying to get the class name of a
>> selectedTextEditor. However, it appears that the getSelectedFile on
>> FileEditorManager is no longer available, only getSelectedFiles which
>> returns an array of VirtualFile(s). It strikes me as a bit of a kludge
>> to get vFile[0] to get the selected file and hence the VirtualFile. Is
>> there a better way?
>>
>> -=david=-


Actually, I should add that I'm trying to get the FQN of the class,
e.g., a.b.c.ClassName

-=david=-



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Please replace PsiJavaClass with PsiJavaFile in the code

Maxim Mossienko wrote:

Hi,

This looks like following:

Editor editor =
FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor!=null) {
PsiFile psifile = PsiDocumentManager.getInstance(project).getPsiFile(
editor.getDocument()
);
if (psifile instanceof PsiJavaFile) {
PsiClass[] clazzes = ((PsiJavaClass)psifile).getClasses();
if (clazzes.length > 0) {
// note, there could be more then one class in the file
// one needs to find public class or class with name = name of
file w/o extension
// so adjust the index accordingly
System.out.println( clazzes[0].getQualifiedName() );
}
}
}

David Harrigan wrote:

>> David Harrigan wrote:
>>
>>> Hi,
>>>
>>> I'm using 4.0.1 of IDEA. I'm trying to get the class name of a
>>> selectedTextEditor. However, it appears that the getSelectedFile on
>>> FileEditorManager is no longer available, only getSelectedFiles which
>>> returns an array of VirtualFile(s). It strikes me as a bit of a
>>> kludge to get vFile[0] to get the selected file and hence the
>>> VirtualFile. Is there a better way?
>>>
>>> -=david=-
>>
>>
>>
>> Actually, I should add that I'm trying to get the FQN of the class,
>> e.g., a.b.c.ClassName
>>
>> -=david=-




--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Maxim Mossienko wrote:

Hi,

This looks like following:

Editor editor =
FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor!=null) {
PsiFile psifile = PsiDocumentManager.getInstance(project).getPsiFile(
editor.getDocument()
);
if (psifile instanceof PsiJavaFile) {
PsiClass[] clazzes = ((PsiJavaClass)psifile).getClasses();
if (clazzes.length > 0) {
// note, there could be more then one class in the file
// one needs to find public class or class with name = name of
file w/o extension
// so adjust the index accordingly
System.out.println( clazzes[0].getQualifiedName() );
}
}
}


Thank you! :)

This is more like we need :) More help from IntelliJ for people writing
plugins will spur on the development of more and more plugins :) It's a
virtuous cycle..(as opposed to a vicious cycle...)

-=david=-

0

Please sign in to leave a comment.