Skip to content
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

Search with regex replacement #3456

Closed
ficristo opened this issue Aug 16, 2015 · 10 comments
Closed

Search with regex replacement #3456

ficristo opened this issue Aug 16, 2015 · 10 comments

Comments

@ficristo
Copy link
Contributor

I tried the search demo here: http://codemirror.net/demo/search.html.
I wanted to replace some char with a newline:

  • pressed Ctrl-Shift-R
  • written <dd> in the text box and pressed enter
  • written \n in text box and pressed enter

Actual result:
<dt id="option_indentWithTabs"><code><strong>indentWithTabs</strong>: boolean</code></dt>
\nWhether, when indenting, the first N*<code>tabSize</code>

Expected result:
<dt id="option_indentWithTabs"><code><strong>indentWithTabs</strong>: boolean</code></dt>

Whether, when indenting, the first N*<code>tabSize</code>

I tried with /<dd>/ but the result was the same.

@marijnh
Copy link
Member

marijnh commented Aug 18, 2015

The input field takes the raw replacement text. Backslash escapes aren't interpreted, and there's no reason to believe that they would be. I agree that it would be useful to be able to insert newlines, but I don't think doing some ad-hoc backslash interpretation is the answer. A better proposal or a clean pull request would be welcome.

@ficristo
Copy link
Contributor Author

The simplest, and maybe silliest thing that comes to my mind, is to detect only the \r and \n and replace them with the CR and LF characters respectively.
If the user writes \\r or \\n (two backslash) it will be inserted \r or \n (one backslash).
I don't think I can help, however where is the code used for this operation?
(I know almost nothing about CodeMirror apart is used in Brackets)

@ficristo
Copy link
Contributor Author

Thanks for patch!
One question though: the replacement with new lines should work only with the regular expression otherwise should work normally, for example inserting exactly the char \ followed by n.
Can you confirm?

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

I don't think I agree. Why should it only work when replacing a regular expression? You might also want to replace some string with a newline.

@ficristo
Copy link
Contributor Author

IMHO there are two way in which find and replace should work:
normal: I search some string and replace them with the one wanted exactly as they are. So if I searched foo and replaced with \n, I expect to see the char \ followed by n instead of foo.
regex: in this case if I searched foo and replaced with \n, I expect to see a new line instead of foo.
But if I replaced with \\n, I expect to see the char \ followed by n instead of foo.

(My english isn't very good, I hope I have been clearer than before)

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

I think the behavior you are proposing is horrible (inconsistent parsing of input in two different situations) and I am not going to change this.

@ficristo
Copy link
Contributor Author

As far as I can tell It is the same behaviour of Netepad++, Sublime Text 2 and Netbeans.

@ficristo
Copy link
Contributor Author

If you really disagree in the behaviour I asked, I think the patch should be reverted: it adds an unexpected behaviour in the "normal" case.
Instead, an help on how to implemented the behaviour I asked using the CodeMirror API would be really appreciated.
Let's say for example I have called this cm.replaceSelection(replaceText), how could I insert the new line?
Thanks for the patience.

@marijnh
Copy link
Member

marijnh commented Aug 20, 2015

I don't see how you are claiming that this is expected behavior in one case and not in an almost identical one. There is no API to implement this from outside, short of reimplementing the search addon. The patch is not going to be reverted.

@ficristo
Copy link
Contributor Author

Because most of code editor work as I said, while as far as can i understand now codemirror has an odd behaviour: that is why I think is unexpected.
Besides \n has a special meaning only with regular expression, in normal find and replace are simply a slash and an en.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants