Sublime Forum

Plugin Idea: Would This Be Useful?

#14

Okay, reworked the code. I am now taking full use of the find_all command. This means you can do some more complicated substitutions. I went ahead and committed as an example the replacements requested on the opening page.

{ "replacements": { // Example replacements "html5_remove_deprecated_type_attr": { "find": "(<(style|script)^>]*)\\stype=(\"|')text/(css|javascript)(\"|')(^>]*>)", "replace": "\\1\\6", "greedy": true, "case": false }, "remove_html_comments": { "find": "<!--\\s\\S]+?-->", "replace": "", "greedy": true, "case": true } } }

To access the commands, you simply define a command in the command palette. I have provided an example, and you can chain the replacements.

	{
		"caption": "Reg Replace: Example - Cleanup HTML",
		"command": "reg_replace",
		"args": {"replacements": "html5_remove_deprecated_type_attr", "remove_html_comments"]}
	}
]

So the quick_panel is removed now.

Non-greedy searches are still limited to the first in the entire file. I plan on applying non-greedy changes from the cursor forward and allow wrapping, but not today.

As always, feedback, bug reports, and suggestions are appreciated.

0 Likes

#15

For people looking for a link (like me :smile: ):https://github.com/facelessuser/RegReplace

0 Likes

#16

You could probably even put a scope selector restriction in.

I’ve always wished you could optionally pass a list of regions and/or a scope selector to the find_all api to further restrict the finds. If find_all returned regions in the ordering of the regions you passed it, eg ([R(caret_pos, view.size()), R(0, caret_pos)] seems like you could easily enough implement wrapped search that way. To restrict your search to a bunch of particular regions currently isn’t as easy as it could be.

Some feedback of which regions were replaced would likely come in handy.

0 Likes

#17

[quote=“castles_made_of_sand”]You could probably even put a scope selector restriction in.

I’ve always wished you could optionally pass a list of regions and/or a scope selector to the find_all api to further restrict the finds. If find_all returned regions in the ordering of the regions you passed it, eg ([R(caret_pos, view.size()), R(0, caret_pos)] seems like you could easily enough implement wrapped search that way. To restrict your search to a bunch of particular regions currently isn’t as easy as it could be.

Some feedback of which regions were replaced would likely come in handy.[/quote]

The regions are returned in order and caret position is easy to calculate, so it is easy enough to at least filter the results. Not really worried about the calculating after the cursor, more just need to get around to it.

In what way would you use the scope selector? I am just curious in what context you would use this. I have no problem adding it, but I would like to better understand it so I can test the scenario.

Added the link in an earlier post, but yeah, I probably need to open up a specific thread for this sometime soon.

0 Likes

#18

easy enough to at least filter the results

Yeah, it’s just tedious. It’s just such a common task that it’d be nicer (and likely a lot more efficient) if there was a more direct api for it. You’ll probably end up implementing replace restricted to selections for example.

In what way would you use the scope selector?

The scope selector would be used most commonly for restricting to, or filtering, string and comment scopes I’d imagine.

0 Likes

#19

I agree.

Ahh. That makes sense. I will add a simplistic filtering based on scope. Initially I will make it include or exclude filtering based on any part of the target exhibiting the scope; it will also be optional parameter that can be left out if not desired. If needed later, I can add options for “absolute filter” (meaning that the entire selection must be completely contained within the specified scope type). I am going to start out as simple as possible and only ramp it up to more complicated rule sets if it becomes needed. Even then, I will probably have to draw a line somewhere so I am not implementing some kind of regex for scopes. I surely see why you desire this feature now; I do hate it when a regex find targets inside comments and/or strings that I just don’t care about.

0 Likes

#20

So, as I get time, these are the current tasks on my plate:

-Feedback on replaced regions
-Non-greedy searches relative to the cursor
-Scope filtering of results

If you don’t see a feature you really want, run it by me, and I will evaluate the request and possibly add it to my todo list.

0 Likes

#21

I just noticed that RegReplace has been addded to the SublimeText Group, and therefore added to Package Control. That version is kind of half baked and incomplete, I personally would discourage use of that branch for now. While people are free to fork the project as much as they want, I think forking it to the SublimeText group is not a wise choice at this point. I am not able to personally maintain that branch. I am hoping by the end of the week, I will have the important features in, and will personally add the package to Package Control myself.

I would like to keep it out of SublimeText group until I am to a point where I wish to longer support the branch, and then it can be supported by the community or whatever. Trying to get pull requests and such on the SublimeText group will not allow me to be as nimble as I would like; especially since the plugin is still in development. Also, this will be confusing to people when I do personally add my branch to Package Control and there will be 2 versions of RegReplace.

I will try and send a message to the maintainer of that group.

0 Likes

#22

I just deleted the package per your request.
[size=85]FYI: I’ve no idea how the package ended at Sublime Text.[/size]

0 Likes

#23

Thanks so much! I have no idea either. It must really want to be released :smiley:

0 Likes

#24

I want to say I disagree forks (to sublimetext organization) of packages without the authorization of the package “owner”

edit: I also sent to you an invitation to “collaborate” there.

0 Likes

#25

my bad … I forked it to my account the other day … and accidentally pressed the ST organisation first but didn’t think the request completed …

0 Likes

#26

[quote=“tito”]I want to say I disagree forks (to sublimetext organization) of packages without the authorization of the package “owner”

edit: I also sent to you an invitation to “collaborate” there.[/quote]

I agree, and thanks for the invite.

No worries. It happens. It just surprised me.

0 Likes

#27

I wanted to see if anyone has an opinion on this. I added scope filtering locally on my machine the other day, but I am trying to figure out the best way to represent the functionality.

Basically I have positive and negative filtering (must include and must exclude scope respectively), but on top of that, I have further defined the difference: include at least one instance of scope vs entire match must be of scope type; alternately on the negative side: one instance of scope disqualifies match, and only the entire match of a scope disqualifies match.

My questions is the best way to represent this to be the most intuitive. Currently I have:

  • Any instance of scope qualifies match: scope.name
  • Entire match of scope qualifies match: !scope.name
  • Any instance of scope disqualifies match: -scope.name
  • Entire match of scope disqualifies match: -!scope.name

Thoughts, opinions?

0 Likes

#28

Also looking for good useful regex tasks to include by default. If you have any suggestions, post them here. I may not include all of them, but I will evaluate them and include the ones I feel would be the most useful.

0 Likes

#29

I just downloaded Sublime yesterday and I am loving it. I’ve been playing with the plugin stuff off and on today and I think I’m missing something. I think I understand how the plugins work but for the life of me I can’t find how to pass arguments to the view.run_command function. In other words, what’s the command prompt syntax to run regreplace? When I run it the script always dies at “if len(replacements) > 0:” because I’m, apparently, not passing anything in.

Any help would be appriciated.

0 Likes

#30

[quote=“brentdooley999”]I just downloaded Sublime yesterday and I am loving it. I’ve been playing with the plugin stuff off and on today and I think I’m missing something. I think I understand how the plugins work but for the life of me I can’t find how to pass arguments to the view.run_command function. In other words, what’s the command prompt syntax to run regreplace? When I run it the script always dies at “if len(replacements) > 0:” because I’m, apparently, not passing anything in.

Any help would be appriciated.[/quote]

First off, you must have the replacement(s) you want defined in the settings file(reg_replace.sublime-settings). If you are adding your own, I recommend to copy the settings file to the User folder under the Packages folder and modify it there so your settings will persist when you upgrade the plugin.

For example:

{ "replacements": { "remove_html_comments": { "find": "<!--\\s\\S]+?-->", "replace": "", "scope_filter": "!comment"], "greedy": true, "case": true }, "trailing_spaces": { "find": " \\t]+$", "replace": "", "greedy": true, "case": true } } }

You can then define a command in the Defaults.sublime-commands file. Do the same as your settings file if you are going to make changes that you wish to persist.

Follow the same convention as your command file.

So, if I have a replacement defined in my settings file called “trailing_spaces”, I would call it via the command line in the console like this (you can also call this from other plugins as well similarly; you just need a valid view to call it):

sublime.active_window().active_view().run_command("reg_replace", {"replacements" : "trailing_spaces"]})

