简体   繁体   中英

Eclipse plugin to read contents of a editor

I want to write a eclipse plugin which reads the contents from the editor window and displays it to me. This should've been a very simple task after I found how to do this on this post Adding item to Eclipse text viewer context menu

Before I state my problem, lemme state what I did till now:

I am developing on eclipse 3.7 and I created a simple hello world plugin whose code was auto-generated by eclipse for me.

Here is the run function:

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

    }

Now when I run this code, I get the output that its of type "reusable editor" unlike what it should've been "ITextEditor" (please correct me I am making some silly statement since I am a newbie in eclipse Plugin dev). I tried to check IReusableEditor instance type(as you can see in the code above, I have tried to check by using "instance of") but to my surprise itsnot pointing to any of the classes which implements IReusableEditor interface.

The editor from which I was trying to read was a simple editor window in which I had written some java code.

Also I get doc2 variable value as null for the following

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

Please let me know where am I doing the mistake, thanks in advance.

Okay I got how to read from the editor window

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

            }

     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM