Skip to content

Commit

Permalink
move compat stuff out of the plugin file for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
jcartledge committed Jul 5, 2013
1 parent 33e3362 commit 8a4544b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 78 deletions.
76 changes: 76 additions & 0 deletions edit.py
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)
86 changes: 8 additions & 78 deletions worksheet.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,20 @@
import sys
import sublime
import sublime_plugin
import os

# Make sure /usr/local/bin is on the path
exec_path = os.getenv('PATH', '')
if not "/usr/local/bin" in exec_path:
os.environ["PATH"] = exec_path + os.pathsep + "/usr/local/bin"

PY3K = sys.version_info >= (3, 0, 0)

from sys import version_info
PY3K = version_info >= (3, 0, 0)
if PY3K:
from .edit import Edit
from . import repl
else:
from edit import Edit
import repl


# Borrowed from SublimeXiKi(https://github.com/lunixbochs/SublimeXiki/blob/st3/edit.py)
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)
# Make sure /usr/local/bin is on the path
exec_path = os.getenv('PATH', '')
if not "/usr/local/bin" in exec_path:
os.environ["PATH"] = exec_path + os.pathsep + "/usr/local/bin"


class WorksheetCommand(sublime_plugin.TextCommand):
Expand Down

0 comments on commit 8a4544b

Please sign in to comment.