Home Download Buy Blog Forum Support

Plugin Idea: Would This Be Useful?

Plugin Idea: Would This Be Useful?

Postby JeffreyWay on Sat Jan 07, 2012 5:40 pm

Hey guys -

I was thinking about a plugin that might be useful. It essentially offers a way to execute saved regular expressions on demand. For example, I'm often working with other people's code or HTML, and I find myself making the same set of changes over and over. One thing that I'm constantly doing is removing type="text/javascript" and type="text/css" from script and link tags, respectively (they're not necessary). If there was a plugin with a settings file...

Code: Select all
{
   replacements: {
      'Remove Link and Script Types': /\stype="text\/(?:css|javascript)/ig
   }
}


I could then pull up the command palette, choose "Remove Link and Script Types," and the associated regular expression will instantly be run on the contents of the focused page.

...Or maybe to remove all HTML comments from the page:

Code: Select all
replacements: {
   'Remove HTML Comments': /<!--[\s\S]+?-->/g
}


It would be even neater if there was a way to stack these. So, in addition to single commands, I could also execute a series of them with a single command: first use this regular expression to remove comments, then make the first letter of all words in h1 tags capital, etc.

Code: Select all
sequence: {
   "Optimize HTML": [replacement1, replacement1, replacement1]
}


The regex references in the array would then be executed sequentially on the document.

Personally, I'd have a lot of uses for something like this, but I'm not sure if anyone else would. Let me know, and, if so, we may hire @weslly to make it. :)
JeffreyWay
 
Posts: 6
Joined: Wed Dec 28, 2011 3:27 pm

Re: Plugin Idea: Would This Be Useful?

Postby sheperson on Sat Jan 07, 2012 6:10 pm

This will really be useful for me too.
sheperson
 
Posts: 1
Joined: Sat Jan 07, 2012 6:09 pm

Re: Plugin Idea: Would This Be Useful?

Postby facelessuser on Sat Jan 07, 2012 7:26 pm

This wouldn't be too difficult of a plugin to write. It would actually be quite straight forward. If I get a little time, I might code it up.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin Idea: Would This Be Useful?

Postby facelessuser on Sat Jan 07, 2012 8:54 pm

I already have the basics in place and example of setting would look like this:

Code: Select all
{
   "commands": {
      "example command 1": [
         {
            "find": "\\bword1\\b",
            "replace": "",
            "greedy": true,
            "case": true
         },
         {
            "find": "\\bword2\\b",
            "replace": "",
            "greedy": true,
            "case": true
         },
      ],
      "example command 2": [
         {
            "find": "\\bword1\\b",
            "replace": "",
            "greedy": true,
            "case": true
         }
      ]
   }
}


I just need to do a little bit of clean up and testing. I will let you know when I am done.

Finds and replaces are done with the find, find_all, and replace commands found in the SublimeText 2 API. They are simple find and replaces. Don't expect anything too fancy.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin Idea: Would This Be Useful?

Postby facelessuser on Sat Jan 07, 2012 9:27 pm

Beta Project is here:

https://github.com/facelessuser/RegReplace

Your desired settings would look like this:

Code: Select all
{
   "commands": {
      "Optimize HTML": [
         {
            "find": "\\stype=\"text\\/(?:css|javascript)\"",
            "replace": "",
            "greedy": true,
            "case": false
         },
         {
            "find": "<!--[\\s\\S]+?-->",
            "replace": "",
            "greedy": true,
            "case": true
         }
      ]
   }
}


Let me know what you think. That is all I plan on doing today :) .
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin Idea: Would This Be Useful?

Postby sublimator on Sun Jan 08, 2012 3:10 am

@facelessuser

The second arg to `view.find` is actually the start `pt` rather than the flags.

Also, `view.find_all` has a signature that's `(pattern, flags, replace_format_string, empty_list_reference)`. It's a somewhat weird/awkward API but the `empty_list_reference` will be filled with renderings of the format string for each found region in order.

When it comes to replacing the regions, you don't have to track offsets manually if you perform the operations in reverse order ( end of document towards beginning). `view.find_all` will return the regions in the order they are found so you just need to reverse them. Python has a handy builtin function `reversed` for this.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Plugin Idea: Would This Be Useful?

Postby facelessuser on Sun Jan 08, 2012 3:25 am

castles_made_of_sand wrote:The second arg to `view.find` is actually the start `pt` rather than the flags.


