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
请先登录再写评论。
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!"
Is there a way to get to PsiFile (from VirtualFile)?
PsiManager.getInstance(project).findFile(virtualFile);
Bas
Hello Bas,
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.
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
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.
@... ; Yes i am facing the same issue. I too want the current project which is opened. Kindly let me know how we can use the ProjectManager in this case
I found this -
from here, worked well for me.
1. From the implement of ToolWindowFactory, using
to retrieve project.
2. From the implement of AnAction, using
to retrieve project.
For the future readers: https://plugins.jetbrains.com/docs/intellij/project.html#how-to-get-a-project-instance