[英]Eclipse plugin to read contents of a editor
我想编写一个eclipse插件,该插件从编辑器窗口中读取内容并将其显示给我。 这应该已经是一个非常简单的任务后,我发现如何做到这一点对这个职位添加项目到Eclipse文本浏览器上下文菜单
在我陈述我的问题之前,lemme陈述我到现在为止所做的事情:
我正在eclipse 3.7上进行开发,并且创建了一个简单的hello world插件,其代码由eclipse为我自动生成。
这是运行功能:
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"AnirudhPlugin",
"Hello, Eclipse world");
try {
IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if(part instanceof IReusableEditor){
System.out.println("is of type reusable editor");
final IReusableEditor editor = (IReusableEditor)part;
if(editor instanceof AbstractTextEditor){
System.out.println("abstract");
}
if(editor instanceof StatusTextEditor){
System.out.println("status");
}
if(editor instanceof TextEditor){
System.out.println("text");
}
if(editor instanceof AbstractDecoratedTextEditor){
System.out.println("abs dec");
}
if(editor instanceof CommonSourceNotFoundEditor){
System.out.println("comm");
}
}
if ( part instanceof ITextEditor ) {
final ITextEditor editor = (ITextEditor)part;
IDocumentProvider prov = editor.getDocumentProvider();
IDocument doc = prov.getDocument( editor.getEditorInput() );
ISelection sel = editor.getSelectionProvider().getSelection();
if ( sel instanceof TextSelection ) {
final TextSelection textSel = (TextSelection)sel;
String newText = "/*" + textSel.getText() + "*/";
MessageDialog.openInformation(
window.getShell(),
"AnirudhPlugin",
newText);
doc.replace( textSel.getOffset(), textSel.getLength(), newText );
}
}else{
MessageDialog.openInformation(
window.getShell(),
"AnirudhPlugin",
"Not ITextEditor");
}
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
现在,当我运行此代码时,我得到的输出是“可重用编辑器”类型,而不是应有的“ ITextEditor”(请纠正我,因为我是eclipse Plugin dev的新手,所以我做出了一些愚蠢的声明)。 我试图检查IReusableEditor实例类型(如您在上面的代码中看到的,我试图通过使用“ instance of”进行检查),但令我惊讶的是,它没有指向实现IReusableEditor接口的任何类。
我试图从中读取的编辑器是一个简单的编辑器窗口,其中编写了一些Java代码。
我也将doc2变量值作为以下内容的null
final IDocument doc2 = (IDocument) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getAdapter(IDocument.class);
请先让我知道我在哪里做错了。
好吧,我知道如何从编辑器窗口中进行读取
if (editor instanceof CompilationUnitEditor) {
CompilationUnitEditor compEditor = (CompilationUnitEditor)editor;
IEditorInput input = compEditor.getEditorInput();
IDocumentProvider provider = compEditor.getDocumentProvider();
IDocument document = provider.getDocument(input);
if (document != null) {
System.out.println("document contents:" + document.get());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.