如何从Eclipse PDE中的活动编辑器获取文本 [英] How to get text from active editor in Eclipse PDE
本文介绍了如何从Eclipse PDE中的活动编辑器获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个处理程序,我想在该处理程序中从工作台中的活动编辑器获取文本。从下面的屏幕快照中,我想将所有内容都保存在Test.java(公共类Test ...)中。
I have a handler in which I would like to get the text from the active editor in my workbench. From the screenshot below, I would like to get everything inside of Test.java ("public class Test...").
我已成功在源菜单下添加了新命令。只是不确定现在从何处从活动编辑器获取文本。到目前为止,这是我尝试获取文本的内容(它只是在弹出窗口中显示文件名):
I've added a new command under the "Source" menu successfully. Just not sure where to get the text from the active editor now. Here's what I have so far in my attempt to get the text (it's just displaying the file name in a popup):
package generatebuilderproject.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
public class GenerateBuilderHandler extends AbstractHandler {
public GenerateBuilderHandler() {
}
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
MessageDialog.openInformation(
window.getShell(),
"GenerateBuilderProject",
editorPart.getEditorInput().getName());
return null;
}
}
推荐答案
拥有 IEditorPart
后,您可以尝试以下操作:
Once you have the IEditorPart
, you can try the following:
IEditorInput input = editorPart.getEditorInput();
if (input instanceof FileEditorInput) {
IFile file = ((FileEditorInput) input).getFile();
InputStream is = file.getContents();
// TODO get contents from InputStream
}
这篇关于如何从Eclipse PDE中的活动编辑器获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文