Chapter 6 - IntelliJ’s Suggestions
We are but human. That means we tend to mess up from time to time. Knowing how to quickly fix mistakes you make is a tremendous boon to your programming productivity. Luckily, our IDE can help us fixing some of the smaller mistakes we often make.
IntelliJ’s Auto-correct ⌃ + ⇑ + ↵︎
Let’s open Chapter6.java (⌘ + o) , a class with many mistakes (and (a hell of (a lot of)) brackets).
Let’s see what IntelliJ can do for us to fix the mistakes by pressing F2 to navigate quickly to the first error, and press ⌃ + ⇑ + ↵︎.
Press F2 again and let’s see if IntelliJ can auto-fill the missing semicolon.
Press F2 again and see if it can auto-correct an incomplete method.
Undo (⌘ + z) your last auto-correct and type out public void poo( above the comment line.
It’s basically the same thing we tried to auto-correct just a few seconds ago. The only difference is that there are now two “incomplete” error statements.
Try pressing ⌃ + ⇑ + ↵︎ anyways and see what happens.
Autocomplete ⌃ + Space vs. ⌃ + Shift + Space
Autocomplete basics
Open Transformers.java
, a class with a lot of action in it. To quickly navigate to the missing constructor argument, use F2.
To have IntelliJ suggest an autocomplete option, press ⌃ + Space. Note that even though the constructor expects an
AutobotEnum, IntelliJ may suggest other options like new
, null
, equals()
, etc.
To have IntelliJ provide more helpful context-specific code completion actions, try using the alternative hotkey combination ⌃ + Shift + Space.
Before selecting one of the AutobotEnum suggestions, try pressing ⌃ + Shift + Space again. This hotkey will
show you that there is a static method Autobot.optimusEnum()
that also returns an AutobotEnum
. IntelliJ knows about this method as well.
Try it out again to construct Optimus’ nemesis, Megatron.
Autocomplete selection confirmation
After using ⌃ + Shift + Space or ⌃ + Space to bring up suggestions, there are multiple ways to confirm your selection:
- Pressing Enter completes the code.
- Pressing Tab replaces code that was there before.
- Pressing . completes the code and adds a . so you can continue typing.
- Pressing Space does the same but adds a space instead of a . (this is different from regular suggestions after typing a .!)
As it stands, Optimus has a method called .catchphrase that takes a prefix and returns <prefix> Rollout!. We can rename that method to rollout.
To navigate to the .catchphrase method, use ⌘ + Shift + B. Then delete catchphrase and replace it with rollout (don’t use Shift + F6).
Next, go back to the Transformers class with ⌄ + [ and use autocomplete to replace the previous non-compiling catchPhrase method with the new rollout method. Try using Enter as your confirmation selection first, and then undo with ⌘ + Z and retry the autocompletion using Tab as your selection confirmation.
Repeat the process with the other confirmation methods listed above.
QuickFix (:bulb:) ⌥ + enter
IntelliJ’s QuickFix is a powerful tool that can save you a lot of time and effort. In fact, it is so powerful that it might as well be magic. Whether you’re creating a new constructor, generating getters and setters for newly created fields, or extracting a method or variable, QuickFix has got you covered. And if your code isn’t compiling, QuickFix can help you fix the problem in no time.
To use QuickFix, simply press ⌥ + enter. This shortcut will bring up a menu of possible actions you can take to fix the issue at hand. Use cases include, but are not limited to:
- Creating a new constructor? Use ⌥ + enter.
- Generating getters and setters for newly created fields? Why not try ⌥ + enter?
- Extracting a method out of selected code? Make a selection, and press ⌥ + enter.
- Extracting a variable? Press ⌥ + enter.
- Code not compiling? Press ⌥ + enter.
- Need to defeat agent Smith and save the matrix? Press ⌥ + enter.
QuickFix is so useful that it’s probably the only IntelliJ shortcut you really need to remember, if you want to be lazy about it.
Creating stuff ⌘ + N
The “Generate” action in IntelliJ is a powerful feature that allows you to quickly create code constructs such as constructors, methods, and
getters/setters. It saves you a lot of time and effort by automatically generating boilerplate code for you, based on the parameters you provide.
You can access the Generate action by pressing ⌘ + N (Generate...
) in the editor window. This action is particularly
useful when you need to create a large amount of repetitive code, or when you need to add functionality to your code quickly.
Try out these actions using the ⌘ + N shortcut:
- Create a new package from the
1: Project
Tool Window to create a new class in the next step. - Create a new class from the
1: Project
Tool Window. - Create a new constructor in that class from the editor.
Deleting stuff Shift + Delete
Programming is not only about creating new concepts and abstractions but also about cleaning up and getting rid of old or unnecessary code. Fortunately, IntelliJ makes deleting code just as easy as creating it.
To delete code, you can simply select the lines you want to remove and press Shift + Delete. You can use it to delete any code fragment in your project.
Your turn! Use this shortcut to:
- Delete the constructor you just created
- Delete the class you just created (the package will probably be deleted automatically because it was the only class in there)
TIP:
For more advanced scenarios, IntelliJ provides refactoring actions to help you delete code more safely and efficiently. For example, you can use the “inline and remove” refactoring action (⌘ + ⌥ + N) to remove a method and all its invocations at once without breaking your syntactical correctness.First you empty the body of the method, next you inline it (⌘ + ⌥ + n).
This is sometimes called the “nuke it” option, and it can be very handy when you want to get rid of a method that is used in multiple places. However, be sure to use this option with caution, as it can have unintended consequences if you’re not careful.
View JavaDoc F1
Open Transformers.java
, and move your cursor to Autobot
.
Press F1 to view the JavaDoc for the Autobot
class. Press Esc to exit the window.
Press F1 twice to open a larger and separate window. Note that you cannot exit this window by pressing Esc, but you can close it by pressing F1 again and then pressing Esc.
You can also use F1 from inside the autocompletion suggestion box. Try calling .toString()
after your new Autobot()
statement using autocompletion (either by typing . or by pressing F1).
While still in the autocompletion suggestion box, navigate to the .toString()
option and press F1 to read the associated JavaDoc.
View parameters ⌘ + P
If you are ever in doubt on which parameters to provide to a method, press ⌘ + P while your cursor is inside the brackets of a method invocation statement. IntelliJ will tell you what parameterization is needed, and even offer a list of alternative invocation options.
In the Transformers.java
class, let’s try to create the Decepticon
, known as “StarScream”.
First uncomment the // Decepticon.StarScream()
line.
Then put your cursor in between the brackets and type ⌘ + P.
Notice how IntelliJ signals which parameter you are required to fill in by emboldening the parameter name.