0

I'm struggling with developing an Eclipse plugin. In general, you should be able to select some lines of Java code in the Java editor, then click a button and do something with the selected code.

How can I actually access this particular Java editor and the selection from the plugin? As a skeleton, I'm using this simple Hello World example, which adds a button to the toolbar, popping up a dialog box when clicking it.

The only thing I have is an instance of IWorkbenchWindow. Using this API, I will get an IEditorPart calling window.getActivePage().getActiveEditor(), which seems to be pretty useless for my purpose ...

Any help is highly appreciated

Cheers -Frank

2 Answers2

0

You can use window.getSelectionService().getSelection(), which gives you an ISelection object representing the active selection in the active part. Check what type of ISelection this returns you and act accordingly.

vainolo
  • 6,907
  • 4
  • 24
  • 47
0

You should be able to do something like this: ((JavaEditor) window.getActivePage().getActiveEditor()).getSelectionProvider().getSelection()

However, it sounds like you are trying to invoke an IAction based on a selection. Presumably this is registered via an editorActions extension point (or something similar). If so, I would have a look at the SelectionDispatchAction class, which dispatches actions based on the current selection (as the name implies). This class is internal API, but you may still find it useful.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148