Wow, it’s so hard to find answers for plugin development! Especially for a python newb like me. But I fuddled my way through moving the set of replaces into another function so that I can easily add another set later.
caveat: I’m a newb here, so there may be ways to do it better.
One of the things that was hard to find out was that you don’t pass the same arguments when you take some code out into a separate function. My new function got “self” and “edit” as arguments (line 20), but… when I passed “self” and “edit” when I called “cfml_to_cfscript” (at about line 8), that gave me an error. It worked when I just passed “edit” only. I guess “self” is implied or something.
Here’s the command I put in the “Packages\User\Context.sublime-menu” file:
[code]{
“id” : “dgReplaceLib”,
“caption”: “dg Replace Library”,
“children”:
{
"id": "dgReplaceLib_cfml_to_cfscript",
"caption" : "CFML to cfScript",
"command" : "dg_replace_lib",
"args" : { "replaceSet": "CFML to cfScript" }
}
]
}[/code]
And here’s the plugin code:
[code]import sublime, sublime_plugin, re
Extends TextCommand so that run() receives a View to modify.
class dgReplaceLibCommand(sublime_plugin.TextCommand):
def run(self, edit, **args):
if (args'replaceSet'] == "CFML to cfScript"):
self.cfml_to_cfscript(edit)
def selections(self, view, default_to_all=True):
regions = [r for r in view.sel() if not r.empty()]
if not regions and default_to_all:
regions = [sublime.Region(0, view.size())]
return regions
def cfml_to_cfscript(self, edit):
view = self.view
arrRE =
" \t]*(/?)> \t]*$", "\\1>"]
, "<cfset \\t]+(.*)/>$", "\\1;"]
, "<cfset \\t]+(.*)>$", "\\1;"]
, "<cfreturn \\t]+(.*)/>$", "return \\1;"]
, "<cfreturn \\t]+(.*)>$", "return \\1;"]
, "<cflocation \\t]+(.*)/>$", ",, location( \\1 );"]
, "<cflocation \\t]+(.*)>$", ",, location( \\1 );"]
, "<cfcatch \\t]+type=\"(^\"]*)\" \\t]*>$", "} catch ( \\1 e ) {"]
, "<cfcatch \\t]*>$", "} catch ( NoTypeSpecifiedSoFixIt e ) {"]
, "^( \\t])*</cfcatch> \\t]*\\n", ""]
, "<cfdump \\t]+(.*)/>$", ",, writeDump( \\1 ) {"]
, "<cfdump \\t]+(.*)>$", ",, writeDump( \\1 ) {"]
, "<cfswitch \\t]+expression=(.*)>$", "switch( \\1 ) {"]
, "</cfcase> \\t]*$", "break;"]
, "</cfswitch> \\t]*$", "}"]
, "<cfcase \\t]+value=(.*)>$", "case \\1: // put a list of values as separate case statements"]
, "<cfdefaultcase> \\t]*", "default:"]
, "</cfdefaultcase> \\t]*", "break;"]
, "<cfif \\t]+(.*)>$", "if (\\1) {"]
, "<cfelseif \\t]+(.*)>$", "} else if (\\1) {"]
, "<cfelse \\t]*> \\t]*$", "} else {"]
, "</cfif> \\t]*$", "}"]
, "<cfinclude \\t]+template=(.*)/>$", "include \\1;"]
, "<cfinclude \\t]+template=(.*)>$", "include \\1;"]
, "<cftry> \\t]*", "try {"]
, "</cftry> \\t]*", "}"]
, "<cfabort \\t/]*> \\t]*", "abort;"]
, "<!--- \\t]*(.*) \\t]*---> \\t]*$", "// \\1"]
, "^( \\t]*)<!--- \\t]*$", "\\1/*"]
, "^( \\t]*)---> \\t]*$", "\\1*/"]
, "^ \\t]*</?cfscript> \\t]*\\n", ""]
, "</?cfscript>", ""]
]
for list in arrRE:
# print(list[0] + list[1])
thisFind = list[0]
thisReplace = list[1]
reobj = re.compile(thisFind, re.MULTILINE)
for region in self.selections(view):
trimmed = reobj.sub(thisReplace, view.substr(region))
view.replace(edit, region, trimmed)
[/code]