title | date | author | tags | keywords | categories | reward | reward_title | reward_wechat | reward_alipay | source_url | translator | translator_url |
---|---|---|---|---|---|---|---|---|---|---|---|---|
“Surround with…” comes to Kotlin |
2013-02-26 01:20:00 -0800 |
Natalia Ukhorskaya |
官方动态 |
false |
Have a nice Kotlin! |
Great news for fans of Intellij IDEA: “Surroud with…” action is now available for Kotlin!
****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):
{% raw %}
{% endraw %}The IDE will add the neccessary code and put the caret at the position that allows you to finish your refactoring:
{% raw %}
{% endraw %}If the surrounded block contains variable declarations that are used after the block, they will be moved out of the block:
{% raw %}
{% endraw %}{% raw %}
{% endraw %}{% raw %}
<iframe allowfullscreen="true" class="youtube-player" height="390" src="https://www.youtube.com/embed/Bwuj15P8yOQ?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" style="border:0;" type="text/html" width="640"></iframe>
{% endraw %}You can surround a block of code like this:
{% raw %}
{% endraw %}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.
{% raw %}
<iframe allowfullscreen="true" class="youtube-player" height="390" src="https://www.youtube.com/embed/ufSDvAxo544?version=3&rel=1&fs=1&autohide=2&showsearch=0&showinfo=1&iv_load_policy=1&wmode=transparent" style="border:0;" type="text/html" width="640"></iframe>
{% endraw %}You don’t need to write whenexpression by hand any more: you can simply select a subject expression, choose “surround with when(expr) {}” and the result will be:
{% raw %}
{% endraw %}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) {}
We release “Surround with…” as a part of Kotlin M5.1 that will be out very soon.