获取编辑器的内容

Kiv*_*iva 4 java eclipse eclipse-plugin

我正在尝试开发一个简单的Eclipse插件来了解它是如何工作的.

我有两个问题:

如何获取活动编辑器的内容?

你有关于生命周期插件和co的良好文档吗?我在Google上找不到真正好的文档.

Joh*_*and 11

Tonny Madsen的答案很好,但也许更透明(getAdapter()非常不透明)类似于:

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();
}
Run Code Online (Sandbox Code Playgroud)


Ton*_*sen 8

关于当前编辑器的内容,有几种方法可以做到这一点.以下代码未经过测试:

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();
}
Run Code Online (Sandbox Code Playgroud)