-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move compat stuff out of the plugin file for clarity
- Loading branch information
1 parent
33e3362
commit 8a4544b
Showing
2 changed files
with
84 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Borrowed from SublimeXiKi | ||
# https://github.com/lunixbochs/SublimeXiki/blob/st3/edit.py | ||
|
||
import sublime | ||
import sublime_plugin | ||
from sys import version_info | ||
PY3K = version_info >= (3, 0, 0) | ||
|
||
try: | ||
sublime.edit_storage | ||
except AttributeError: | ||
sublime.edit_storage = {} | ||
|
||
|
||
class EditStep: | ||
def __init__(self, cmd, *args): | ||
self.cmd = cmd | ||
self.args = args | ||
|
||
def run(self, view, edit): | ||
if self.cmd == 'callback': | ||
return self.args[0](view, edit) | ||
|
||
funcs = { | ||
'insert': view.insert, | ||
'erase': view.erase, | ||
'replace': view.replace, | ||
} | ||
func = funcs.get(self.cmd) | ||
if func: | ||
func(edit, *self.args) | ||
|
||
|
||
class Edit: | ||
def __init__(self, view): | ||
self.view = view | ||
self.steps = [] | ||
|
||
def step(self, cmd, *args): | ||
step = EditStep(cmd, *args) | ||
self.steps.append(step) | ||
|
||
def insert(self, point, string): | ||
self.step('insert', point, string) | ||
|
||
def erase(self, region): | ||
self.step('erase', region) | ||
|
||
def replace(self, region, string): | ||
self.step('replace', region, string) | ||
|
||
def callback(self, func): | ||
self.step('callback', func) | ||
|
||
def run(self, view, edit): | ||
for step in self.steps: | ||
step.run(view, edit) | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
view = self.view | ||
if not PY3K: | ||
edit = view.begin_edit() | ||
self.run(view, edit) | ||
view.end_edit(edit) | ||
else: | ||
key = str(hash(tuple(self.steps))) | ||
sublime.edit_storage[key] = self.run | ||
view.run_command('worksheet_apply_edit', {'key': key}) | ||
|
||
|
||
class WorksheetApplyEditCommand(sublime_plugin.TextCommand): | ||
def run(self, edit, key): | ||
sublime.edit_storage.pop(key)(self.view, edit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters