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

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.

This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">