IntelliJ IDEA
IntelliJ IDEA – the Leading Java and Kotlin IDE, by JetBrains
Alt+Enter: The Problem-solving Shortcut
Imagine that you have the entire IntelliJ IDEA interface at the tips of your fingers. IntelliJ IDEA has two shortcuts that can get you out of almost any kind of trouble: Shift+Shift to run Search Everywhere and Alt+Enter to invoke Context Actions.
In one of the previous IntelliJ IDEA: how2pro blog posts, we talked about the Search Everywhere feature and how you can use it to search for code elements and even disable and enable IDE actions or assign shortcuts. Today, let’s dive deeper into Context Actions.
Context Actions become available to you in the editor as you type. They differ depending on your current context and the code that you’re working with. In a nutshell, context actions help you fix problems and improve your code. In reality, they incorporate a vast amount of quick-fixes and suggestions that can correct and improve your code, add code elements, upload missing components, create classes and methods, and do many other amazing things.
Whenever you see a yellow or red light bulb icon in the editor, press Alt+Enter, because IntelliJ IDEA has tips to offer.
Fix code problems
The most powerful tool in IntelliJ IDEA that operates by means of Alt+Enter is code inspections. Pressing this shortcut on a highlighted code element in the editor opens a list of possible fixes from which you can select the one that works best. If you’re not sure how a certain fix is going to change your code, you can open the preview by pressing Alt+Space on macOS or Ctrl+Shift+I on Windows and Linux on a suggestion in the list.
By the way, if the widget in the top-right corner tells you that there are problems in your current file, don’t scroll down and look for them. Click the widget to open the list of problems in the Problems tool window. There, you can deal with each problem, from errors to typos, by pressing Alt+Enter without having to jump between the tool window and the editor.
Learn more about how to deal with code problems from this blog post.
Add missing libraries
If you’re using a piece of code from a library that has not yet been added to your project, it will be highlighted in red as an error. To quickly fix that, press Alt+Enter and select the necessary library from the list. The IDE will download and install it for you.
Add missing import statements
Pasting code from elsewhere? If the inserted code block has a class or a method that has not yet been imported, you’ll see an error.
Normally, you’d have to go up to the beginning of the file and add the missing statement manually. IntelliJ IDEA has a faster solution: it shows you the most suitable import statement in a popup right in the editor. If the suggested fix works for you, press Alt+Enter, and the IDE will add the import statement for you. If there are several options, pressing Alt+Enter opens a list of options from which you can pick the most suitable one.
Inject another language
Sometimes, you need to work on small elements of code in other languages embedded in your project. For example, these can be fragments of HTML code in Java, regular expressions, or SQL queries.
Type the code that you want to embed, press Alt+Enter and select Inject language or reference. After, pick the necessary language from the list and get code completion, inspections, highlighting, and even a dedicated editor for the injected code.
If you want to learn more about language injections, we have an entire blog post dedicated to this feature.
Create test classes
Context Actions make the process of creating tests easy and quick. All you need to do is to place the caret at the production class for which you want to create a new test, press Alt+Enter, and select Create Test.
IntelliJ IDEA will prompt you to select or download the necessary library if it’s missing, specify the destination package, and even select the methods for which the IDE will generate new test methods.
Content Actions are designed to help you code comfortably. They’re always ready to optimize your code and provide fixes to all sorts of problems soon as you press Alt+Enter.
What’s your favorite Content Action in IntelliJ IDEA? Let us know in the comments.
Develop with pleasure!