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

0
Avatar
Permanently deleted user

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

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

0

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

Bas

0

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

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
Avatar
Permanently deleted user

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

@... ; 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

0

I found this - 

DataContext dataContext = DataManager.getInstance().getDataContext();
Project project = (Project) dataContext.getData(DataConstants.PROJECT);

from here, worked well for me.

3

1. From the implement of ToolWindowFactory, using

override fun createToolWindowContent(project: Project, toolWindow: ToolWindow)

to retrieve project.

 

2. From the implement of AnAction, using

/**
* @see [com.jetbrains.plugins.remotesdk.console.RunSshConsoleAction.actionPerformed]
*/
override fun actionPerformed(e: AnActionEvent) {
e.getData(CommonDataKeys.PROJECT)?.let { project ->
    TODO("Do something with project...")
}
}

to retrieve project.

 

 

0

请先登录再写评论。