I have been attempting to find the location of a selected project in Eclipse via code from my editor plugin and I have had some success. I have not been able to locate the project in one scenario. When a file using the editor was left open from the last session in Eclipse and Eclipse is reopened, I cannot find the location of the current project without opening and closing the file because this method: Eclipse Plugin: how to get the path to the currently selected project will not work. Any suggestions? Thanks in advance.
Asked
Active
Viewed 101 times
1 Answers
1
Eclipse doesn't really have a notion of a 'current project'. There is a current selection in each view but most views don't save the selection between sessions.
In an editor you probably want the project that the file you are current editing belongs to. In the editor you can use something like:
IEditorInput editorInput = getEditorInput();
IFile file = (IFile)editorInput.getAdapter(IFile.class);
IProject project = file.getProject();
Note: file
might be null if you are editing a file which is not in the workspace. You don't need the (IFile)
cast on the most recent versions of Eclipse.

greg-449
- 109,219
- 232
- 102
- 145
-
I have used this method in the past, but I am trying to locate the open file before the editor is actually created, so that I can change the syntax based on user defined parameters. Thank you for your response, but this method will not work in my case. – Forrest Smith Jun 16 '17 at 14:00
-
So where are you actually trying to run this code? There is no such thing as a 'current project' and most views don't save the selection between sessions so there really isn't anything else you can look at. – greg-449 Jun 16 '17 at 14:43