Intra-area refactoring in JetBrains

I would like to reorganize the variable inside the function, but only inside this function. Is this possible in a JetBrains environment?

Example:

var global = 0; function func1 (val) { if (val === global) { doSomething(); } else if (val * 2 === global) { doSomethingElse(); } else { doSomethingElseEntirely(); } } function func2 (val) { if (val === global) { doSomething(); } else if (val * 2 === global) { doSomethingElse(); } else { doSomethingElseEntirely(); } } 

If I try to change the global variable inside func1 through refactoring, it will be changed in the entire scope of global regions, therefore in func2 . I would like to prevent this. Is it possible?

+1
source share
2 answers

Here is a simpler workaround.

webstorm refactoring

  • Add the dummy var global to func1 .
  • Use the Refactor> Rename tool ( Shift + F6 ), selecting any global event in func1 .
  • Delete the line (place the cursor on the line and press Ctrl + Y ) added in step 1.
+1
source

As far as I know, the β€œcurrent file” is the smallest possible area.

Workaround:

To achieve what you want with the least effort, I suggest using a regular search.

  • Search "global" (Strg + F)
  • place the cursor before or after the first appearance of the "global" in your function (to get the correct starting point and remove focus from the search field)
  • press "Add selection for next event" (or Alt + J) as often as needed
  • rename all occurrences at once by entering a new name

Some kind of manual work is needed here, but I consider it the fastest way for large functions.

0
source

Source: https://habr.com/ru/post/1247104/


All Articles