Commits

guillermooo committed 906dc17

port to ST2

  • Participants
  • Parent commits 2ca5825

Comments (0)

Files changed (6)

 syntax: glob
 
 *.pyc
-_*.txt
+_*.txt
+
+*.sublime-project
+*.sublime-workspace
 from __future__ import with_statement
-import sublime, sublimeplugin
+import sublime, sublime_plugin
 import os
 import re
 import sys
 
-USER_PKG_PATH = sublime.packagesPath() + "\\User"
+USER_PKG_PATH = sublime.packages_path() + "\\User"
 DEFAULT_REGEX_DB_TEMPLATE = """
 #===============================================================================
 # SublimeText modelines plugin required for the sublime: lines to work! (optional)
 
 THIS_PACKAGE_NAME = "TextButcher"
 THIS_PACKAGE_DEV_NAME = "XXX" + THIS_PACKAGE_NAME
-DEBUG = os.path.exists(sublime.packagesPath() + "/" + THIS_PACKAGE_DEV_NAME)
-THIS_PACKAGE_PATH  = sublime.packagesPath() + "\\" + THIS_PACKAGE_DEV_NAME if DEBUG else THIS_PACKAGE_NAME
+DEBUG = os.path.exists(sublime.packages_path() + "/" + THIS_PACKAGE_DEV_NAME)
+THIS_PACKAGE_PATH  = sublime.packages_path() + "\\" + THIS_PACKAGE_DEV_NAME if DEBUG else THIS_PACKAGE_NAME
 
 
 def list2Table(theList, rowSep='\t', cellPadding=2):
     return theTable
 
 
-class TextButcherCommand(sublimeplugin.TextCommand):
+class TextButcherCommand(sublime_plugin.TextCommand):
     """
     This plugin provides a facility to replace the selected region(s)'s text
     by selecting predefined actions by a meaningful name. It avoids having to
     Refer to the readme file for instructions.
     """
 
-    REGEX_DB_DEFAULT = os.path.join((sublime.packagesPath() + "/" + THIS_PACKAGE_DEV_NAME if DEBUG else THIS_PACKAGE_NAME), 'regexes.txt')
-    REGEX_DB_USER = os.path.join(sublime.packagesPath(), 'User/regexes.txt')
+    REGEX_DB_DEFAULT = os.path.join((sublime.packages_path() + "/" + THIS_PACKAGE_DEV_NAME if DEBUG else THIS_PACKAGE_NAME), 'regexes.txt')
+    REGEX_DB_USER = os.path.join(sublime.packages_path(), 'User/regexes.txt')
     _EDIT_IN_ST_CMD_IDX = 0
 
     def onSelect(self, i):
             # The following will fail silently if the requested file doesn't exist.
             openMe = self.REGEX_DB_USER if os.path.exists(self.REGEX_DB_USER) \
                                           else self.REGEX_DB_DEFAULT
-            self.view.window().runCommand("open '%s'" % openMe.replace("\\","/"))
+            self.view.window().open_file(openMe.replace("\\", "/"))
             return
 
         name, searchPattern, replacePattern, options = self.rgs[i].split("\t")[:4]
         if options == 'sublime:':
             # Option keys with surrounding whitespace are illegal in Sublime Text.
             searchPattern = searchPattern.strip()
-            self.view.options().set(searchPattern, replacePattern)
+            # FIXME: will only work for settings taking string values
+            self.view.settings().set(searchPattern, replacePattern)
             return
 
         if options == "tbf":
             # Runs a TextButcher filter.
-            self.view.runCommand("textPipelineCommandRunner", [searchPattern, replacePattern,])
+            self.view.run_command("text_pipeline_command_runner", {"search_pattern": searchPattern, "replace_pattern": replacePattern})
             return
 
         if options == "uberselection:":
-            self.view.runCommand("uberSelection", ["%s;%s" % (searchPattern, replacePattern),])
+            self.view.run_command("uber_selection", {"command": "%s;%s" % (searchPattern, replacePattern)})
             return
 
         if options == "powershell:":
-            self.view.runCommand("runExternalPSCommand", [searchPattern, searchPattern])
+            self.view.run_command("run_powershell", {"command": searchPattern})
             return
 
         r = re.compile(searchPattern)
         for region in self.view.sel():
-            self.view.replace(region, re.sub(r, replacePattern, self.view.substr(region)))
+            edit = self.view.begin_edit()
+            self.view.replace(edit, region, re.sub(r, replacePattern, self.view.substr(region)))
+            self.view.end_edit(edit)
 
-    def run(self, view, args):
-        self.view = view
+    def run(self, edit):
         try:
             f = open(self.REGEX_DB_USER, 'r')
         except IOError:
                     try:
                         f = open(self.REGEX_DB_USER, 'r')
                     except IOError:
-                        sublime.errorMessage("regexes.txt default file couldn't be created.")
+                        sublime.status_message("regexes.txt default file couldn't be created.")
                         return
 
         self.rgs = [ l[:-1] for l in f.readlines() if l.strip() and not l.strip().startswith("#") ]
         self.rgs.insert(self._EDIT_IN_ST_CMD_IDX + 3,
                         "")
 
-        view.window().showSelectPanel(list2Table(self.rgs), self.onSelect, None,
-                                    sublime.SELECT_PANEL_MONOSPACE_FONT | sublime.SELECT_PANEL_MULTI_SELECT)
+        self.view.window().show_quick_panel(list2Table(self.rgs), self.onSelect,
+                                    sublime.MONOSPACE_FONT)
 
 
-class TextPipelineCommandRunner(sublimeplugin.TextCommand):
+class TextPipelineCommandRunner(sublime_plugin.TextCommand):
     """I'm passed the name of a module and a callable living in that module's
     namespace, extract the callable and run it."""
 
-    def run(self, view, args):
+    def run(self, edit, search_pattern=None, replace_pattern=None):
         clean_up = False
         if not USER_PKG_PATH in sys.path:
             sys.path.extend((USER_PKG_PATH, THIS_PACKAGE_PATH))
             clean_up = True
 
-        filters_module_name, callable_name = args[0], args[1]
+        filters_module_name, callable_name = search_pattern, replace_pattern
+        print "XXX", filters_module_name, callable_name
         filters_module = __import__(filters_module_name)
-        getattr(filters_module, callable_name)(view)
+        getattr(filters_module, callable_name)(self.view)
 
         if clean_up:
             sys.path.pop()

textbutcher.sublime-project

-<!-- Documentation is available at http://www.sublimetext.com/docs/projects -->
-<project>
-    <mount dir="." exclude="*.png,*.jpg,*.exe,*.dll,*.obj" direxclude=".svn,.git,.hg,CVS,bin" />
-    <options>
-        <![CDATA[buildFile Packages/Python/Python.sublime-build]]>
-</options>
-</project>
+{
+	"folders":
+	[
+		{
+			"path": "/C/Users/guillermo/AppData/Roaming/Sublime Text 2/Packages/TextButcher"
+		}
+	]
+}

textbutcherlib/regionselectors.py

         self.fromPosition = fromPosition
 
     def run(self, view):
-        for s in view.findAll(self.pattern, self.flags):
+        for s in view.find_all(self.pattern, self.flags):
             yield s

textbutcherlib/textactions.py

-import sublime
-import sublimeplugin
 import re
 import functools
-from decorators import asTextCommand
-from textbutcherlib.textpipeline import asTextCommandM
 
 
 DONT_CAPITALIZE = ('in', 'of', 'on', 'with',)
         self.upper_initial = upper_initial
 
     def run(self, view, where):
+        edit = view.begin_edit()
         for reg in where:
 
             words = view.substr(reg).split(' ')
                 if not w in DONT_CAPITALIZE:
                     words[i] = w[0].upper() + w[1:]
 
-            view.replace(reg, ' '.join(words))
+            view.replace(edit, reg, ' '.join(words))
+        view.end_edit(edit)
 
 
 class Upper(object):
     def run(self, view, where):
+        edit = view.begin_edit()
         for reg in where:
-            view.replace(reg, view.substr(reg).upper())
+            view.replace(edit, reg, view.substr(reg).upper())
+        view.end_edit(edit)
 
 
 class Lower(object):
     def run(self, view, where):
+        edit = view.begin_edit()
         for reg in where:
-            view.replace(reg, view.substr(reg).lower())
+            view.replace(edit, reg, view.substr(reg).lower())
+        view.end_edit(edit)
 
 
 class Replace(object):
         self.with_this = with_this
 
     def run(self, view, where):
+        edit = view.begin_edit()
         for reg in where:
-            view.replace(reg, re.sub(self.what, self.with_this, view.substr(reg)))
+            view.replace(edit, reg, re.sub(self.what, self.with_this, view.substr(reg)))
+        view.end_edit(edit)
 
 
 class Transpose(object):
         self.sep = sep
         self.ask = ask
 
-    @asTextCommandM
     def onDone(self, view, s):
         self.sep = s or self.sep
         self.transpose(view)
 
     def transpose(self, view):
+        edit = view.begin_edit()
         for sel in view.sel():
             left, sep, right = view.substr(sel).partition(self.sep)
             if self.sep.strip():
-                view.replace(sel, ("%s %s %s" % (right.strip(), self.sep, left.strip())).strip())
+                view.replace(edit, sel, ("%s %s %s" % (right.strip(), self.sep, left.strip())).strip())
             else:
-                view.replace(sel, ("%s%s%s" % (right, self.sep, left)).strip())
+                view.replace(edit, sel, ("%s%s%s" % (right, self.sep, left)).strip())
+        view.end_edit(edit)
 
     def run(self, view, where):
         if self.ask:
-            view.window().showInputPanel("Separator? (Leave blank for space.):", "", functools.partial(self.onDone, view), None, None)
+            view.window().show_quick_panel("Separator? (Leave blank for space.):", "", functools.partial(self.onDone, view), None, None)
         else:
             self.transpose(view)

textbutcherlib/textpipeline.py

-import sublimeplugin
-import sublime
-import sys
-import functools
-
 
 class Do(object):
     """I act as a glue between a region selector and a text action."""
     def __init__(self, view, where, action):
         action.run(view, where.run(view))
-
-
-def asTextCommandM(f):
-    """I run `f` through a TextCommand.run method. Useful to group buffer edits
-    atomically from code external to a TextCommand.
-
-    Requirements:
-
-        A TextCommand called TextCommandRunnerCommand must exist and it must
-        have a .prime() method.
-
-        `f`'s second argument must be a view instance.
-
-    Usage:
-
-        @asTextCommandM
-        def method(self, view, x, y):
-            view.replace(x, y)
-
-    """
-    def runThruTextCommand(*args, **kwargs):
-        i = sublimeplugin.textCommands["textCommandRunner"]
-        i.prime(f, args, kwargs)
-        args[1].runCommand("textCommandRunner")
-    return runThruTextCommand