Tools

“Surround with…” comes to Kotlin

Great news for fans of Intellij IDEA: “Surroud with…” action is now available for Kotlin!

What is the “Surround with…” action?

This action allows you to surround a block of code with if, try or other statements (see the full list bellow). Simply select a block of code, press Ctrl + Alt + T and choose the template (Cmd + Alt + T for Mac):

The IDE will add the neccessary code and put the caret at the position that allows you to finish your refactoring:

If the surrounded block contains variable declarations that are used after the block, they will be moved out of the block:

http://www.youtube.com/watch?v=Bwuj15P8yOQ&feature=youtu.be

Function Literal Template

You can surround a block of code like this:

Use this template if you want to “hide” some variables in the scope of the block, to create a property with function type or a local function and if you want to call any function with function literal as an argument.

http://youtu.be/ufSDvAxo544

When template

You don’t need to write when expression by hand any more: you can simply select a subject expression, choose “surround with when(expr) {}” and the result will be:

Full list of  “Surround with…” templates

When a block of code is selected:

  • if
  • if / else
  • { }                       – function literal
  • try
  • try / catch
  • try / catch / finally

When an expression is selected:

  • !(expr)                – add negation for Boolean
  • (expr)                 – surround with parentheses
  • “${expr}”           – surround with quotes
  • when(expr) {}

Kotlin M5.1

We release “Surround with…” as a part of Kotlin M5.1 that will be out very soon.

image description