+4

API: better use of context managers

David Baumgold hace 12 años 0

The Python API should use context managers (http://docs.python.org/reference/compound_stmts.html#with) wherever reasonable to make editing and other stateful operations more seamless and Pythonic. This will make it even easier to write more and better plugins, and since it's a backwards-incompatible change, the time to make it is *now*, while ST2 is still in alpha/beta state.


The sublime.View class has a begin_edit() method and an end_edit() method: these two commands are prime candidates to be turned into a context manager. Proposed API:


with view:

  do_a_thing()

  do_another_thing()


Much more straightforward and Pythonic than creating an edit object, passing it around, and finalizing it with end_edit()