Home Download Buy Blog Forum Support

RegReplace Plugin

Re: RegReplace Plugin

Postby BUGMENOT! on Wed Apr 09, 2014 8:32 pm

Oh I see, will be usefull for a case I have! thanks, keep the good work!
BUGMENOT!
 
Posts: 20
Joined: Thu Sep 19, 2013 1:54 pm

Re: RegReplace Plugin

Postby facelessuser on Wed Apr 30, 2014 5:51 pm

ST3 only (currently).

RegReplace now allows you to do procedural evaluations on found text via plugins.

Code: Select all
        // Some defintion
        "some_definition": {
            "find": "some regex here",
            "replace": "some optional replace here",
            "greedy": true,
            "case": true,
            "plugin": "User.rr_modules.force_all_caps",
            "args": {"some_plugin_rguments": "if_desired"}
        },


A plugin with path "User/rr_modules/force_all_caps.py" would be specified in your defintion as "User.rr_modules.force_all_caps".

Built-in RegReplace modules are simply referenced as "rr_modules.my_module" etc. (though there aren't any provided at this time).

Plugins are very simple:

Code: Select all
def run(text, **optional_keyword_arguments):
    # Do some stuff to text
    return text


That is it.

You can import RegReplace plugins into other RegReplace plugins if desired by.

Code: Select all
from RegReplace.rr_plugin import Plugin

def run(text, **optional_keyword_arguments):
    module = Plugin.load("User.rr_modules.myplugin")
    text = module.run(text)
    return text


If I were creating a User module, I personally would include from a sub-folder in User that does not provide a "__init__.py". This just basically means sublime won't automatically import the plugin file.

I guess you could consider this feature in beta. So, real world feedback may cause some aspects to change in the future, but maybe not.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: RegReplace Plugin

Postby facelessuser on Fri Jun 06, 2014 2:43 pm

Got some big changes.

Extended Backreferences (Replace Only)

People occasionally complain that RegReplace can't use title case back references:

        "test_case": {
"find": "([a-z])(?P<somegroup>[a-z]*)((?:_[a-z]+)+)",
"replace": "\\u\\1\\L\\g<somegroup>\\E\\U\\g<3>\\E",
"greedy": true
}


Well this is because the regex engine that RegReplace uses is not Sublime's built in regex engine, but instead uses Python. I won't go into reasons why, but suffice it to say, I have reasons. Now Python doesn't support title case backreferences in its regex engine. So I have been thinking about this recently and added some extended back references to handle this.

Now hold up, it isn't exactly the same! Python strings already use \u, so \u wasn't an option. So it is a sightly altered notation:

        "test_case": {
"find": "([a-z])(?P<somegroup>[a-z]*)((?:_[a-z]+)+)",
"replace": "\\c\\1\\L\\g<somegroup>\\E\\C\\g<3>\\E",
"greedy": true
},


See the difference? You have to use \c and \C instead of \u and \U. Now this is disabled by default and only works for the replace string (I'm not re-writing the regex parser for matches), so you have to enable it in your settings file by using:

    // Use extended backreferences
"extended_back_references": true


There might be more in the future.


Updated Plugin System (More Useful)
Also, I recently realized the plugin system was not that good in its current state. So I simplified it.

You still define a plugin the same way (plugins are referenced like importing a python module with Packages as your root) User.rr_modules.<plugin>:
        "some_definition": {
"find": "some regex here",
"replace": "some optional replace here",
"greedy": true,
"case": true,
"plugin": "User.rr_modules.useful_stuff",
"args": {"some_plugin_rguments": "if_desired"}
},



And I have provided a simple example on the repo you can try out. Now you just define a function called replace and it accepts a match object and optional arguments (I show them in the example just to remind you you can use them):
def replace(m, **kwargs):
text = "Here are your groups: "
for group in m.groups():
if group is not None:
text += "(%s)" % group
return text
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: RegReplace Plugin

Postby facelessuser on Wed Jul 30, 2014 12:29 am

Edit: Posted in wrong thread.
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Previous

Return to Plugin Announcements

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests