“Surround with…” comes to Kotlin

Posted by: Andrey Breslav on February 26, 2013

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:

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.

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.

Andrey Breslav

About Andrey Breslav

Andrey, the lead language designer for Kotlin, began his career at Borland, where he worked on language implementations for MDA support. After spending a few years as a college teacher, he joined JetBrains in 2010 to develop the Kotlin programming language.