How to open a newly created class

Using the Open API I've created a new Java class in the current project. I have a PsiFile object that represents this new class. I would now like to open this new class in the editor. How do I do this?

4 comments
Comment actions Permalink

psiFile.navigate(true);

Bas

0
Comment actions Permalink

So something like this?

*final PsiDirectory psiDirectory = psiManager.findDirectory(currentFile);
final PsiClass psiFile = psiManager.getElementFactory().createClass("NewClass");
psiDirectory.add(psiFile);
psiFile.navigate(true);*

Unfortunately this doesn't work for me, it gives an exception...

And psiFile.canNavigate() returns false

What am I missing?

0
Comment actions Permalink

Hello Steve,

SM> So something like this?
SM>
SM> *final PsiDirectory psiDirectory =
SM> psiManager.findDirectory(currentFile);
SM> final PsiClass psiFile =
SM> psiManager.getElementFactory().createClass("NewClass");
SM> psiDirectory.add(psiFile);
SM> psiFile.navigate(true);*
SM> Unfortunately this doesn't work for me, it gives an exception...
SM>
SM> And psiFile.canNavigate() returns false
SM>
SM> What am I missing?

psiFile = psiDirectory.add(psiFile);

It will return a different instance.

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Aha! That did the trick.

For the sake of the next person searching for a solution to the same problem, I had to cast the returned instance as follows:

*PsiClass createdClass = (PsiClass) psiDirectory.add(psiFile);
createdClass.navigate(true);*

0

Please sign in to leave a comment.