Skip to content

Commit

Permalink
fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
titoBouzout committed Jun 6, 2013
1 parent 10e4883 commit f099527
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
76 changes: 76 additions & 0 deletions Edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# edit.py
# buffer editing for both ST2 and ST3 that "just works"

import sublime
import sublime_plugin
from collections import defaultdict

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:
defer = defaultdict(dict)

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 sublime.version().startswith('2'):
edit = view.begin_edit()
self.run(edit)
view.end_edit(edit)
else:
key = str(hash(tuple(self.steps)))
sublime.edit_storage[key] = self.run
view.run_command('apply_edit', {'key': key})


class apply_edit(sublime_plugin.TextCommand):
def run(self, edit, key):
sublime.edit_storage.pop(key)(self.view, edit)
7 changes: 5 additions & 2 deletions tag_remove.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sublime, sublime_plugin
import re
from .Edit import Edit as Edit

def TagRemoveAll(data, view):
return re.sub(r'<[^>]*>', '', data);
Expand Down Expand Up @@ -44,7 +45,8 @@ def on_done(self, edit, tags):
continue
dataRegion = sublime.Region(region.begin(), region.end())
data = TagRemoveSelected(self.view.substr(dataRegion), tags, self.view)
self.view.replace(edit, dataRegion, data);
with Edit(self.view) as edit:
edit.replace(dataRegion, data);

class TagRemovePickedInDocumentCommand(sublime_plugin.TextCommand):
def run(self, edit, tags = False):
Expand All @@ -58,4 +60,5 @@ def run(self, edit, tags = False):
def on_done(self, edit, tags):
dataRegion = sublime.Region(0, self.view.size())
data = TagRemoveSelected(self.view.substr(dataRegion), tags, self.view)
self.view.replace(edit, dataRegion, data);
with Edit(self.view) as edit:
edit.replace(dataRegion, data);
7 changes: 5 additions & 2 deletions tag_remove_attributes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sublime, sublime_plugin
import re
from .Edit import Edit as Edit

def TagRemoveAttributesClean(data):
regexp = re.compile('(<([a-z0-9\:\-_]+)\s+>)');
Expand Down Expand Up @@ -48,7 +49,8 @@ def on_done(self, edit, attributes):
continue
dataRegion = sublime.Region(region.begin(), region.end())
data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
self.view.replace(edit, dataRegion, data);
with Edit(self.view) as edit:
edit.replace(dataRegion, data);

class TagRemovePickedAttributesInDocumentCommand(sublime_plugin.TextCommand):
def run(self, edit, attributes = False):
Expand All @@ -62,4 +64,5 @@ def run(self, edit, attributes = False):
def on_done(self, edit, attributes):
dataRegion = sublime.Region(0, self.view.size())
data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
self.view.replace(edit, dataRegion, data);

This comment has been minimized.

Copy link
@lunixbochs

lunixbochs Jun 12, 2013

Oh c'mon, you already had an edit object here from the TextCommand ;)

This comment has been minimized.

Copy link
@titoBouzout

titoBouzout Jun 12, 2013

Author Owner

the edit object is lost when asking the user for data input :)

This comment has been minimized.

Copy link
@titoBouzout

titoBouzout Jun 12, 2013

Author Owner

which is exactly the thing this commit is fixing #45

with Edit(self.view) as edit:
edit.replace(dataRegion, data);

3 comments on commit f099527

@FichteFoll
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your implementation idea of this very much, will probably do something similar for sublime_lib (in AAAPackageDev) when implementing ST3 support.

@titoBouzout
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes, glad you have see it, is really handy, not my, is a contribution of lunixbochs via http://www.sublimetext.com/forum/viewtopic.php?f=6&t=12551

@FichteFoll
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh I kinda figured that, but well guessed that I wanted to find the source. ;)

Please sign in to comment.