intellij wrapping brackets around if-else - intellij-idea

Coming from a language like python, I often forget to wrap parenthesis around if conditions when writing Java.
Is there a parenthesis completer built into IntelliJ? If not, how do I best work around this issue? (short of manually wrapping with parenthesis myself -:))

When you will start typing if code completion will ocurs. Push enter - your carret will land inside bracket.
Use surrounding blocks of code. You can first write boolean condition then press shortcut for Surround With option (on windows/linux by default it is Ctrl + Alt + T) and choose if(expr).
Or if you want to only surround with paranthesis, then use (Surround With) and then choose (Curly Braces).
You can use predefined live template with ifn which will initialize if block with comparison to null. You can also configure your own template.
Also you might want to check if this option is selected in your IDE:
Settings(Preferences) | Editor | General | Smart Keys, Surround selection on typing quote or brace

Related

In IntelliJ what's the shortcut to call a function with the expression before the cursor as parameter

Let's just say I am typing foo.getBar() and now I would like to call my method stuff() on that result, to produce stuff(foo.getBar()). If I have to do it manually, I have to surround my expression with parentheses and move the cursor before the first parenthesis, then write my method. Is there any shortcut that would do it for me?
It means go from
foo.getBar()|
to
|(foo.getBar());
in one shortcut, with '|' being my cursor.
Just type .arg after your expression and it will wrap the expression into an argument with the cursor before.
You have to enable Postfix Completion (which is enabled by default I think). You can find it in Editor > General > Postfix Completion

Shortcut to get out of parenthesis in IntelliJ (or possibly other IDE as well)

Let's say | is a cursor pointer. What I want to do is to get out from parenthesis once I finish typing paremeters. In step by step explanation:
// 1: type function name
void function|
// 2: openening paren automatically generate closing paren for convenience
void function(|)
// 3: type paremeters...
void function(42|)
// 4: ...and get out of parenthesis!
void function(42)|
Usually I use right arrow to do that, but using arrows are not so convenient, and I wonder if there's any shortcut for this. What I'm curious at is IntelliJ's, but it would be very nice if you can tell me shortcut of any other Jetbrain IDE or Visual Studio, etc.
I think you are looking for Ctrl + Shift + Enter (on Windows). The Action name is Complete Current Statement
On Mac if you want to know the shortcut invoke Command + Shift + A (Find Action) then type Complete Current Statement it will show you the shortcut for the action as well
The shortcut you're looking for, on most keyboards, is shift+0 -- this won't work on the keypad, only the main numbers at the top of the keyboard. Technically this isn't a shortcut, you're actually typing the close parenthesis, but IntelliJ is smart enough not to double up on them so it's as good as a shortcut in this case.
Another setting that I find convenient to use is this option.
Settings - Smart Keys - Jump outside closing bracket/quote with Tab

Pharo: customizing smart characters

The checkbox "Smart Characters" in "Code Completion" section of Settings Browser does (at least) two things:
1) It doubles some characters when typed: ', ", (, [, {
2) It enables that I can select a piece of code, press ( (i.e. Shift+9), and the selected code becomes surrounded by parentheses: (). I also can remove parentheses by pressing ( again. I also can do this with [] by pressing [ and with {} by pressing {, i.e. Shift+[.
I do not like the first of these things so I want to disable it, but I like the second thing and want to keep it. How can I achieve this? Turning off the checkbox will disable both.
P.S. I know that when the checkbox is off, adding/removing parentheses works by Cmd+Shift+9 (which is less convenient than Shift+9) and that Cmd+[ works for [], although I do not know any working shortcut for adding/removing {} when the checkbox is off.
The setting is called "Smart Characters", which should give you a clue as to where to look. Open a Finder, type in smartCharacters, and hit Enter. You should see some partial matches as well as an exact match for NECController and NECPreferences class (and the former just calls the latter). If you investigate the classes involved a bit, you'll see that smartCharacters stores a boolean, and that smartCharactersMapping returns a dictionary mapping some characters to their "counterparts", i.e. $[ to $] and so on. Now look at senders of smartCharactersMapping, and you'll see where it's being called from.
The caller you're most likely interested in would be NECController>>smartCharacterWithEvent. So put a breakpoint in that very ugly method to see what it does. You don't care about the first two cases (the editor having a selection and there not being a smart mapping), since you want to prevent the second matching character from being inserted. So the interesting bit for you is this bit:
self newSmartCharacterInsertionStringForLeft: char right: opposite
The method only has one implementor and that one sender, so it should be safe to comment out the original method and just return the "left" character, i.e.:
newSmartCharacterInsertionStringForLeft: left right: right
^String with: left
In other words, instead of creating a string with the left and right characters, and possibly spaces between them, just return a new string with the single character you typed. Might not be the most elegant way of solving this, but it should work, and should show you how to solve similar problems in the future.
(Ideally, you'll find a better solution, post it here as an alternate answer, and contribute it to the Pharo codebase - Pharo is open source, after all.)

Intellij Idea Keyboard Shortcut To Select A Token

Is there any intellij idea shortcut to select a token in a statement?
For example, consider this:
cell.setCellValue((profileInfo.get("startTimeOfCrawl")!=null)?profileInfo.get("startTimeOfCrawl"):"");
Currently, if my cursor is on the first character of the above statement(i.e. at beginning of c of cell, if I have to select profileInfo, then I will have to use my mouse and double click on profileInfo to select that.
Another workaround I found was to use arrow keys to get cursor to profileInfo
Then use ctrl+shift+right-arrow keys to select till the end(i.e. till o) of profileInfo
This is good when my cursor is placed at beginning of profileInfo(or even end in which case we can use left-arrow key).
But if my cursor is placed somewhere in between of profileInfo then I will have to use ctrl+right (or ctrl+left) to get the cursor to either beginning or end of this token. Then I Will have to use ctrl+shift+right-arrow (or left-arrow as the case may be).
(Switching from keyboard to mouse breaks the continuity, hence looking for keyboard shortcuts.)
Is there a better shortcut to do above in 1 step?
PS: Solution to above will be very useful when making string replacements.
Use Edit | Extend Selection (Ctrl-W in the default keymaps, Alt-Up in the Mac OS 10.5+ keymap). You can press it multiple times to extend the selection to larger enclosing syntax constructs.
I can't think of a 1-step process of doing this but try using the alt to traverse via arrow keys, it will traverse it per "word" instead of per character.
You can also use alt+shift+ arrow keys to select per word.

Intellij IDEA shortcut to "create field"?

There is an useful functionality in IDEA that lets you create a field, and it appears in the little light-bulb when you highlight the non-created variable.
However, I can't find the short-cut, searched for "create field" in the settings but no luck.
Anyone knows?.
Control + Alt + F on a literal will give you the option to extract the value into a field.
Use Alt-Enter to access the lightbulb menu and then use the arrow keys to select the necessary option. There is no possibility to assign shortcuts to individual quickfixes (there are too many of them).
On MacOS, its option-command+F.
Its much faster than Option-Enter, then you need some brain cycle to select create field in the down down menu