8

I'm developing an Eclipse plug-in where upon pressing a button, the plug-in takes the selected text in the Java editor and puts in a text box which appears.

My code looks like this: I got it from here: http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg02200.html

private ITextSelection getSelection(ITextEditor editor) {
     ISelection selection = editor.getSelectionProvider()
            .getSelection();
     return (ITextSelection) selection;
}

private String getSelectedText(ITextEditor editor) {
     return getSelection(editor).getText();
}

The problem is how will I get the ITextEditor of the Java editor being displayed. Coincidentally it's the next question in the thread in the link I posted but it's unanswered :(

Lii
  • 11,553
  • 8
  • 64
  • 88
Krt_Malta
  • 9,265
  • 18
  • 53
  • 91
  • When you say, "selected text," do you mean actually highlighted, or merely the current keyword that the cursor is on? – StockB Feb 13 '13 at 17:26

2 Answers2

7

You could ask for the ActiveEditor, as in this thread:

IEditorPart part;

part =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().get
ActiveEditor();

if(part instanceof ITextEditor){
    ITextEditor editor = (ITextEditor)part;
    IDocumentProvider provider = editor.getDocumentProvider();
    IDocument document = provider.getDocument(editor.getEditorInput());

The OP Krt_Malta mentions this blog entry "Programmatically query current text selection", which is similar to this other SO answer (written before the blog entry) "Replace selected code from eclipse editor through plugin command".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • See also SO question http://stackoverflow.com/questions/1694748/adding-item-to-eclipse-text-viewer-context-menu with a similar problem – VonC Mar 07 '10 at 10:48
  • Thanks :) Just after I submitted the question I decided to give the methods I posted earlier a seach on google just to see if any matches come up. One of the hits was http://usayadis.wordpress.com/2009/10/20/programmatically-query-current-text-selection/ which did exactly what I was looking for. Thanks and regards, Krt_Malta – Krt_Malta Mar 07 '10 at 10:57
1

I'd like to add one thing to VonCs answer. The technique he describes to get the selection is useful for all kinds of text editors, not only Java editors as this questions is about. But his solution does not work in the case that the workspace part is a MultiPageEditorPart, since that is not a ITextEditor.

But in many cases (for example with the standard XML editor) a MultiPageEditorPart has pages which are ITextEditors. In those cases you can get the active page from a MultiPageEditorPart and get the selection from that.

This can be done with the following code:

ITextEditor editor = null;

if (part instanceof ITextEditor) {
    editor = (ITextEditor) part;
} else if (part instanceof MultiPageEditorPart) {
    Object page = ((MultiPageEditorPart) part).getSelectedPage();
    if (page instanceof ITextEditor) editor = (ITextEditor) page;
}

if (editor != null) {
    IDocumentProvider provider = editor.getDocumentProvider();
    IDocument document = provider.getDocument(editor.getEditorInput());
}
Lii
  • 11,553
  • 8
  • 64
  • 88