phitranphitranphitran / regexp-saver

VSCode extension for saving and re-using regular expressions
https://marketplace.visualstudio.com/items?itemName=phi.regexp-saver
MIT License
8 stars 2 forks source link

Problem replacing \n using command palette #12

Open duwejeferson opened 1 year ago

duwejeferson commented 1 year ago

I've downloaded the extesion to use it with a very particular case where i replace \n (written) with a new line.

After creating the RegExp, it ended being:

{
    "label": "\\\\n to \\n",
    "regExp": "\\\\n ",
    "replacePattern": "\\n ",
    "regExpFlags": "gm"
},

But executing this, just make it the same as before, as the replacement is scaped too.

I think the replacement pattern shouldn't be escapped when saving with the command palette.

It ended up working after removing the scape in "replacePattern".

phitranphitranphitran commented 1 year ago

Interesting find. I was able to reproduce your issue - when using the command palette UI to save, entering in \n as the replace pattern results in the settings JSON being "replacePattern": "\\n"

If manually editing the settings JSON, this would work:

 {
    "label": "written \\n to newline",
    "regExp": "\\\\n",
    "replacePattern": "\n",
    "regExpFlags": "g"
},

So at least there's a workaround.

Unfortunately there might not be a way to fix the command palette because of the way vscode's showInputBox works. Entering \ will always result in the string being \\ when using the input box.

I could add an extra step in the code to replace all \\ with \ before saving, but I'm not sure if that would be expected behaviour for users.

duwejeferson commented 1 year ago

@phitranphitranphitran Thanks for the fast response.

Agreed, this could be a breaking change. Perhaps an option to change the behaviour could be added?

Or even easier, make it clear in the docs about the command palette default behaviour.