2012-06-12 3 views
1

PyDev Jython 스크립트에서 변경된 텍스트 알림을 등록하려면 어떻게해야합니까?편집기 텍스트가 바뀔 때마다 PyDev에서 자이 썬 스크립팅 트리거

필자는 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.' 

답변

1

onInputChanged는 일부 편집기가 한 파일에 바인딩 된 다음 문서 변경시 다른 파일에 바인딩 될 때만 호출해야합니다. 당신이 명령에 대한 변경을 문서화 듣고 달성 할 수 원하는 것은

'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.' 

그냥 당신이 때 사용자이 메인 스레드에서 실행되기 때문에 할 건지에 조심 (그리고이 경우에는 느리게하고 싶은 것을 원하지 않을 것입니다. 단지 현재의 줄을 분석하고 있다면 괜찮을 것입니다. 그러나 전체 문서에서 경험적 방법을 실행하면 느린).