Sublime Forum

Plugin Idea: Would This Be Useful?

#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

#34

I downloaded and ran a few tests and everything seems to work well. I have two more question for you and any help would be appreciated. Iā€™m editing large XHR responses with lots of ā€œ\nā€ and ā€œ\tā€ control characters. I need to replace the "\n"s with a sublime line breaks and the "\t"s with either tabs or a few spaces.

  1. Is it possible to string together multiple replaces into one command or do I need to use 2 commands?

  2. Do you know how to get the replace to pick up ā€œ\nā€ and ā€œ\tā€ strings? Iā€™ve tried everything I can think of and itā€™s a hard thing to search for.

Iā€™ll keep working with it and I will run some more test cases. Thanks in advance to any help.

0 Likes

#35

[quote=ā€œbrentdooley999ā€]I downloaded and ran a few tests and everything seems to work well. I have two more question for you and any help would be appreciated. Iā€™m editing large XHR responses with lots of ā€œ\nā€ and ā€œ\tā€ control characters. I need to replace the "\n"s with a sublime line breaks and the "\t"s with either tabs or a few spaces.

  1. Is it possible to string together multiple replaces into one command or do I need to use 2 commands?[/quote]

Do you mean once a specific region is targeted for replace, you want to run multiple regex on just that region, or just chain multiple regex in one command? I am not sure I understand; could you elaborate?

This is perfectly allowable; is this not what you are looking for?

"args": {"replacements": "trailing_spaces","another_task","yet_another_task"]}

[quote=ā€œbrentdooley999ā€]2) Do you know how to get the replace to pick up ā€œ\nā€ and ā€œ\tā€ strings? Iā€™ve tried everything I can think of and itā€™s a hard thing to search for.

Iā€™ll keep working with it and I will run some more test cases. Thanks in advance to any help.[/quote]

What kind of file line endings are you dealing with? Windows? or Unix/Linux?

Windows you may need to do something like /n/r for line endings.

If you can post an example file here and explain what exactly you are trying to target, I may be able to help you.

0 Likes

#36

Iā€™m on a Mac. Iā€™m still a little new to developing on a Mac so line feeds and whatnot still throw me a bit. Iā€™m doing web development and sometimes I have to execute Javascript embedded in a JSON reply. Hereā€™s an example of the returned code. I have the ability to turn on a debug mode to leave the line feeds and tabs.

if(Ext.getCmp('report_toolbar'))\n{\n\tvar btn = new Ext.Button({\n\t\tid:'btn_0', \n\t\ttext: 'Labor Expense GL', \n\t\tenableToggle: true, \n\t\tpressed: true,\n\t\ttoggleGroup: 'btn_grp',\n\t\tlisteners:{\n\t\t\tclick:function(){\n\t\t\t\tExt.getCmp('win_0').show();\n\t\t\t}\n\t\t}\n\t});\n\tExt.getCmp('report_toolbar').add(btn);\t\n\tExt.getCmp('report_toolbar').doLayout();\n\tvar ismaximizable = true;\n\tvar isminimizable = true;\n}

To debug it I need it in a more readable format like this.

if(Ext.getCmp('report_toolbar')) { var btn = new Ext.Button({ id:'btn_0', text: 'Labor Expense GL', enableToggle: true, pressed: true, toggleGroup: 'btn_grp', listeners:{ click:function(){ Ext.getCmp('win_0').show(); } } }); Ext.getCmp('report_toolbar').add(btn); Ext.getCmp('report_toolbar').doLayout(); var ismaximizable = true; var isminimizable = true; }

Also, I will try that other thing once I get this going. Thanks!

0 Likes

#37

Ahh, you are converting the the symbol ā€œ\nā€ to a literal newline, etc.

You have to escape your escapes so the slash will be literal. I am also telling it here that none of the target can be part of a string; ā€œ\nā€ inside a string is probably what you really want it to be. Hope that helps you.

"convert_symbol_newline_to_literal": { "find": "\\\\n", "replace": "\n", "scope_filter": "-!string"], "greedy": true, "case": true }, "convert_symbol_tab_to_literal": { "find": "\\\\t", "replace": "\t", "scope_filter": "-!string"], "greedy": true, "case": true }

Then you can chain these two together in a command.

0 Likes