How to get "active editor" in Eclipse plugin?
Asked Answered
S

3

13

In my Eclipse plugin, I need to know when the editor that is visible on the screen has changed. I am currently getting the active editor as follows:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor()

This works for most cases except for when the green Continue button is pressed:

Debugger buttons

If I use the F8 shortcut then the active editor is updated as expected.

It seems that the active editor property is not updated until the editor tab gets focus (which doesn't happen when the Continue button is pressed).

Is there any other route that I can take to get the "visible editor"?

Thanks in advance.

Alan

Swithbart answered 19/2, 2012 at 11:30 Comment(2)
Given the discussion below, I assume your plug-in is an extension to the existing debug plug-in. Correct?Zaxis
It is not an extension to the debug plugin but I do hook into various debug events about breakpoints etc.Swithbart
R
2
  1. An editor is active only when it has focus, so what you are getting is the right output by the API. The user of your plugin will not be running it in debug mode, so not a concern for the end user
  2. Alternatively, to get all open editors you can do the following:

PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()

Ria answered 19/2, 2012 at 12:9 Comment(3)
Thanks for your reply. I was actually referring to the user running the debugger in their copy of Eclipse, so it is an issue for the end user.Swithbart
its highly unlikely that the use has a breakpoint in your code so that the execution is stalled at that point right?Ria
I'm talking about the user's own code. I want to do things as the user is stepping through the debugger in their code. This particular scenario just doesn't work as I would like it to. If I had access to the visible editor then I can deal with it.Swithbart
T
1

The IWorkbenchPage interface has anisPartVisible()` method which indicates if the specified part is visible. The result does not depend on whether the specified part is currently active, i.e. has focus, or not.

To find a visible but currently not active editor it may however not be sufficient to simply call this method on the active workbench page. Instead you might have to iterate over all workbench windows and check the visibility of your editor on the page of each of them.

Tay answered 27/10, 2013 at 23:43 Comment(0)
M
0

The question is similar to the question posted in the link below. One way to achieve this is to keep track of which editor was previously opened by creating a Part Listener. Eclipse Plugin - How to get the last worked on editor

Milliemillieme answered 2/5, 2014 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.