How do you put a save hook into wing?


27
views
1
11 days ago by
Wanting to add yapf formatting on save, but I need a way to capture the save event, run the formatter, update the buffer, then call the base save.

I see mentions about a pre-save event, but not finding any details anywhere.
Community: Wing Python IDE

2 Answers


2
11 days ago by
Here's a different approach that does hook up to presave to replace the contents of the editor at save time, just before saving, with the output from yapf:

import subprocess
import wingapi

def run_yapf(doc=wingapi.kArgDocument):
  
  # Set to non-None to use an uninstalled copy of yapf
  kYapfDir = None
  #kYapfDir = '/Users/jpe/Documents/tmp/yapf'
  
  proj = wingapi.gApplication.GetProject()
  pyexec = proj.GetPythonExecutable(doc.GetFilename())
  
  orig_text = doc.GetText()
  
  argv = [pyexec, '-m', 'yapf']
  process = subprocess.Popen(argv, cwd=kYapfDir, stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  from_yapf, error_output = process.communicate(orig_text)
  if from_yapf != orig_text:
    doc.SetText(from_yapf)

def _connect_to_document(doc):
  def _on_presave(filename, encoding):
    # Called for autosave w/ filename set to non-None
    if filename is not None:
      return
    
    if doc.GetMimeType() == 'text/x-python':
      run_yapf(doc)

  connect_id = doc.Connect('presave', _on_presave)

def _init():
  wingapi.gApplication.Connect('document-open', _connect_to_document)
  for doc in wingapi.gApplication.GetOpenDocuments():
    _connect_to_document(doc)
    
_init()
​
0
11 days ago by
You want the save-point signal on the CAPIDocument.  Something like this:

import wingapi

def _connect_to_document(doc):
  def _on_modified(savepoint):
    if not savepoint:
      return
    # Execute yapf here, possibly with wingapi.gApplication.AsyncExecuteCommandLine
  connect_id = doc.Connect('save-point', _on_modified)

def _init():
  wingapi.gApplication.Connect('document-open', _connect_to_document)
  for doc in wingapi.gApplication.GetOpenDocuments():
    _connect_to_document(doc)
    
_init()
​
This approach assumed that yapf would run and change the file on disk, and Wing would reload the changed file automatically afterward.  The other answer above is probably better, and is more complete in any case.
written 11 days ago by Wingware Support  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »