“Surround with…” comes to Kotlin

Posted by: Andrey Breslav on 02/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.


About Andrey Breslav

Andrey Breslav

Andrey is the lead language designer working on Project Kotlin at JetBrains (http://kotlin.jetbrains.org/). He also works on making the Java language better, serving as a Java Community Process expert in a group for JSR-335 ("Project Lambda"). In what spare time is left he tries to make sure that his traveling is not all about work and teaches programming to high-school children. Used to teach OOP/Software Design at a university, but currently switched to speaking at software conferences. Audiences of Devoxx, OSCON, StrangeLoop, Jfokus and other events gave warm reception to his talks on Kotlin, programming languages and foundations of software engineering.

More About Andrey »

NFJS, the Magazine

May Issue Now Available
  • On the road to learning

    by Raju Gandhi
  • Refactoring to Modularity

    by Kirk Knoernschild
  • RESTful Groovy

    by Kenneth Kousen
  • Getting Started with D3.js

    by Brian Sletten
Learn More »