{"id":59793,"date":"2020-07-22T15:08:08","date_gmt":"2020-07-22T14:08:08","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=scala&#038;p=59793"},"modified":"2023-09-04T17:30:55","modified_gmt":"2023-09-04T16:30:55","slug":"intellij-scala-plugin-2020-2-indentation-based-brace-handling","status":"publish","type":"scala","link":"https:\/\/blog.jetbrains.com\/ru\/scala\/2020\/07\/22\/intellij-scala-plugin-2020-2-indentation-based-brace-handling","title":{"rendered":"IntelliJ Scala Plugin 2020.2: Indentation-based Brace Handling"},"content":{"rendered":"<p>Because Scala is a <a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_programming_languages_by_type#Curly-bracket_languages\" target=\"_blank\" rel=\"noopener\">curly-bracket language<\/a>, adding and removing <strong>curly braces<\/strong> is an integral part of Scala programmers&#8217; everyday life. Those actions compound, so every improvement in this area matters. To understand the <strong>new advances<\/strong>, let us first take a take a look at it from historical perspective (or just watch <a href=\"#tldr\">this<\/a>).<\/p>\n<h2>Before<\/h2>\n<p>At the beginning, there was no IDE assistance, so you had to tinker with curly braces <strong>manually<\/strong>:<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]--><br \/>\n<video class=\"wp-video-shortcode\" id=\"video-59793-1\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-1.mp4?_=1\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-1.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-1.mp4<\/a><\/video><\/div>\n<p>Not only did this require more keystrokes, but the code was not valid for long. It was not good&#8230;<\/p>\n<p>But now, thanks to <em>Editor | General | Smart Keys | Insert paired brackets<\/em>, IntelliJ IDEA can add a <strong>complementary brace<\/strong> automatically:<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-2\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-2.mp4?_=2\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-2.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-2.mp4<\/a><\/video><\/div>\n<p>However, you have to do this <strong>in advance<\/strong>, as it does not work for already <strong>existing code<\/strong>:<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-3\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3.mp4?_=3\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3.mp4<\/a><\/video><\/div>\n<p>In such cases, you are right back to square one.<\/p>\n<p>As a workaround, you could use the <em>Add braces<\/em> <strong>quick fix<\/strong> (or select the expression and press <code>{<\/code>):<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-4\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3a.mp4?_=4\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3a.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-3a.mp4<\/a><\/video><\/div>\n<p>But you have to know about these features in the first place, and it requires <strong>an explicit action<\/strong> on your part.<\/p>\n<p>Fortunately, in Scala plugin 2019.2 we have <a href=\"https:\/\/blog.jetbrains.com\/scala\/2019\/07\/24\/intellij-scala-plugin-2019-2-functional-code-highlighting-fine-grained-type-diff-case-clause-completion-build-server-protocol-and-more\/#wrap-unwrap-expressions-as-you-type\">introduced<\/a> automatic handling of complementary braces (<em>Editor | General | Smart Keys | Scala | Wrap single expression<\/em>):<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-5\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-4.mp4?_=5\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-4.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-4.mp4<\/a><\/video><\/div>\n<p>A <strong>complementary brace<\/strong> can be added and removed automatically, <strong>regardless of existing code<\/strong> in between. (By the way, we received a lot of overwhelmingly positive feedback for this supposedly small feature.) But you still had to add or remove <strong>a single brace<\/strong>&#8230; that is&#8230; until now.<\/p>\n<h2>After<\/h2>\n<p>Now we have taken it even further \u2013 <strong>both braces<\/strong> can be added and removed automatically and on the fly <strong>based on code indents<\/strong>:<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-6\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-5.mp4?_=6\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-5.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-5.mp4<\/a><\/video><\/div>\n<p>With this, you can make use of <em>significant indentation<\/em> while still keeping the braces, even in Scala 2.x. To take advantage of this feature, <strong>just don&#8217;t think about the braces<\/strong> and let the IDE do the work. And it&#8217;s not just a catchphrase &#8211; to use the new feature, you must not use the legacy ones: don&#8217;t add a pair of <code>{}<\/code>, don&#8217;t invoke the &quot;wrap&quot; quick fix, and don&#8217;t insert an opening <code>{<\/code> manually.<\/p>\n<p><a id=\"tldr\"><\/a>The plugin can correctly handle appending or inserting a new line, keywords, and so on (and if braces are not needed, you can either press <em>Enter<\/em> twice or press <em>Backspace<\/em> to delete the indent):<\/p>\n<div style=\"width: 750px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59793-7\" width=\"750\" height=\"300\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-6.mp4?_=7\" \/><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-6.mp4\">https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/07\/braces-6.mp4<\/a><\/video><\/div>\n<p>At the moment, the plugin doesn&#8217;t remove braces by default, but you can enable it in <em>Editor | General | Smart Keys | Scala<\/em>.<\/p>\n<p>To try this now, select <em>Nightly builds<\/em> as your <em>Settings | Languages | Scala | Updates | Channel<\/em> (you can revert to a more stable build in the same way at any time). We\u2019re announcing this feature early, before the official release, in order to gather more information and polish off the rough edges. <strong>We need your feedback!<\/strong> Please report any bugs to <a href=\"https:\/\/youtrack.jetbrains.com\/issues\/SCL\" target=\"_blank\" rel=\"noopener\">YouTrack<\/a>. If you have any questions, feel free to ask them in our <a href=\"https:\/\/gitter.im\/JetBrains\/intellij-scala\" target=\"_blank\" rel=\"noopener\">Gitter<\/a>.<\/p>\n","protected":false},"author":11,"featured_media":60011,"comment_status":"closed","ping_status":"closed","template":"","categories":[808,89,8058,6945],"tags":[6233,83,6234,225],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/scala\/59793"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/scala"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/types\/scala"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/comments?post=59793"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/scala\/59793\/revisions"}],"predecessor-version":[{"id":273093,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/scala\/59793\/revisions\/273093"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/media\/60011"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/media?parent=59793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/categories?post=59793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/tags?post=59793"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/ru\/wp-json\/wp\/v2\/cross-post-tag?post=59793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}