簡體   English   中英

Eclipse插件可讀取編輯器的內容

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM