Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixes #45

  • Loading branch information...
commit f09952751b6a2b131d83927593b6e788afb48a9e 1 parent 10e4883
Tito titoBouzout authored

Showing 3 changed files with 86 additions and 4 deletions. Show diff stats Hide diff stats

  1. +76 0 Edit.py
  2. +5 2 tag_remove.py
  3. +5 2 tag_remove_attributes.py
76 Edit.py
... ... @@ -0,0 +1,76 @@
  1 +# edit.py
  2 +# buffer editing for both ST2 and ST3 that "just works"
  3 +
  4 +import sublime
  5 +import sublime_plugin
  6 +from collections import defaultdict
  7 +
  8 +try:
  9 + sublime.edit_storage
  10 +except AttributeError:
  11 + sublime.edit_storage = {}
  12 +
  13 +class EditStep:
  14 + def __init__(self, cmd, *args):
  15 + self.cmd = cmd
  16 + self.args = args
  17 +
  18 + def run(self, view, edit):
  19 + if self.cmd == 'callback':
  20 + return self.args[0](view, edit)
  21 +
  22 + funcs = {
  23 + 'insert': view.insert,
  24 + 'erase': view.erase,
  25 + 'replace': view.replace,
  26 + }
  27 + func = funcs.get(self.cmd)
  28 + if func:
  29 + func(edit, *self.args)
  30 +
  31 +
  32 +class Edit:
  33 + defer = defaultdict(dict)
  34 +
  35 + def __init__(self, view):
  36 + self.view = view
  37 + self.steps = []
  38 +
  39 + def step(self, cmd, *args):
  40 + step = EditStep(cmd, *args)
  41 + self.steps.append(step)
  42 +
  43 + def insert(self, point, string):
  44 + self.step('insert', point, string)
  45 +
  46 + def erase(self, region):
  47 + self.step('erase', region)
  48 +
  49 + def replace(self, region, string):
  50 + self.step('replace', region, string)
  51 +
  52 + def callback(self, func):
  53 + self.step('callback', func)
  54 +
  55 + def run(self, view, edit):
  56 + for step in self.steps:
  57 + step.run(view, edit)
  58 +
  59 + def __enter__(self):
  60 + return self
  61 +
  62 + def __exit__(self, type, value, traceback):
  63 + view = self.view
  64 + if sublime.version().startswith('2'):
  65 + edit = view.begin_edit()
  66 + self.run(edit)
  67 + view.end_edit(edit)
  68 + else:
  69 + key = str(hash(tuple(self.steps)))
  70 + sublime.edit_storage[key] = self.run
  71 + view.run_command('apply_edit', {'key': key})
  72 +
  73 +
  74 +class apply_edit(sublime_plugin.TextCommand):
  75 + def run(self, edit, key):
  76 + sublime.edit_storage.pop(key)(self.view, edit)
7 tag_remove.py
... ... @@ -1,5 +1,6 @@
1 1 import sublime, sublime_plugin
2 2 import re
  3 +from .Edit import Edit as Edit
3 4
4 5 def TagRemoveAll(data, view):
5 6 return re.sub(r'<[^>]*>', '', data);
@@ -44,7 +45,8 @@ def on_done(self, edit, tags):
44 45 continue
45 46 dataRegion = sublime.Region(region.begin(), region.end())
46 47 data = TagRemoveSelected(self.view.substr(dataRegion), tags, self.view)
47   - self.view.replace(edit, dataRegion, data);
  48 + with Edit(self.view) as edit:
  49 + edit.replace(dataRegion, data);
48 50
49 51 class TagRemovePickedInDocumentCommand(sublime_plugin.TextCommand):
50 52 def run(self, edit, tags = False):
@@ -58,4 +60,5 @@ def run(self, edit, tags = False):
58 60 def on_done(self, edit, tags):
59 61 dataRegion = sublime.Region(0, self.view.size())
60 62 data = TagRemoveSelected(self.view.substr(dataRegion), tags, self.view)
61   - self.view.replace(edit, dataRegion, data);
  63 + with Edit(self.view) as edit:
  64 + edit.replace(dataRegion, data);
7 tag_remove_attributes.py
... ... @@ -1,5 +1,6 @@
1 1 import sublime, sublime_plugin
2 2 import re
  3 +from .Edit import Edit as Edit
3 4
4 5 def TagRemoveAttributesClean(data):
5 6 regexp = re.compile('(<([a-z0-9\:\-_]+)\s+>)');
@@ -48,7 +49,8 @@ def on_done(self, edit, attributes):
48 49 continue
49 50 dataRegion = sublime.Region(region.begin(), region.end())
50 51 data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
51   - self.view.replace(edit, dataRegion, data);
  52 + with Edit(self.view) as edit:
  53 + edit.replace(dataRegion, data);
52 54
53 55 class TagRemovePickedAttributesInDocumentCommand(sublime_plugin.TextCommand):
54 56 def run(self, edit, attributes = False):
@@ -62,4 +64,5 @@ def run(self, edit, attributes = False):
62 64 def on_done(self, edit, attributes):
63 65 dataRegion = sublime.Region(0, self.view.size())
64 66 data = TagRemoveAttributesSelected(self.view.substr(dataRegion), attributes, self.view)
65   - self.view.replace(edit, dataRegion, data);
  67 + with Edit(self.view) as edit:
  68 + edit.replace(dataRegion, data);

3 comments on commit f099527

FichteFoll
Owner

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

Tito
Collaborator

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
Owner

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

Ryan Hileman

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

Tito

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

Tito

which is exactly the thing this commit is fixing #45

Please sign in to comment.
Something went wrong with that request. Please try again.