我有一个处理程序,我想在其中从工作台中的事件编辑器获取文本。从下面的屏幕截图中,我想获取 Test.java 中的所有内容(“public class Test...”)。
我已成功在“源”菜单下添加了一个新命令。只是不确定现在从事件编辑器中获取文本的位置。这是迄今为止我尝试获取文本的内容(它只是在弹出窗口中显示文件名):
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
后,您可以尝试以下操作:
IEditorInput input = editorPart.getEditorInput();
if (input instanceof FileEditorInput) {
IFile file = ((FileEditorInput) input).getFile();
InputStream is = file.getContents();
// TODO get contents from InputStream
}
关于eclipse - 如何从 Eclipse PDE 中的事件编辑器获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14897938/