1

How can I register for a notification of changed text in a PyDev Jython script?

I'd like to write a Jython script for PyDev that will analyse the text in the editor and then add some comments to the text under certain circumstances. Every time the user types something, the analysis should run again.

I read through the introduction to Jython scripting in PyDev, and I looked at the example scripts, but they all seem to trigger from a command key. I looked at the PyEdit class, and it looks like I should register for IPyEditListener3's onInputChanged event.

I tried the script below, but it doesn't seem to call my event handler. What did I miss?

if False:
    from org.python.pydev.editor import PyEdit #@UnresolvedImport
    cmd = 'command string'
    editor = PyEdit

#-------------- REQUIRED LOCALS
#interface: String indicating which command will be executed
assert cmd is not None 

#interface: PyEdit object: this is the actual editor that we will act upon
assert editor is not None

print 'command is:', cmd, ' file is:', editor.getEditorFile().getName()
if cmd == 'onCreateActions':
    from org.python.pydev.editor import IPyEditListener #@UnresolvedImport
    from org.python.pydev.editor import IPyEditListener3 #@UnresolvedImport

    class PyEditListener(IPyEditListener, IPyEditListener3):
        def onInputChanged(self, 
                           edit, 
                           oldInput, 
                           newInput, 
                           monitor):
            print 'onInputChanged'

    try:
        editor.addPyeditListener(PyEditListener())
    except Exception, ex:
        print ex

print 'finished.'
Don Kirkby
  • 53,582
  • 27
  • 205
  • 286

1 Answers1

1

onInputChanged should only be called when some editor was bound to one file and then becomes bound to another file (not on document changes).

What you want can be achieved listening to document changes on command 'onSetDocument':

if False:
    from org.python.pydev.editor import PyEdit #@UnresolvedImport
    cmd = 'command string'
    editor = PyEdit

#-------------- REQUIRED LOCALS
#interface: String indicating which command will be executed
assert cmd is not None 

#interface: PyEdit object: this is the actual editor that we will act upon
assert editor is not None

print 'command is:', cmd, ' file is:', editor.getEditorFile().getName()
if cmd == 'onSetDocument':
    from org.eclipse.jface.text import IDocumentListener
    class Listener(IDocumentListener):

        def documentAboutToBeChanged(self, ev):
            print 'documentAboutToBeChanged'

        def documentChanged(self, ev):
            print 'documentChanged'

    doc = editor.getDocument()
    doc.addDocumentListener(Listener())

print 'finished.'

Just take care on what you're going to do because this will be run in the main thread when the user types things (and you definitely don't want what you want to be slow in this case -- if you're just analyzing the current line, things should be OK, but if you run your heuristics on the whole document, things could get slow).

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78