Kiva
Kiva

Reputation: 9353

Get contents of editor

I am trying to develop a simple Eclipse plugin to understand how it works.

I have two questions about this:

How can I get the content of the active editor?

Do you have a good documentation about life cycle plugin and co? I can't find real good documentation on Google.

Upvotes: 5

Views: 3796

Answers (3)

John Tang Boyland
John Tang Boyland

Reputation: 1088

Tonny Madsen's answer is fine, but perhaps a little more transparent (getAdapter() is very opaque) is something like:

public String getCurrentEditorContent() {
    final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
        .getActiveEditor();
    if (!(editor instanceof ITextEditor)) return null;
    ITextEditor ite = (ITextEditor)editor;
    IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
    return doc.get();
}

Upvotes: 11

Tonny Madsen
Tonny Madsen

Reputation: 12718

Regarding the content of the current editor, there are several ways to do this. The following code is not tested:

public String getCurrentEditorContent() {
    final IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
            .getActiveEditor();
    if (activeEditor == null)
        return null;
    final IDocument doc = (IDocument) activeEditor.getAdapter(IDocument.class);
    if (doc == null) return null;

    return doc.get();
}

Upvotes: 8

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

I'm assuming you're already familiar with using Eclipse as an IDE.

  • Create a new plug-in project using the New Plug-in Project Wizard.
  • On the Templates panel, choose "Plug-in with an editor"
  • Read the generated code

If you're serious about writing Eclipse plug-ins, the book, "Eclipse Plug-ins" by Eric Clayberg and Dan Rubel is invaluable. I couldn't make sense of the eclipse.org write-ups until after I read the book.

Upvotes: 2

Related Questions