Search code examples
regexdartvisual-studio-coderegex-replace

How to replace a part of a string maintaining the other one intact in IntelliJ or VSCode?


I'm developing a Flutter app.

I have used a function written by me to calculate the sizes of every stuff. This was called scaleAdapter and its signature was this one:

scaleAdapter(aNumber)

This is used everywhere in the project.
I recently however discovered that this approach is not useful in many cases and I want to migrate to the flutter_screenutil package which is very good. The fact is that now in every file I should substitute

scaleAdapter(aNumber) with aNumber.aUnit
where aUnit is:

  • w for horizontal
  • h for vertical values
  • sp for size values
  • r for round values (such as border-radius)

in a concrete example

SizedBox(
  width: scaleAdapter(22),
  height: scaleAdapter(22),
  child: SvgPicture.asset(
    'assets/images/svg/yellow/prezzo.svg',
  ),
),

Now it must become:

SizedBox(
  width: 22.w,
  height: 22.h,
  child: SvgPicture.asset(
    'assets/images/svg/yellow/prezzo.svg',
  ),
),

Since it is insane to think about manually substituting everything I was wondering if there is some function in IntelliJ or also VSCode that can do the work for me.

I am able with IntelliJ to search for a specific pattern using a RegEx, (for example by searching width: scaleAdapter\(.\) I find all the width values with the old function) but I don't know how to keep the original value when substituting.

Any help please?


Solution

  • In VSC

    regex search for: width: scaleAdapter\(([^)]+)\)

    replace with: width: $1.w

    similar for the other cases of h, sp and r