如何在PyDev Jython脚本中注册更改文本的通知?
我想为PyDev编写一个Jython脚本,它将分析编辑器中的文本,然后在某些情况下为文本添加一些注释。每次用户输入内容时,分析都应该再次运行。
我通读了introduction to Jython scripting in PyDev,然后查看了example scripts,但它们似乎都是从命令键触发的。我查看了PyEdit class,看起来我应该注册IPyEditListener3's onInputChanged event。
我尝试了下面的脚本,但它似乎没有调用我的事件处理程序。我错过了什么?
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.'
答案 0 :(得分:1)
您可以通过命令'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.'
请注意你要做的事情,因为当用户输入内容时,这将在主线程中运行(并且你绝对不希望在这种情况下你想要什么慢 - 如果你'只是分析当前的行,事情应该没问题,但如果你在整个文档中运行你的启发式,事情可能会变慢。)