Chapter 2 - Navigation
Word-skipping ctrl+arrows
Open Chapter2.java
, and place your cursor in front of the word “The” of the popular phrase, and tap ctrl + → 8 times.
You should now have your cursor at the beginning of the word “camel”.
Begin/End of Line home, end
Still in Chapter2.java
, place your cursor at the start of the popular phrase, and press end.
Press home once, look at your cursors position, then press home again and see what happens. Press home 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 + home.
Enjoy annoying anyone that might be watching over your shoulder.
Begin/End of File ctrl+home, ctrl+end
Try out ctrl + home and ctrl + end in Chapter2.java
.
Alternatively, you can accomplish the same using PgUp and PgDn.
CamelHumps (+ how to toggle)
In Chapter2.java
, put your cursor at the beginning of the method theQuickBrownFoxJumpedOverTheLazyCamel
.
Try to use Skip Word with ctrl + → 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 ctrl + shift + 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 (ctrl + →), or skip based on CamelCasing (alt + →).
Jumping methods alt+up/down
In Chapter2.java
, place your cursor at the theQuickBrownFoxJumpedOverTheLazyCamel
method.
Press alt + ↓ a few times and see what happens.
Now use alt + ↑ to go back the way you came.
Jump to “error” F2
Move to the top of the file with ctrl + Home 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 Alt + 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 ctrl (+alt) + 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 ctrl b.
This should take you straight to the interfaces jump()
method.
Now, let’s go back to where we came from. Press ctrl alt b. IntelliJ knows you want to “drill down” into the
actual method implementation but doesn’t know which one, so download shareit 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 ctrl+alt+left/right
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 ctrl + alt + ← 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 ctrl +
alt + ←.
Also try backtracking your backtrack by pressing ctrl + alt + →.
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 ctrl+shift+backspace
From the end of previous exercise, make sure you’re back in the Chapter2.java
class and press ctrl + shift + backspace
to return to where you were last editing.
Try pressing the hotkey again and see what happens.
Show in Project alt+F1
Open MehJumper.java
by pressing ctrl + n, then use alt + F1 to open up the Project
navigational
sidebar with the MehJumper.java
class selected.
You can now use alt + 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 alt + 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 ctrl + n, and press your arrow keys to navigate to thegetId()
method inside this
class.
Now press alt + F7 and look at the bottom of your screen.
ctrl + alt + 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 ctrl + alt + 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 to
3: Find
), because you’ll usually want to optimize your screen’s horizontal space rather than its vertical space.
ctrl+b
Navigate to the return id;
line in the Ball.java
class and put your cursor on id
. Now press ctrl + b
This should navigate your code editor to the instantiation of the field itself.
Press ctrl + 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) ctrl + shift + i
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 ctrl + shift + i 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 ctrl + q. 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 ctrl + 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 ctrl+g (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 ctrl + g.
Now use ctrl + g 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 ctrl+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 ctrl + n. 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 ctrl + 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 ctrl + F12 to check for an equals
method. If you can’t find it on your first try, tap ctrl + 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.