Eclipse Plugin to granularly monitor editor changes

后端 未结 2 1431
暖寄归人
暖寄归人 2021-01-06 16:48

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 walke

相关标签:
2条回答
  • 2021-01-06 17:06

    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.

    0 讨论(0)
  • 2021-01-06 17:14

    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.

    0 讨论(0)
提交回复
热议问题