Whoops, I actually knew this, but still forget it anyways. I will fix it. :). It is really more of a place holder right now, I plan to have it look forward from the current cursor location, but this is a very early implementation. Alpha is probably more accurate than beta, but it will get there.

castles_made_of_sand wrote:Also, `view.find_all` has a signature that's `(pattern, flags, replace_format_string, empty_list_reference)`. It's a somewhat weird/awkward API but the `empty_list_reference` will be filled with renderings of the format string for each found region in order.


Interesting. I will look into this more. I literally threw this thing together while my kids were napping, so I didn't look to deep.

castles_made_of_sand wrote:When it comes to replacing the regions, you don't have to track offsets manually if you perform the operations in reverse order ( end of document towards beginning). `view.find_all` will return the regions in the order they are found so you just need to reverse them. Python has a handy builtin function `reversed` for this.


Clever. I will implement this as well.

Thanks for the input and interest. I will patch it up soon.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin Idea: Would This Be Useful?

Postby jps on Sun Jan 08, 2012 3:39 am

May I suggest that rather than using window.show_quick_panel(), just integrating into the Command Palette instead? The replacements can then be specified in a .sublime-commands file (passing the find and replace strings via the args parameter), and you'll get to reuse the Command Palette key binding, which should be easier on the user.
jps
Site Admin
 
Posts: 3062
Joined: Wed Mar 19, 2008 12:33 pm

Re: Plugin Idea: Would This Be Useful?

Postby sublimator on Sun Jan 08, 2012 4:06 am

>>> Thanks for the input and interest.

np :) I'm actually just trying to `help out` and answer a few questions on the forums, sucking up to Jon, so he'll give me (get|set)_view_position APIs. Ulterior motives haha.

>>> I plan to have it look forward from the current cursor location, but this is a very early implementation

Only real issue you'll have is if you decide to use the replacement format string capabilities of view.find_all then you'll want the interface to be consistent with single selection replace. view.find doesn't have the replace functionality. You could bisect the `view.find_all` regions to find the closest selection to your first selection though (copping the wasted perf on the chin) The other option is integration with find/replace commands somehow. I don't think that's viable with the current state of the api though.

>>> May I suggest that rather than using window.show_quick_panel(), just integrating into the Command Palette instead?

what he said :) You could still use the sublime-settings file though keyed with identifiers for parameters for the case that you want to run a batch (DRY! and all that ...)

eg.
Code: Select all
{
   "replacements": {
      "html5_remove_deprecated_type_attr": [
        {
            "find": "<(style|script)[^>]+type=(\"|')text/(css|javascript)(\"|')[^>]*>",
            "replace": "<\\1>",
            "greedy": true,
            "case": true
         }
      ]
   }
}

You could have the RegReplaceCommand accept a list of `ids`(declared in the sublime-commands file) to do the lookup.
Code: Select all
{
        "caption": "Reg Replace: HTML5: Remove deprecated `@type` for script|style",
        "command": "reg_replace_menu",
        "args"   : {"replacements" : ["html5_remove_deprecated_type_attr"]}
}


The `replacements` list (in either sublime-settings or sublime-commands files) could do sublime-settings lookups on finding a "string" or inline parameterisation for {objects}.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Plugin Idea: Would This Be Useful?

Postby facelessuser on Sun Jan 08, 2012 4:37 am

np I'm actually just trying to `help out` and answer a few questions on the forums, sucking up to Jon, so he'll give me (get|set)_view_position APIs. Ulterior motives haha.


Hope you get it :)

May I suggest that rather than using window.show_quick_panel(), just integrating into the Command Palette instead?


Good idea. I haven't thought everything 100% through yet; kind of picked this one up on a whim, so things will be in flux for a bit until I find what really works. I am completely open to suggestions though and will take them in to account as I mold this into something useful.

Only real issue you'll have is if you decide to use the replacement format string capabilities of view.find_all then you'll want the interface to be consistent with single selection replace. view.find doesn't have the replace functionality. You could bisect the `view.find_all` regions to find the closest selection to your first selection though (copping the wasted perf on the chin) The other option is integration with find/replace commands somehow. I don't think that's viable with the current state of the api though.


Yeah, I haven't played with the find/replace API much yet. I am just going to have to play around with it and see what works. I will look into your suggestions and see what the most viable option is. Using the find_all for the closest might be the best option. Thanks for the suggestions.
facelessuser
 
Posts: 1549
Joined: Tue Apr 05, 2011 7:38 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests