Wrap an existing variable to be the argument to a newly-typed method call in IntelliJ code editor - intellij-idea

I have this:
this.label = label;
I want this:
this.label = Objects.requireNonNull( label );
Is there a shortcut way to edit such a line rather than me typing in the Objects.requireNonNull() followed by cut/paste or dragging the variable to inside the parentheses?
I can imagine selecting the variable (label in this example), then pressing some keystroke that lets me type the method call, and upon hitting Return puts that var within the parens.

Surround live template
You can create a surround live template.
Surround templates wrap a block of the selected code with the text specified by the user. For example, T expands into a pair of tags, for which you can specify a name. You can also select a block of code, then press Ctrl+Alt+J to open the Select Template popup and select the T template to wrap the selection with a pair of tags.
Here is an example of creating your desired Objects.requireNonNull, here with abbreviated name ornn.
To invoke your live template:
Select the text you want to surround.
Press the keystroke currently designated for live templates.
You discover that needed keystroke by looking at Preferences/Settings > Keymap > Other > Surround with Live Template….
That shortcut would be…
macOS: Option + Command + J
Windows: Control + Alt + J
Structural Search
Another option would be to configure Structural Search and Replace inspection with the quick fix, then apply it via the Alt+Enter on the matched code.

Related

Intellij Idea - complete next parameter shortcut

Example:
Author author = new Author("Jack", "Daniels");
In Exlipce when filled "Jack" and pressed "enter" - it automatically went to second parameter and let me fill second one. In Intellij I always have to click with mouse, or use arrow keys.
Is there a shortcut in Intellij to make it quicker? I can't find one myself.
Since IntelliJ 2018.2
You can now jump outside the closing bracket or quote with Tab.
(from: https://blog.jetbrains.com/idea/2018/07/intellij-idea-2018-2-macbook-touch-bar-java-11-breakpoint-intentions-spring-boot-version-control-and-more/)
Basically it means if you have:
Author author = new Author("Jack<cursor>");
And you press Tab, the cursor is placed like this:
Author author = new Author("Jack"<cursor>);
Note: it doesn't matter where the cursor is initially, as long as you are somewhere inside the quotes when you press Tab the cursor is moved outside and placed behind it. If you want to enter the next parameter you have to manually type the comma and String.
The option can be found under: Settings -> Editor -> General -> Smart Keys and is called Jump outside closing bracket/quote with Tab
There are some other useful options there as well like insert pair quote and insert pair brackets.
If you want to go more advanced you can have a look into Live Templates.

intellijidea code completion not new variables

I'm using Android Studio and I have code completion enabled as in picture.
I have enabled "Autopopup code completion" and "Insert selected variant by typing dot, space, etc..".
I want to write:
Drawable d = new BitmapDrawable(....);
I write "Dr", appears the popup, I click the space bar, and it writes "Drawable". It's all ok.
Then I write "d", appears the popup with "drawable" suggested, I click the space bar, and it writes "drawable".
But I want to write "d".
Is it possible to change this option applying only to classes and methods and not to new variables?
There is not a way to have code complete only apply to classes and methods and not to new variables. You have three options:
Turn off the Insert selected variant by typing dot, space, etc." options. You will then need to use Enter (to insert) or Tab (To overwrite) when you want to select an item from auto complete. The . (period/dot) key will still work when completing classes if you want to invoke a static member. (This is likely the best of the three choices and is the default behavior.)
After you type d for the variable name, hit Esc to close the auto complete popup before you hit the Space.
Turn off "Autopopup code completion" so that you have to manually activate it each time via Ctrl+Space

Returning to usage's location after Intellij IDEA "Create on Usage" intention action

Suppose I type a Java method call with an argument that I intend to make into a field:
knownObject.knownMethod(newField);
At this point, newField is highlighted as compilation error. I can press Alt+Enter and select "Create Filed 'newField'" from the menu.
That brings me up to the beginning of the class file where other fields are defined.
I can press Enter to confirm the new field's type.
Now I'd like to go back to my knownMethod() call and continue coding. How do I do that?
Bonus question: in the above situation, Ctrl+Shift+Backspace may help because I just edited the the knownMethod() call. What if I decide to first type in multiple arguments to the method? How do I get back to the argument I've just created a field for in that case?
For getting back to a previous caret location the Navigate > Back menu item usually works (CtrlAltLeft Arrow on Windows). Another option may be to use Edit > Find > Find Usages on the field (enable the Skip results tab with one usage checkbox setting)

IntelliJ IDEA find matching parenthesis / bracket

Is there any keyboard shortcut to navigate from one parenthesis ( to the matching one ) in IntelliJ?
Or even to "jump to the next opening/closing parenthesis (/) from wherever my cursor is"?
And, if there's no way to do it 'natively', are there any plugins that could add this functionality quickly and easily?
EDIT: the best solution I've found is added as a comment in liango's answer below, namely to put the cursor on the first or last brace and then press CTRL-W twice. Not ideal, but it does the job!
You can use Ctrl + [ and Ctrl + ] to navigate to a code block's start and end.
You can also use Ctrl + Shift + M to navigate between the start and end of a code block.
On Mac, you can use Option + Cmd +[ and Option + Cmd + ], and Ctrl + M
Source: https://www.jetbrains.com/help/idea/2016.2/navigating-to-braces.html
use CTRL-W to expand the selection within a bracket, use repeatedly if needed to select further out, then use the arrows.
CTRLSHIFTM is the Windows/Linux default shortcut for action "Move caret to matched brace". (For other environments see here and use the Shortcuts dropdown in the top right of the page.)
Or if desired, the shortcut keys can be changed as follows:
Go to Settings -> Keymap -> Editor Actions, or just type the name in the search box.
Here's the latest and greatest as of version 2020.1
To move caret between matching code block braces, press Ctrl+Shift+M.
To navigate between code blocks, press Ctrl+Shift+[ or Ctrl+Shift+].
From the documentation
Navigate with the caret
To navigate backwards, press Ctrl+Alt+← Left. To navigate forward, press
Ctrl+Alt+→→ Right.
To navigate to the last edited location, press Ctrl+Shift+Backspace.
To find the current caret location in the editor, press Ctrl+M. This
action might be helpful if you do not want to scroll through a large
file.
However, you can press the Up and Down arrow keys to achieve the same
result.
To highlight a word at the caret you are trying to locate, select Edit
| Find | Next Occurrence of the Word at Caret from the main menu.
To see on what element the caret is currently positioned, press Alt+Q.
To move caret between matching code block braces, press Ctrl+Shift+M.
To navigate between code blocks, press Ctrl+Shift+[ or Ctrl+Shift+].
To move the caret forward to the next paragraph or backward to the previous one, press Ctrl+Shift+A and search for the Move Caret Forward a Paragraph or Move Caret Backward a Paragraph action.
You can also select a text and then move the caret forward or backward to a paragraph. Press Ctrl+Shift+[ and search for the Move Caret Forward a Paragraph with Selection or Move Caret Backward a Paragraph with Selection action.
You can use Ctrl-[ and Ctrl-].

How to change Code style for adding space around parameter within an IntelliJ environment?

I accidentally changed something within the code style in my PhpStorm. Now, when I press Ctrl + Shift + F for formatting, a space is placed in front and after parameters in a method call within a function.
How can I change this back?
Go to Editor > PHP > Spaces > Within, see if "Function declaration parantheses" or "Function call parantheses" are checked.
In tab "Spaces", option "Within", desactive "Method declaration parentheses".