3

So, I'm looking to develop a plugin for Eclipse 4.2 that monitors edits that a user makes to their files.

This is my first Eclipse plugin, and to prepare, I walked through the Eclipse plugin development cheat sheet (HelloWorld) and spent many hours on help.eclipse.org looking through the documentation and the API. I think I have some idea of what tools I need, but I'm not sure how to put those tools together to do what I want.

Desired Results: Have a plugin that's kept apprised of every new letter added to a (Java) editor and any and all deletes. This includes things that Eclipse does (auto-completing variables, curly braces) as well as what the user types.

Tools that might help: I'm thinking that a IResourceChangeListener will assist, as it gives me a IResourceChangeEvent, with an accessible IResourceDelta which represents workspace changes. Also, since editors extend EditorPart, I'm thinking that adding a IPropertyChangeListener to the relevant editor may be useful as well.

I think I've got the right tools, but I've got no idea how to go about assembling them to do as I wish.

Questions:
Are the tools listed above the proper ones for the job?
How can I get a list of all editors opened or that will be opened and add listeners to them?
Any additional tips for resources on learning how to program Eclipse plugins?

KevinL
  • 1,238
  • 11
  • 28

2 Answers2

2

Yes and no. The IResourceChangedListener will trigger once the resource (file) changes. In most editors, this correspond to the user saving the file.

To monitor typing in close to real-time, one approach is to use a MonoReconciler to catch buffer changes after the user has been idle for, say, 0.5 seconds. This is how the JDT works.

Now, this is all easy if you are the creator of the EditorPart. Depending on which editor you wish to monitor, you need to get hold of its IDocument and add listeners as appropriate. See the documentation. For what its worth, IIRC the Java editors use ProjectionDocuments.

Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • What would you recommend that I extend? Which extension(s) should I use? – KevinL Jul 14 '12 at 23:50
  • If your goal is to add functionality to the Java editor, I recommend that you look into extending the JDT itself. However, adding a listener just requires you to get hold of a reference to the `IDocument`. – Krumelur Jul 15 '12 at 07:03
2

Krumelur's tip gave me a good starting point. The DocumentEvents returned by IDocumentListener are granular enough to give me a character by character analysis of what happened.

IEditorPart editor = ...;  
IEditorInput input = editor.getEditorInput();  
IDocument document=(((ITextEditor)editor).getDocumentProvider()).getDocument();

document.addDocumentListener(new IDocumentListener() {

        @Override
        public void documentChanged(DocumentEvent event) 
        {
            System.out.println("Change happened: " + event.toString());
        }

        @Override
        public void documentAboutToBeChanged(DocumentEvent event) {
            System.out.println("I predict that the following change will occur: "+event.toString());


        }
    };
});

As far as what I should extend, I extended org.eclipse.ui.startup and added a series of listeners until I got to the code that looks like what I've got above.

I hope this helps anybody else who is looking for what I was.

KevinL
  • 1,238
  • 11
  • 28
  • I'm taking a similar approach with my plugin, however when I launch eclipse the IDocumentListener in my plugin is not getting attached to the first Editor that opens. I'm implementing IPartListener2 to change my IDocumentListener from document to document as the Editor window changes - do you have a solution for this? – Diarmaid Oct 01 '12 at 09:21
  • 1
    Never mind, with a bit of thought I solved this - in Activator.start() I use workbenchWindow.getActivePage() which returns an IWorkbenchPage, then workbenchPage.getActiveEditor() to return an IEditorPart. This is what I needed. – Diarmaid Oct 01 '12 at 13:04
  • 1
    If you want to get a setup where every new editor gets your listener attached to it, get the IWorkBench with `IWorkbench wb = PlatformUI.getWorkbench();` , then you can call `wb.addWindowListener(...)`which gets the activePage with `workbenchWindow.getActivePage()` and then you can add a listener to the activePage with `activePage.addPartListener(...)` and this pattern continues until everything is listening to everything. – KevinL Oct 13 '12 at 13:35
  • @KevinL : I am new to eclipse plugin development. I have written a sample plug-in which will execute (execute method inside a handler) when I click on a menu item. I want this code to be executed when some even occurs in editor. lets say when I press some key. How can I achieve this? – Anurag Rana Nov 10 '14 at 14:39
  • @User42, With just the 4 sentences you gave, I cannot help you. Please make a separate, more verbose question on this site and I (and the rest of the community) will try to assist you. Feel free to link that question in a comment. – KevinL Nov 10 '14 at 21:24
  • @KevinL : I post a new question. Please have a look and let me know if more information is required. http://stackoverflow.com/questions/26871970/eclipse-plugin-development-how-to-listen-events-in-eclipse-editor – Anurag Rana Nov 11 '14 at 18:35