{"id":27993,"date":"2020-06-18T08:46:23","date_gmt":"2020-06-18T08:46:23","guid":{"rendered":"https:\/\/blog.jetbrains.com\/idea\/?p=22144"},"modified":"2020-07-16T14:10:06","modified_gmt":"2020-07-16T14:10:06","slug":"language-injections-in-intellij-idea","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/pt-br\/idea\/2020\/06\/language-injections-in-intellij-idea","title":{"rendered":"Mix and Match Languages with Language Injections in IntelliJ IDEA"},"content":{"rendered":"<p>In almost any application, there are situations when you need to work on small elements of code in other languages embedded in your project code. For example, these can be fragments of HTML code in Java, or regular expressions sprinkled around your application. Working with such code elements is super easy in IntelliJ IDEA because it features language injections. They bring code highlighting, completion, error detection, and other coding assistance features to embedded code fragments. Let\u2019s take a closer look at how language injections work.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/02\/idea-blog-banner.png\" rel=\"attachment wp-att-21117\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-21117\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/02\/idea-blog-banner.png\" alt=\"how2pro banner\" width=\"1412\" height=\"726\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p>Let\u2019s try to embed a piece of HTML code into Java. All you need to do is to press\u00a0<strong>Alt + Enter<\/strong>, select <strong>Inject language or reference<\/strong>, and pick <strong>HTML<\/strong> from the list.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22145\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-inject-html-animation.png\" alt=\"inject-html-animation\" width=\"1368\" height=\"598\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-inject-html-animation.gif\" \/><\/p>\n<p>By default, the IDE injects a language temporarily. It means that if you delete the embedded element, the injection will be lost as well. If you want to keep the injection permanently, press <strong>Alt + Enter<\/strong> again when you see the popup prompting you to replace the injection with an annotation. The IDE inserts the <strong>@Language<\/strong> annotation with the corresponding language ID. For method calls, the IDE creates a language injection pattern that looks like this:\u00a0<strong>\/\/ language=HTML<\/strong>.<\/p>\n<p>Now even if you remove the original injection, the IDE will already know which language is intended there and will be ready to offer coding assistance.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22150\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-annotation-injected.png\" alt=\"annotation-injected\" width=\"1368\" height=\"714\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-annotation-injected.gif\" \/><\/p>\n<h2>Dedicated Editor for Injected Code<\/h2>\n<p>In case you want to focus only on the language of the embedded code fragment, IntelliJ IDEA features a dedicated editor. It allows you to work with the code as if it were in a separate document. This can be really handy if the chunks of embedded code are large, or they are hard to make out from the rest of the code.<br \/>\nOf course, the editor for language injections provides the usual coding assistance features: code completion, intentions, inspections, and so on. To open the editor, press <strong>Alt + Enter<\/strong> and select <strong>Edit Fragment<\/strong>.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22152\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injection-editor.png\" alt=\"injection-editor\" width=\"1520\" height=\"766\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injection-editor.gif\" \/><\/p>\n<h2>Inject Regular Expressions<\/h2>\n<p>Regular expressions is a powerful tool that has a broad range of applications, it can help with everything from search to input validation and refactorings. However, sometimes we all struggle with composing them correctly. If you use regular expressions in your code, IntelliJ IDEA has a nice little bonus for you. First, make sure to embed your regular expressions using language injections.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injecting-regexp.png\" rel=\"attachment wp-att-22153\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22153\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injecting-regexp.png\" alt=\"injecting-regexp\" width=\"1424\" height=\"633\" \/><\/a><\/p>\n<p>When regular expressions are injected, you can check whether they are compiled correctly. Press <strong>Alt + Enter<\/strong> and select <strong>Check RegExp<\/strong>. Now you can type a sequence of characters to make sure your expression matches the intended combination. Voil\u00e0!<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22155\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-check-regexp.png\" alt=\"check-regexp\" width=\"1432\" height=\"680\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-check-regexp.gif\" \/><\/p>\n<h2>Automatically Inject Languages<\/h2>\n<p>In IntelliJ IDEA, there is a set of predefined rules according to which the IDE automatically injects languages in certain places in your code. You can find these rules in <strong>Settings\/Preferences | Editor | Language Injections<\/strong>.<\/p>\n<p>You can modify the existing injection rules, duplicate them, and disable any that you don\u2019t need.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injection-rules.png\" rel=\"attachment wp-att-22156\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-22156\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2020\/06\/idea-injection-rules.png\" alt=\"injection-rules\" width=\"1884\" height=\"1446\" \/><\/a><\/p>\n<p>If none of the existing rules work for you, you can create your own and then share them with other members of your team.<\/p>\n<p>To share a custom rule via a VCS, move it to the <strong>Project<\/strong> scope using the corresponding option on the toolbar in <strong>Settings\/Preferences | Editor | Language Injections<\/strong>. By doing so, you will create a new file in the <strong>.idea<\/strong> directory named <strong>IntelliLang.xml<\/strong> with your custom rules that you can place under version control.<br \/>\nIf you don\u2019t use a VCS, you can share your rules by exporting them to XML files and then importing them to another project.<\/p>\n<h2>The Bottom Line is\u2026<\/h2>\n<p>Language injections are great for mixing in multiple languages in one project. They provide code completion, analysis, highlighting, and even a dedicated editor for embedded chunks of code. They can automatically inject the necessary languages in specific places in your code according to predefined rules or your own custom ones.<\/p>\n<p>If you want to get more information on injections, visit the IntelliJ IDEA <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/using-language-injections.html\" target=\"_blank\" rel=\"noopener\">documentation<\/a>. This feature works out of the box and is powered by the IntelliLang plugin that is bundled and enabled by default.<br \/>\nHave you ever used language injections in IntelliJ IDEA? What do you think of them? Let us know in the comments below.<\/p>\n<p>In the meantime, IntelliJ IDEA 2020.2 is well under way. <a href=\"https:\/\/www.jetbrains.com\/idea\/download\/\" target=\"_blank\" rel=\"noopener\">Join the early access program<\/a> and be the first to try new features and improvements.<\/p>\n<p>Happy developing!<\/p>\n","protected":false},"author":1152,"featured_media":27613,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea\/27993"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/1152"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=27993"}],"version-history":[{"count":1,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea\/27993\/revisions"}],"predecessor-version":[{"id":168220,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea\/27993\/revisions\/168220"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media\/27613"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=27993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=27993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=27993"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=27993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}