Get current Project, current file in editor

Hi there,
anyone can tell me how to get the current project and the current file in the editor.

-


I'm using to get the project:

Project p=ProjectManager.getInstance().getOpenProjects()[0];

-


And for the editor:

FileEditorManager manager = FileEditorManager.getInstance(ModificationsPlugin.myProject);

VirtualFile files[] = manager.getSelectedFiles();

and then the files[0] is the current one.


The problem with the project is when i have more than one Intellij instance running.
The problem with the editor file is that it works with Idea 4.5 but in 5.0 has some problems(only when i close the project and open again it works).

Any ideas? Thanks in advance.

Raul

6 comments
Comment actions Permalink

Hello Raul,

R> Hi there,
R> anyone can tell me how to get the current project and the current
R> file in the editor.
R> -


R> I'm using to get the project:
R> Project p=ProjectManager.getInstance().getOpenProjects()[0];
R>
R> -


R> And for the editor:
R> FileEditorManager manager =
R> FileEditorManager.getInstance(ModificationsPlugin.myProject);
R>
R> VirtualFile files[] = manager.getSelectedFiles();
R>
R> and then the files[0] is the current one.
R>
R> The problem with the project is when i have more than one Intellij
R> instance running.

If your code is running in an action, the recommended APIs to use are:
AnActionEvent.getDataContext().getData(DataConstants.PROJECT)
AnActionEvent.getDataContext().getData(DataConstants.VIRTUAL_FILE)

Also, if you have an Editor instance, you can call Editor.getProject() to
get the project, and FileDocumentManager.getInstance().getFile(Editor.getDocument())
to get the VirtualFile for the document open in the editor.

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0
Comment actions Permalink

Is there a way to get to PsiFile (from VirtualFile)?

0
Comment actions Permalink

PsiManager.getInstance(project).findFile(virtualFile);

Bas

0
Comment actions Permalink

Hello Bas,

PsiManager.getInstance(project).findFile(virtualFile);


Thanks again ;)

Do you have any general advise for a new openapi developer? (plugin sources
to look at, packages to explore etc)
I really appreciate your answers, but would hate to keep asking everything.


0
Comment actions Permalink

I can only give some very general advice:

  • Many things are controlled by "managers", Ctrl+N "*Manager" can be useful sometimes

  • Many times typing the desired method name in CtrlAltShift+N can find the classes you need.

  • Some useful information still can be found on www.intellij.org

  • Use the forum search, many answers can be found in the openapi forum already.


Bas

0
Comment actions Permalink

Just for your information, ProjectManager.getInstance().getOpenProjects()[0] returns the first opened project rather that the currently opened project, that is, if we open several projects one by one, the code will return the first project we open.

0

Please sign in to leave a comment.