本文介绍了Eclipse插件开发:如何访问在eclipse编辑器中编写的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在创建一个Eclipse插件,需要访问Eclipse编辑器中编写的代码。我已经按照链接中提到的过程进行了操作。

解决方案

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



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

  org.eclipse.jface.text.IDocument document = 
((org.eclipse.ui.texteditor.ITextEditor)编辑)。
getDocumentProvider()。
getDocument(输入);

您可以通过 IDocument


I'm making an Eclipse plug-in that requires access to code written in the Eclipse editor. I've followed the process mentioned in the link.Accessing Eclipse editor codeBut it's showing filepath instead of code in the message box. The getEditorInput() of IEditorEditor class isn't doing what it should do according to the link. Here's my code. Please help me find what i'm doing wrong.

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;
}

And here's snap of the output.

解决方案

You can do this two different ways. This way works regardless of whether the contents are backed by a file on disk or not.

This method gets the text IDocument from the editor, which is how the contents are stored and accessed for most uses. StyledText is a widget, and unless you are doing something with Widgets and Controls, it's not the right way in. For that you're going to go from the editor part, through the ITextEditor interface, and then use the IDocumentProvider with the current editor input. This is skipping the instanceof check you'd want to do beforehand, as well as anything you might have to do if this is a page in a MultiPageEditorPart (there's no standard way for handling those).

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

You can get, and modify, the contents through the IDocument.

这篇关于Eclipse插件开发:如何访问在eclipse编辑器中编写的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:54