2

I have this:

Arrays.asList(from(A, 14), from(A, 21), ...

What I need is:

Arrays.asList(of(from(A, 14), 1), of(from(A, 21), 2), ...

The call from(A, number) should be turned into of(from(A, number), anotherNumber).

In other words: I have to update a lengthy list of such from() calls by enclosing them within an of() and adding a second parameter. Ideally, that second parameter would simply count upwards.

Is there a way to that with IntelliJ refactoring tools? ( instead of doing it all manually )

And note: I am not asking for a tools recommendation. I am asking whether a known tool supports a specific refactoring situation.

2
  • 1
    I personally don't like statically importing common method names such as of. The method name by itself is meaningless in English. Also, it could easily be confused with List.of, Stream.of, LocalDate.of, etc. Commented Jan 24, 2019 at 15:29
  • 1
    @DodgyCodeException This is for a unit test, so context is very clear ... but I am already glad that I named the first static one "from", and not "of" of of of. Commented Jan 24, 2019 at 15:30

3 Answers 3

2

You can highlight from( and use the "select next occurrence" hot key. Once you have selected all occurrences just replace it with of(from. Once you are done adding of you can use the "alt + left arrow key" to move the cursor to the position where you want to add the number OR use the "select next occurrence" by highlighting the ),.

On Mac the hot key is "CTRL + G" and on Windows\Linux "ALT + J". Here is a list of the hot keys https://resources.jetbrains.com/storage/products/intellij-idea/docs/IntelliJIDEA_ReferenceCard.pdf

It is still a bit manual, but beats doing it one by one.

Sign up to request clarification or add additional context in comments.

1 Comment

That is definitely better than doing it all manual (like putting them in distinct rows, and use the column mode, what I did first).
2

You can try the following:

  • Extract method with replacing the duplicates for from(A, param)
  • Inside the extracted method write something like of(from(A, param), NNN)
  • Inline method
  • Replace NNN with numbers you need (this has to be performed manually)

If there is some formula that can calculate anotherNumber based on number, you can use it instead of NNN.

Comments

2

"Replace structurally" can do some of what you need.

  1. Select Edit > Find > Replace Structurally...
  2. Enter from($a$, $b$) as the search template
  3. Enter of(from($a$, $b$), i) as the replacement template
  4. Choose Scope: Current File (or Selection, if you prefer)
  5. Hit Find
  6. Hit Replace all

Assuming i is undefined you'll then be left with lots of errors. You can cycle through the errors with F2 and replace the undefined i with the values you want.


Bonus tip: on a Mac, run seq 1 100 | pbcopy at a terminal to put the numbers 1-100 into your clipboard. Then, with multiple cursors in IntelliJ, hit Paste. 1 will be pasted at the first cursor, 2 at the second, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.