JavaRush/Courses/Kotlin SELF/IDEA life hacks for Kotlin

IDEA life hacks for Kotlin

Open

1. Autocomplete (Tab)

IntelliJ IDEA is an IDE created by the same company that invented the Kotlin language (JetBrains). So it’s no surprise that IDEA understands Kotlin better than anyone in the world.

IntelliJ IDEA has a wonderful feature called autocomplete. It analyzes all the files in your project, all connected libraries, and understands what classes, functions, and variables you have. It then uses this information to help you write code.

You simply start typing a word, and it immediately suggests hints. One of the most important keys is the tab key “Tab”.

For example, you want to print text to the console. In Kotlin, the println() function is used for this. You just need to start typing pri and press “Tab”: IDEA will finish the word and add parentheses.

If you put a dot after an object name, IDEA will suggest a list of functions and properties available for that object. This is very convenient so you don’t have to memorize all methods by heart.

For example, you created a variable with text. You don’t have to remember what the function is called to convert text to uppercase. Just type the variable name and put a dot:

fun main() {
    val text = "Hello Kotlin"
    text.   // <-- At this moment, after the dot, IDEA will show the list of commands
}

Important: first type the variable name (text), and only then put the dot. In the drop-down list you’ll see properties (for example, length — length) and functions (for example, uppercase(), replace()).

CamelHumps

Suppose you want to type the name of a long class, for example InputStreamReader. IntelliJ IDEA lets you save time: you can type only the capital letters — ISR (InputStreamReader) and press Tab. IDEA will expand that abbreviation into the full class name. Almost magic.

2. Live Templates

IntelliJ IDEA has thousands of code snippets called Live Templates. Here are the most popular ones in Kotlin:

The main function

To run a program, you need the fun main() function. Typing this manually every time is slow.

Just type 4 letters main and press Tab (or Enter).
IDEA will instantly expand it into:

fun main() {

}

By the way, the old Java template psvm also works in Kotlin files, but get used to main.

Loop (fori)

You can quickly write a loop using fori + Tab. IDEA will create the loop skeleton:

for (i in 0..index) {
}

All you’ll have left is to specify up to what number the counter should run.

soutv (System.out Variable) — Print a variable’s value.
If you have a variable age, type soutv and press Tab. IDEA will find the nearest variable and generate the code:

println("age = $age")

soutm (System.out Method) — Print a method name.
Useful when you need to understand whether the program entered a method or not. Generates:

println("Main.main") // Your class and method name

iter (Iterate) — Iterate over a collection.
If you have a list of elements (for example, users), this template will create a for-each loop:

for (user in users) {

}

todo — A note for later.
Leaves a special comment that is highlighted in IDEA with a bright color and appears in the task list (the TODO tab at the bottom of the screen):

// TODO: implement this method later

fixme — A critical marker.
Similar to TODO, but used for places where code works incorrectly or needs an urgent fix:

// FIXME: an error occurs here, needs fixing!

3. Postfix Completion

It lets you turn an already written expression into different code by simply adding a dot at the end.

You don’t need to go back to the beginning of the line to wrap a variable in parentheses or a condition. You write code left to right without interrupting your train of thought.

Examples:

  • .sout — wraps the expression in println(...)
  • .var — creates a variable from an expression.
  • .not — inverts a boolean value (puts ! before the expression).

What it looks like:

You type Press Tab — you get
"Hello".sout println("Hello")
10 + 5.var val i = 10 + 5
isValid.not !isValid

4. Surrounding code: Ctrl+Alt+T

Sometimes you need to place already written code inside an if condition, a loop, or an error-handling block like try-catch.

Instead of typing braces manually and copying code, use the “Surround With” command:

  1. Select the lines of code.
  2. Press Ctrl+Alt+T (Windows/Linux) or Cmd+Opt+T (macOS).
  3. Choose the needed construct from the list (if, while, try/catch, etc.).

Example menu in IDEA:

5. Safe renaming: Shift+F6

Never rename variables, functions, or classes by simply deleting text! If a variable is used in 10 places, you’ll have to find and fix them all manually, which practically guarantees errors.

Use Rename Refactoring:

  1. Place the cursor on the variable name.
  2. Press Shift + F6.
  3. Enter the new name and press Enter.

IntelliJ IDEA will find all usages of this variable across the entire project and rename them neatly.

6. Search Everywhere: Shift + Shift

If you lost a file, forgot where a class is located, or can’t find the needed setting in the menu — don’t panic.

Just press the Shift key twice in a row.

The Search Everywhere window will open (search everywhere). Start typing the name of a file, class, or even a setting name (for example, "Font size"), and IDEA will instantly find it.

7. Local History

Imagine this situation: you were writing code for half an hour, then decided to delete everything and rewrite it. You deleted it. Then realized it was a mistake. You pressed Ctrl+Z, but accidentally closed the file... and undo no longer works. Panic?

No! IntelliJ IDEA has its own “time machine” built in — Local History.

It saves versions of your code every few minutes, even if you’re not using Git. To restore code from the past:

  1. Right-click anywhere in the code editor (or on the file in the project tree).
  2. Select Local History -> Show History.
  3. In the window that opens, select a file version on the left and click the revert button (Revert).

8. Code style: Ctrl+Alt+L

Very often, as a result of copying or quickly writing code, its formatting gets broken: crooked indentation, extra spaces, braces in the wrong places.

To make your code match the official Kotlin Style Guide, press: Ctrl+Alt+L (Windows/Linux) or Cmd+Opt+L (macOS).

IDEA will automatically arrange spaces and line breaks.

Example:

Before After
fun main() {
println("Hello")
   if(2<3){
println("Oops") }

for(i in 0..10){
println(i)
    }
}
fun main() {
    println("Hello")
    if (2 < 3) {
        println("Oops")
    }

    for (i in 0..10) {
        println(i)
    }
}

Style settings can be found at Settings - Editor - Code Style:

Комментарии
  • популярные
  • новые
  • старые
Для того, чтобы оставить комментарий Вы должны sign in
This page has no comments yet