-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
command: Fix replace to be able to insert '$' #2954
Conversation
As already mentioned in #2951, it would be more common in case a regex search is used to implicit replace |
I think we should only perform this replacement if |
Correct me if I'm wrong, but in regex the character |
Yes but the replaceStr is used with regexp.Expand and is not itself a regular expression so it shouldn't necessarily use |
So what you're basically referring to is to still keep the "template" functionality of the But at this point we can be sure, that this will cause additional user/community issues created, because with a regex-search and a normal replace string (non-regex) you can't simply insert a The current user base has already problems with the given search mechanism itself and this then remaining template functionality will cause further confusion. I've no problem to move that line, but I don't want to be the one explaining this over and over again in the issues. 😉 |
I think moving it inside the The replace command is confusing for new users who don't realize it's using the full power of regex, but capture groups are a very useful feature for power users and it would be a shame to take it away without providing an alternative. A better fix would be to completely remove regular expression functionality from |
As already mentioned, it was never the intention to remove any functionality targeting regex/power users.
Anyway, if you both insist that the correction shall be placed within the Maybe it's worth to refer to |
runtime/help/commands.md
Outdated
In case the search is done non-literal (without `-l`) the character `$` can't | ||
be simply added, since `Regexp.Expand()` is used in the background to add | ||
more complex template replacement functionality. To add a `$` a double `$$` | ||
must be used. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't say much to a user who doesn't speak Go. How about a summary like:
In case the search is done non-literal (without `-l`) the character `$` can't | |
be simply added, since `Regexp.Expand()` is used in the background to add | |
more complex template replacement functionality. To add a `$` a double `$$` | |
must be used. | |
In case the search is done non-literal (without `-l`), the 'value' | |
is interpreted as a template: | |
* `$3` or `${3}` substitutes the submatch of the 3rd (capturing group) | |
* `$foo` or `${foo}` substitutes the submatch of the (?P<foo>named group) | |
* You have to write `$$` to substitute a literal dollar. |
I don't think we need to elaborate on exact lexing e.g. "$10 is equivalent to ${10}, not ${1}0" — if user needs to care about that they're better off just using ${foo}.
(Well, perhaps do mention "as a Go Regexp.Expand template:" — even if one don't speak Go, knowing it's an existing convention can be reassuring vs. "an ad-hoc thing of this editor".)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P.S. while we're here, perhaps 'value' is not the best term 🤔
I'd actually say it'd be clearest to document 2 separate usage lines, like some man pages do:
replace 'regexp' 'template' '-a'?
replace 'literal_text' 'replacement' '-l'
but not sure it fits the doc's style, and it's hard to explain how the flags can be combined (is it '-l' '-a'? '-l -a'? '-la'?)
[Out of scope for this PR: perhaps the literal/regexp modes are sufficiently different that they deserve splitting a separate command, e.g. replacestring
? Also provides some discoverability by tab completion? 🤔]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd actually say it'd be clearest to document 2 separate usage lines, like some man pages do:
Unfortunately the behavior isn't that common as expected and I think we should solve that in the first place than precising corner cases within the documentation.
perhaps the literal/regexp modes are sufficiently different that they deserve splitting a separate command
Many GUI editors treat them in the same way with a search and an replace mask, while you've to set a checkbox for regex capabilities.
I wouldn't separate them, but they need to behave as the user would expect and currently it's everything else than obvious.
My suggestion would be to prevent the usage of Expand
in a literal search. Just do a plain replace. But this is then a task for #2963. 🤔
FWIW a more "proper" implementation (preventing usage of Although both implementations produce the same result. |
I think after closing this PR you will need to close this too #2440 |
Co-authored-by: Beni Cherniavsky-Paskin <cben@redhat.com>
On top of JoeKar's fix.
I don't think so. It doesn't freeze or crash. But test it on your own and you will see that the result maybe doesn't match your expectation. 😉 |
Yep, #2440 is a different issue, independent of this PR, since it is about |
This will introduce at least the capability to insert
$
s, but I'm unsure, if every possible scenario can be fulfilled with that, because the user input can be complex, the expectation for the two different replace modes could be different and not everybody reads the documentation.Fixes #2951