or define it in the command file like this (this will make the command accessible via the command pallet):

{ "caption": "Reg Replace: Example - Remove Trailing Spaces", "command": "reg_replace", "args": {"replacements": "trailing_spaces"]} }

Notice the arguments for the command are contained inside a dict, and the “replacements” argument is always in an array (you can add other regex tasks to the array to perform multiple regex in a sequence).

I hope this makes it a little more clear.

Note this is still in development, so please note there may be some bugs. I have a fairly big commit comming tonight that should complete most of the currently planned features…assuming there are no bugs :smile:.

0 Likes

#31

Very nice. Worked on the first try. I didn’t put my replacement in brackets.

Thanks for the great example and plugin!

0 Likes

#32

A lot of additions:

-Add ability to filter results by scope
-Add regex feedback in status bar (or optionally a panel)
-Add setting to enable panel feedback
-Add non-greedy replace relative to cursor (allow wrapping)
-Add a input panel for quickly creating and running a sequence
-Add trailing space removal “replacement”
-Change default regex sequence commands

I have done some general testing, but do let me know if you find some bugs. I am going to do some heavier testing in the next couple to shake out any bugs and probably do an initial release, so if you have any requests, suggestions, or issues let me know.

github.com/facelessuser/RegReplace

0 Likes

#33

By the way, the input sequencer command simply allows you to create a regex sequence on the fly via an input panel.

So if you have a couple of replacements defined in the settings file, you can just enter the names separated by commas and run the sequence of replacements.

Was using it during testing, but then I figured why not leave it in.

0 Likes