【问题标题】:Eclipse Plugin Development: How to access code written in the eclipse editorEclipse 插件开发:如何访问在 Eclipse 编辑器中编写的代码
【发布时间】:2018-06-06 11:53:17
【问题描述】:

我正在制作一个需要访问在 Eclipse 编辑器中编写的代码的 Eclipse 插件。我已经按照链接中提到的过程进行了操作。 Accessing Eclipse editor code 但它在消息框中显示文件路径而不是代码。 IEditorEditor 类的 getEditorInput() 没有按照链接执行它应该执行的操作。这是我的代码。请帮我找出我做错了什么。

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);

    IEditorPart editor = ((IWorkbenchPage) PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getActivePage()).getActiveEditor();

    IEditorInput input = (IEditorInput) editor.getEditorInput();     // accessing code from eclipse editor

    String code = input.toString();

    MessageDialog.openInformation(
            window.getShell(),
            "Project",
            code);

    return null;
}

这是输出的快照。

【问题讨论】:

标签: java eclipse-plugin code-editor


【解决方案1】:

您可以通过两种不同的方式执行此操作。无论内容是否由磁盘上的文件支持,这种方式都有效。

此方法从编辑器中获取文本IDocument,这是大多数用途中存储和访问内容的方式。 StyledText 是一个小部件,除非您使用小部件和控件做某事,否则它不是正确的方法。为此,您将从编辑器部分进入 ITextEditor 界面,然后使用 @ 987654324@ 与当前编辑器输入。这将跳过您想要事先进行的instanceof 检查,以及如果这是MultiPageEditorPart 中的一个页面,您可能需要做的任何事情(没有处理这些的标准方法)。

org.eclipse.jface.text.IDocument document = 
    ((org.eclipse.ui.texteditor.ITextEditor)editor).
    getDocumentProvider().
    getDocument(input);

您可以通过IDocument获取和修改内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多