Extract "with" block in Kotlin Code with IntelliJ - kotlin

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.

Related

intellij wrapping brackets around if-else

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

How to get output of Kotlin IntelliJ scratch file?

A script as simple as
println("a")
doesn't produce any output in the Scratch output window. I expect an a to appear in the output window.
I'm using IntelliJ 2019.1.2 CE.
EDIT: Information regarding the expected functionality
According to a question I had asked: IntelliJ Ultimate Kotlin Script REPL skips first printed lines - Scratch Output cut off
It seems to be the case that the REPL wants to output per line, and will only overflow into the bottom area after a certain line length is reached.
In the question I state that I generally add some initial padding so that it always flows into the lower area with something similar to:
repeat(10) { println("BLANK ") }
END EDIT
You have to make sure that Interactive Mode is turned off at the top of the scratch window
This will cause prints to be put into the Scratch Output window.
Be warned, atleast in the version I have of IntelliJ ultimate 2019.1.3, the first 5-9 lines generally dont print so I do something like:
repeat(9) { println("blank") }
If you've defined everything inside of a function, the script won't execute the function automatically - you'll need to explicitly call the function.
You can create your own print, like this:
fun <T> myPrint(x: T) : T = x
then calling e.g.:
myPrint(5)
should show 5 in the result window.

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

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: 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.