Chapter 2 - Navigation
Word-skipping ⌥ + arrows
Open Chapter2.java
, and place your cursor in front of the “The” of the popular phrase, and tap ⌥ + → 8 times.
You should now have your cursor at the beginning of the word “camel”.
Begin/End of Line ⌘ + →/←
Still in Chapter2.java
, place your cursor at the start of the popular phrase, and press ⌘ + →.
Press ⌘ + ← once, look at your cursors position, then press ⌘ + ← again and see
what
happens. Press ⌘ + ← one more time.
If you would like to see a stroboscopic effect, put your cursor at the beginning of the word “return”, and hold down shift + ⌘ + ←.
Enjoy annoying anyone that might be watching over your shoulder.
CamelHumps (+ how to toggle)
NOTE:
These shortcuts might conflict with your OS shortcuts, so it’s wise to disable these before you continue with the next exercises.
In Chapter2.java
, put your cursor at the beginning of the method theQuickBrownFoxJumpedOverTheLazyCamel
.
Try to use Skip Word with ⌥ + → on that method.
Depending on your CamelHumps setting, your cursor either ended up on the “Q” or it skipped the entire method name and ended up on the ”(“.
Return to the beginning of the method name and press ⌘ + a, then type CamelHump
. There used to be a setting
named Smart Keys: Use "CamelHumps" words
with a toggle indicator, but this disappeared since some new release around 2019.
Instead if you want to do something using the alternative CamelHumps mode, there is an action in the action menu for that.
These are called Move Caret to Next Word with Selection in Different "CamelHumps" Mode
(or Previous Word, or without Selection).
They all appear when you type CamelHumps and IntelliJ remembers your last action command, so it’s not all bad.
TIP:
Other tools, like SublimeText, have different key combinations to skip the entire word (⌃ + →), or skip based on CamelCasing (⌥ + →).
Jumping methods ⌃ + Shift + ↓/↑
In Chapter2.java
, place your cursor at the theQuickBrownFoxJumpedOverTheLazyCamel
method.
Press ⌃ + Shift + ↓ a few times and see what happens.
Now use ⌃ + Shift + ↑ to go back the way you came.
Jump to “error” F2
Move to the top of the file with Fn + ⌘ + ← and from there press
F2. This should navigate
your cursor to the class named Chapter2
because IntelliJ marks it as being unused.
TIP:
If you want to see which error your IDE has detected, check out the “Problems” pane. You can open this message view by using the combination ⌘ + 6
If you keep pressing F2 it should keep cycling your cursor over the unused warnings. In between the methods jump()
and camel()
,
paste the following:
privet String kakdilla(){
"horocho";
}
Move to the top of the file again, and press F2 once again.
Notice how the cursor now first jumps to the actual compilation error (privet
cannot be resolved).
NOTE:
Cycling only happens over all actual errors, and the unused warnings are not cycled over anymore.
Jump into ⌘ ( + ⌥) + b
“Jump into”, or “drill down” as I like to call it, allows you to follow the path the code will execute at runtime. It is a big timesaver when attempting to follow the logic of any given program.
Right now, there’s a typo in both the EpicJumper.java
and MehJumper.java
classes. Let’s fix that.
Go to Chapter2.jumpUsingStrategy()
and place your cursor on the jump()
method call. Press ⌘ b.
This should take you straight to the interfaces jump()
method.
Now, let’s go back to where we came from. Press ⌘ ⌥ b. IntelliJ knows you want to “drill down” into
the actual method implementation but doesn’t know which one, so it will suggest some options. Select the MehJumper
method by pressing
↓ and then enter and see where it leads you.
You can now correct the typo in the method, and move on to the next exercise.
History and its importance ⌘ + ⌥ + ←/→
In the previous exercise we drilled down into a method call and changed some things. But sometimes we want to go back in time (usually after messing something up). Let’s repeat the previous exercise! If you are following this tutorial in one go, and are currently at the end-position of the previous topic, you can continue onwards from there.
Press ⌘ + ⌥ + ← to go return to your starting position.
You should now be back at the Chapter2.java
class.
Now repeat the previous exercise, but pick the EpicJumper
and also fix the typo. Then go back again using ⌘ +
⌥ + ←.
Also try backtracking your backtrack by pressing ⌘ + ⌥ + →.
NOTE:
every time you use Navigation shortcuts that bring you to new classes, IntelliJ will remember this in a Navigation History of sorts.
Jump to last edit position ⌘ + shift + backspace
From the end of previous exercise, make sure you’re back in the Chapter2.java
class and press ⌘ + shift +
backspace to return to where you were last editing. Try pressing the hotkey again and see what happens.
Show in Project ⌥ + F1
Open MehJumper.java
by pressing ⌘ + o, then use ⌥ + F1, enter to open up
the Project
navigational sidebar with the MehJumper.java
class selected.
You can now use ⌥ + 1 (do not press F1, we mean the actual digit) to minimize the sidebar and move your window focus back to your editor.
More navigational goodness: code hierarchy transversal
Inspect the mud
package. It’s got your typical layered application where we pass around a Ball
through all of its layers.
In order to navigate more complex code hierarchies, play around with some of these hotkeys:
Pressing ⌥ + F7 will show you how the selected element is used inside your codebase.
This shortcut works on virtually anything, be it a class, a method or a field in a separate Tool Window.
As an example: open Ball.java
using ⌘ + o, and press your arrow keys to navigate to thegetId()
method inside
this class.
Now press ⌥ + F7 and look at the bottom of your screen.
⌃ + ⌥ + h will show you the call hierarchy leading up to the element you are currently inspecting. Repeat the previous step, but instead of inspecting an element’s usage, press ⌃ + ⌥ + h. Navigate the element tree using the arrow keys, and select any element you wish to take a closer look at with ctrl + enter (or F4 if you want to navigate to the code without further ado).
TIP:
move this navigational Tool Window to the bottom bar (next to3: Find
), because you’ll usually want to optimize your screen’s horizontal space rather than its vertical space.
⌥ + b
Navigate to the return id;
line in the Ball.java
class and put your cursor on id
. Now press ⌥ + b
This should navigate your code editor to the instantiation of the field itself.
Press ⌥ + b again. This time it should show a popup asking if you want to show accessors of the field. Let’s
go with Yes
.
Pop-up windows (but not the annoying kind)
Sometimes when you are working on code, you want to quickly reference how a certain class, field or method is defined without opening a new workspace window. In order to do so, you can make use of the ⌥ + space keyboard combination to do just so.
Other useful overlay pop-ups include: the quick documentation and quick parameter definition shortcuts. Let’s find out what they do! Move to any line of code, and press ⌃ + j. The overlayed information pop-up will show you relevant documentation of the selected code element.
Navigate to the return id;
line in the Ball.java
class and put your cursor on id
again. Pressing ⌘ + p will
show you the relevant documentation for this parameter.
As we have not written any documentation, this overlay window will be blank.
Jump to line ⌘ + l (example with paste from stacktrace)
Here is a part of a very long stacktrace.
java.lang.NullPointerException
at be.swsb.productivity.chapter2.mud.service.BallServiceImpl.findBall(BallServiceImpl.java:18)
at be.swsb.productivity.chapter2.mud.ui.BallScreen.render(BallScreen.java:15)
at be.swsb.productivity.chapter2.mud.ui.BallScreenTest.screenCallsStuff(BallScreenTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Let’s see where the NullPointer is occurring. Open BallServiceImpl
, using ⌘ + l.
Now use ⌘ + l and enter the desired line number (this is line nr. 18
), as the stacktrace above states the
error occurs on this line: at be.swsb.productivity.chapter2.mud.service.BallServiceImpl.findBall(BallServiceImpl.java:18)
Navigate to method ⌘ + F12
Paste from stacktrace
Another way of navigating to the location of the code causing our NullPointer exception, albeit less precise, is to first copy the method
name from the stacktrace, in this case findBall
.
Next, open BallServiceImpl.java
, using ⌘ + o . Now press ctrl + F12. This lists all methods
of a class.
To navigate to the suspicious method: paste the name we copied from the stacktrace into the method list popup window.
You can then press Enter to navigate to that method.
TIP:
You can also use the ⌘ + F12 shortcut to get a quick overview of the contents of a file. This works well even in non-Java files. As an example, try out the combination inside the source code of this text (chapter2.md
)
Quick check equals impl
Let’s imagine we want to write a comparator for some object, or want to use it in a Set. You’ll want to take a look at that
objects equals()
implementation. In case the object is filled to the brim with other methods, it might be easier to tap
⌘ + F12 to check for an equals
method. If you can’t find it on your first try, tap ⌘ + F12
again to browse the methods of the superclass as well.
Let’s open RealBall.java
, and try to find out if this class has its own equals
and hashcode
implementations, or wether makes use of
its superclass implementations.