获取Eclipse编辑器的当前源代码? [英] Get the current source code of an Eclipse editor?
问题描述
我正在使用Eclipse IDE。首先,我想开发一个插件,这个插件使我可以将整个源代码作为一个简单的字符串来获取。
I am working with Eclipse IDE. I want to develop a plug-in first of all, this plug-in allows me to get the whole source code as a simple string.
我做了一个简单的你好 world插件,使用模板 hello world command。现在,我正在搜索以从Eclipse编辑器获取源代码,并使用System.out.println();进行显示。
I made a simple "hello the world" plug-in using the template "hello world command". Now I am searching to get the source code from the editor of Eclipse and display it with System.out.println(); statement instead of showing HELLO THE WORLD.
我尝试过此操作,但它仅向我显示了项目的层次结构顺序,即packageName / src / nameOfClass。
I tried this but it shows me only the hierarchical sequence of my project, packageName/src/nameOfClass.
System.out.println(
Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getEditorInput()
);
我的目标是获取类本身的源代码(公共类nameOfClass {***} )。
My goal is to get the source code of the class itself (public class nameOfClass{ *** }).
推荐答案
首先不要使用 Workbench
,这是内部类,不得使用。使用 PlatformUI
获取工作台。
First do not use Workbench
this is an internal class and must not be used. Use PlatformUI
to get the workbench.
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editor instanceof ITextEditor)
{
ITextEditor textEditor = (ITextEditor)editor;
IDocumentProvider provider = textEditor.getDocumentProvider();
IEditorInput input = editor.getEditorInput();
IDocument document = provider.getDocument(input);
String text = document.get();
...
}
注意:并非所有编辑器是文本编辑器,因此需要进行检查(上面的 ITextEditor
实例检查)。
Note: Not all editors are text editors so this needs to be checked (the ITextEditor
instance check above).
这篇关于获取Eclipse编辑器的当前源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!