atom / find-and-replace

Find and replace in a single buffer and in the project
MIT License
242 stars 219 forks source link

Project level regex find-replace replaces literal $1, $2 #1110

Closed rdpoor closed 4 years ago

rdpoor commented 4 years ago

Prerequisites

Description

A Project-level regex find/replace inserts literal $1, $2, etc rather than the appropriate regex field.

Steps to Reproduce

  1. Copy and save the following text into "foo.c" in a new directory. if (tstat_fvars()->curr_setpoint[HEAT_EN] > new_tptr->heat_limit) { tstat_fvars()->curr_setpoint[HEAT_EN] = new_tptr->heat_limit; }
  2. Open Atom on the directory as a project
  3. Open Project find
  4. For the search string, type tstat_fvars\(\)->curr_setpoint\[(.+?)\] = (.+?);
  5. For the replacement string, type tstat_set_curr_setpoint($1, $2);
  6. Click on Find All
  7. Observe that the Find Results window displays the correct substitutions
  8. Click on 'Replace All' and 'OK' in the replacement dialog.

Expected behavior:

Before the substitution, foo.c contains:

if (tstat_fvars()->curr_setpoint[HEAT_EN] > new_tptr->heat_limit) { tstat_fvars()->curr_setpoint[HEAT_EN] = new_tptr->heat_limit; }

After the substitution, foo.c should contain:

if (tstat_fvars()->curr_setpoint[HEAT_EN] > new_tptr->heat_limit) { tstat_set_curr_setpoint(HEAT_EN, new_tptr->heat_limit); }

Actual behavior:

But instead, foo.c ends up with: if (tstat_fvars()->curr_setpoint[HEAT_EN] > new_tptr->heat_limit) { tstat_set_curr_setpoint($1, $2); }

Reproduces how often:

Appears to be 100% reproducible.

Versions

`$ atom --version

Atom : 1.43.0 Electron: 4.2.7 Chrome : 69.0.3497.128 Node : 10.11.0

$ apm --version internal/child_process.js:357 throw errnoException(err, 'spawn'); ^ Error: spawn UNKNOWN at ChildProcess.spawn (internal/child_process.js:357:11) at spawn (child_process.js:528:9) at EventEmitter. (C:\Users\r\AppData\Local\atom\app-1.43.0\resources\app\apm\lib\apm-cli.js:169:17) at process._tickCallback (internal/process/next_tick.js:61:11)

$ systeminfo

Host Name: NUC-10 OS Name: Microsoft Windows 10 Pro OS Version: 10.0.18362 N/A Build 18362 OS Manufacturer: Microsoft Corporation OS Configuration: Standalone Workstation OS Build Type: Multiprocessor Free Registered Owner: esnowden@nsa.gov (just kidding) Registered Organization: N/A Product ID: 00330-80000-00000-AA681 Original Install Date: 9/15/2019, 8:25:45 PM System Boot Time: 1/27/2020, 12:46:03 PM System Manufacturer: Intel(R) Client Systems System Model: NUC8i5BEK System Type: x64-based PC Processor(s): 1 Processor(s) Installed. 01: Intel64 Family 6 Model 142 Stepping 10 GenuineIntel ~2304 Mhz BIOS Version: Intel Corp. BECFL357.86A.0064.2019.0213.1122, 2/13/2019 Windows Directory: C:\WINDOWS System Directory: C:\WINDOWS\system32 Boot Device: \Device\HarddiskVolume2 System Locale: en-us;English (United States) Input Locale: en-us;English (United States) Time Zone: (UTC-08:00) Pacific Time (US & Canada) Total Physical Memory: 32,637 MB Available Physical Memory: 21,799 MB Virtual Memory: Max Size: 37,501 MB Virtual Memory: Available: 26,855 MB Virtual Memory: In Use: 10,646 MB Page File Location(s): C:\pagefile.sys Domain: WORKGROUP Logon Server: \NUC-10 Hotfix(s): 8 Hotfix(s) Installed.

                       [02]: KB4515383
                       [03]: KB4516115
                       [04]: KB4520390
                       [05]: KB4521863
                       [06]: KB4524569
                       [07]: KB4528759
                       [08]: KB4528760

Network Card(s): 4 NIC(s) Installed. 01: Hyper-V Virtual Ethernet Adapter Connection Name: vEthernet (Default Switch) DHCP Enabled: No IP address(es)

                             [02]: fe80::524:2ec5:dfb4:d699
                       [02]: Intel(R) Wireless-AC 9560
                             Connection Name: Wi-Fi
                             DHCP Enabled:    Yes
                             DHCP Server:     192.168.1.1
                             IP address(es)
                             [01]: 192.168.1.10
                             [02]: fe80::ece1:22b8:1695:5f11
                       [03]: Intel(R) Ethernet Connection (6) I219-V
                             Connection Name: Ethernet
                             Status:          Media disconnected
                       [04]: Bluetooth Device (Personal Area Network)
                             Connection Name: Bluetooth Network Connection
                             Status:          Media disconnected

Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed. `

Additional Information

Note that an identical File-level regex find-replace works without trouble and does not manifiest the issue described here.

christalee commented 4 years ago

I just ran into this; here's a screenshot showing the correct replacement in the Find and Replace preview and the wrong replacement in the confirmation dialog.

Screen Shot 2020-02-06 at 4 39 04 PM
asvela commented 4 years ago

Reproduced in project replace. I was only using one regex group:

find: some_text_(.*) replace: other_text_$1

results in the literal other_text_$1 in every instance of some_text_(.*) in the project.

Also seeing the same as @christalee, the window displays the correct changes in all instances, so was a little surprised when I suddenly had lots of $1 literals in my code.

However, when trying the same in buffer find-replace, the result is interestingly that some_text_(.*) is found, but when replace is pressed nothing changes and the panel is now showing not found.

$ atom --version
Atom    : 1.43.0
Electron: 4.2.7
Chrome  : 69.0.3497.128
Node    : 10.11.0

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G3020
drgrigg commented 4 years ago

I came here to report the same bug. It's very annoying, particularly if you don't spot the bad result immediately.

lkashef commented 4 years ago

Fixed by https://github.com/atom/text-buffer/pull/318