【问题标题】:Eclipse plugin to read contents of a editor用于读取编辑器内容的 Eclipse 插件
【发布时间】:2012-01-07 05:40:02
【问题描述】:

我想编写一个 Eclipse 插件,它从编辑器窗口中读取内容并将其显示给我。在我在这篇帖子Adding item to Eclipse text viewer context menu找到如何做到这一点之后,这应该是一个非常简单的任务@

在我陈述我的问题之前,请先陈述我到目前为止所做的事情:

我正在 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 插件中的新手,所以我做了一些愚蠢的声明开发)。我尝试检查 IReusableEditor 实例类型(如您在上面的代码中所见,我尝试使用“instance of”进行检查),但令我惊讶的是它没有指向任何实现 IReusableEditor 接口的类。

我试图从中读取的编辑器是一个简单的编辑器窗口,我在其中编写了一些 java 代码。

此外,我将以下 doc2 变量值设为 null

final IDocument doc2 = (IDocument) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getAdapter(IDocument.class);

请让我知道我在哪里做错了,提前谢谢。

【问题讨论】:

  • 我部署了插件并尝试在 windows 和 linux 上运行它,但问题仍然存在
  • #getAdapter() 可以根据编辑器实现返回 null。这是正常的。

标签: java eclipse eclipse-plugin


【解决方案1】:

好的,我知道如何从编辑器窗口阅读

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());

            }

     }

【讨论】:

  • CompilationUnitEditorITextEditor 的一个实例。我不确定为什么你的第一个代码会失败。
  • 我认为您忽略了由于一个类可以实现多个接口,因此像这样的大型 if-then-else 只会继续进行第一个成功的测试。奇怪的是搜索范围没有包含足够的插件来找到实现者——其中一个是 AbstractTextEditor。
猜你喜欢
  • 2010-11-09
  • 2019-01-13
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
相关资源
最近更新 更多