Intellij IDEA generate for-each/for keyboard shortcut - intellij-idea

Is there a keyboard shortcut generating a foreach and also for loop?

you can use 'live templates' to generate several types of code snippets, loop iteration is done by following -
iter Iterate (for each..in)
itin Iterate (for..in)
itli Iterate over a List
itar Iterate elements of array
ritar Iterate elements of array in reverse order
There are probably many more, just lookup 'Live Templates' in help documentation.

Pressing Ctrl+J opens the list of live templates within your code context. Then type 'itin'.

Aditionally to other answers, you don't even have to use Ctrl + J (but nice for checking what are your possibilities..) Just start type and press Enter:
Foreach:
type "iter" then press Enter.
For loop:
type "fori" then press Enter.
Idea will recognize the context and suggest the most likely var to iter through, if you like the given options, just press Enters!

Of course, have a look in the help at the default keymap references. Type one of the following and hit "tab":
iter Iteration according to Java SDK 1.5 style
inst Check object type with instanceof and downcast it
itco Iterate elements of java.util.Collection
itit Iterate elements of java.util.Iterator
itli Iterate elements of java.util.List

Create for loop from an existing list statement:
Highlight the list
Cmd+Alt+J or Ctrl+Alt+J
Choose Iterate Iterable from the dropdown (i)
https://www.jetbrains.com/phpstorm/help/creating-code-constructs-using-surround-templates.html

You can also use Surround with!
Mac : Command+Option+T
Windows : Ctrl+Alt+T
https://www.jetbrains.com/phpstorm/webhelp/surrounding-blocks-of-code-with-language-constructs.html
There is also plenty of live templates in the internet ! you can just google it.

You can use Postfix Completion too. Press Ctrl + Alt + S to open the Settings.
For example:
int[] list = {1, 2, 3};
list.for -> for(int i : list) {}

You can create your own live template, for example, I created this for iterating over an array:

Related

Extract "with" block in Kotlin Code with IntelliJ

Assuming data is a normal Kotlin data class, I want to surround a block with a with construct, so that
myFun(data.start, data.end, data.name)
becomes
with(data) { myFun(start, end, name) }
Could that be done with an IntelliJ refactoring (ideally with a keyboard shortcut). I couldn't find any suitable option in IntelliJ 2022.3
I don't know of such a refactoring, or any way to do this in one step.
However, I would usually just extract a function from it:
extract a function from this expression with Ctrl + Alt + M
(sometimes) if data was captured in the function instead of automatically passed as param, I would make it a param by using Ctrl + Alt + P on it. This might happen if data is a property that's available wherever your new function ends up.
turn the param into a receiver by using Alt + Enter on the data param declaration and selecting the quick action Convert parameter to receiver
Now you should have:
data.myFun2() // call site
fun TypeOfData.myFun2() = myFun(start, end, name)
Then you can leave things this way (I personally like it like that), but if you want your end result to look like in your question, you can do a couple more steps:
manually change data.myFun2() to with(data) { myFun2() } (simpler because you have only one occurrence to change now)
inline the call to myFun2() with Ctrl + Alt + N
But the whole sequence might feel overkill if you want to go to this result.

VIM equivalent of IntelliJ's expand/shrink selection?

How would one achieve the same result. I believe the keybinding for macOS Intellij is op+up/down and on windows it is alt+w/d.
Essentially the function highlights the current word, then, with successive presses, expands out to the full string/line/area in-between parenthesis/further out to the next set of parenthesis. Very useful for developing in LISP.
The closest I've gotten is this: https://vi.stackexchange.com/a/19028
Try this plug in: https://github.com/terryma/vim-expand-region
It expands selections based on Vim’s text objects.
Well this may seem comfortable but does not correspondent with the internal logic of vim itself.
See, in vim everything you enter is like a sentence. va{ for example: there is a verb v -> visually select and an object (or movement) { -> paragraph. In this case there is also a modifier a around. You can exchange stuff in this sentence and it will still work vaw, dil, cB and so on. The power of vim is greatly based on that concept.
Of course you can write a function that does vaw first, then S-v and lastly va{ but that will only work with visual selection. It will not work with c or d or anything. So I will recommend to get used to use different keys for different actions.
The visual selection is mostly not needed anyway. Change a paragraph? directly use ca} and so on.
I have found that VI/VA + WOBO (as many times as you need to expand) works similarly. Not as fast but its the same concept and you can even expand/shrink asymmetrically based on your WO's and BO's (Or OW's and OB's depending on how you look at it)

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

IntelliJ IDEA: Need help to set shortcut line

I am new to coding ... and i am using IntelliJ idea ... my question is ...
i want to make some shortcut for lines... like ...
System.out.println(); or
Scanner input = new Scanner (System.in); ..
and if i type like sop it will print ..system.out.println(); ... as those type of lines i have to use almost every code ... so it will be good if i can make shortcuts for those. is it possible to make this kinda shortcut in Intellij???
It's called Live Templates and there are a couple of them predefined. You can easily define your own.
The first one for System.out.println() is used by printing sout and then Tab.
A couple of the predefined ones can be found in Help -> Default Keymap Reference:
As you can see pressing Ctrl + J will bring up a list of available Live Templates.