Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Replacing in the project uses the captured groups of the first match in all cases #1136

Open
msoutopico opened this issue Sep 22, 2020 · 5 comments

Comments

@msoutopico
Copy link

Prerequisites

Description

When batch-replacing parts of the text matched by a regular expression which captures some groups, the captured groups of the first instance are used in all occurrences, instead the groups captured in each occurrence. The issue only happens when finding in the project.

Steps to Reproduce

  1. Have a text file that includes several strings with a common part, e.g. "000".
  2. Press Cmd+Shift+F to launch replace menu in the project (to replace in all files)
  3. Search for something like (.+)000(.+), where the non-captured part is to be removed.
  4. Replace with $1$2 so as to replace the whole matched string with the captured groups (thus removing the common non-captured part).

Expected behavior:

In every occurrence, the text matched is replaced with the groups captured in that occurrence.

Actual behavior:

In every occurrence, the text matched is replaced with the groups captured in the first occurrence. https://youtu.be/d7I5Y4-jaOs

IMAGE ALT TEXT HERE

Reproduces how often:

Every time.

Versions

Atom : 1.51.0
Electron: 5.0.13
Chrome : 73.0.3683.121
Node : 12.0.0

apm 2.5.0
npm 6.14.5
node 10.20.1 x64
atom 1.51.0
python 2.7.16
git 2.11.0

Mac OS X: 10.13.6 (17G14019)

@smashwilson
Copy link
Contributor

Moving this over to the find-and-replace package, as this sounds more relevant there 📬

@smashwilson smashwilson transferred this issue from atom/github Sep 26, 2020
@marehr
Copy link

marehr commented Apr 30, 2021

This is a really annoying bug, can you please fix this?

It worked at some point in time.

@msoutopico
Copy link
Author

How is it going? Will this be fixed soon? It's quite a showstopper for me.

@second-pair
Copy link

+1 - my symptoms are pretty much the same as those shown in the video from @msoutopico.

@heinrichmartin
Copy link

I was about to file the same issue. This looks like a duplicate of #625 that has a nice video embedded, but this issue has the better description.

Notes:

  • This works in normal find&replace, i.e. the issue affects only "Search in Folder".
  • The preview displays the expected result, just the actual result is incorrect.
  • I can reproduce it with a single file.
  • I had the affected file open when I "Replace All". (I have not tested with the file closed, because of Add undo to replace all in project #331.)
  • I have seen this issue in two flavors: captured group reused across the whole file vs. captured group reused only across successive lines
    • Maybe this helps debugging, but don't rely on my wording ("only across successive lines").

Simple example

This is a test.
This is another test.
Wait.
Yet another test.
And another one.
Break.
Yet another one.
And one more test.
More.
Yet another one.
And another one.

Find: (another|one more) (\w+)
Replace: $1 freaky $2
Expected: use reported group per occurrence
Experienced: "another freaky test" everywhere

Variant

This is my real-world issue, but with data stripped. I am not able to reproduce it with this simplified version, but if it is of actual interest, I am willing to help bisecting.

parameters {
  string 
}
stages {
   stage('foo') {
      steps {
         script {
            switch ("${params.bar}") {
               case "debug":
                 LISTFILE_DIR="url1"
                 break
               case "devel":
                 LISTFILE_DIR="url2"
                 break
               default:
                 LISTFILE_DIR="url3"
                 break
            }
         }
      }
   }
}

Find: ^((?: )*) (\S) (indent is 3*n+2)
Replace: $1 $2 (add that missing space character)
Expected: letters s, L, and b remain as well as the rough indentation
Experienced:

  • real data: the s of "string" is not reused, but occurrences of "break" are becoming "Lreak"
  • simplified data: both reporting groups are pinned after the first line, i.e. indent becomes one triple and initial word characters become s in all replaced lines

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

No branches or pull requests

5 participants