Kata for practicing keyboard shortcuts for basic code completion features
Key sequences to practice
- ⌃␣ (Ctrl + Space) - basic code completion
- ⌘⇧⏎ (Command + Shift + Return) - complete statement
- ⌥⌘⇧] (Option + Command + Shift + ]) - select from caret to end of code block
- ⌫ (Delete) - delete selected text
Keystrokes without explanations
- Open the Java source file, CodeCompletionPractice.java.
- Begin editing at the top of method1().
- Repeat the following keyboard inputs until the sequence becomes fluid.
double randomNumber = M
⏎
.
ran
⏎
⌘⇧⏎
⏎
double result = ra
⏎
⌘⇧⏎
⏎
S
⏎
.
⏎
.
⏎
System.out.println("Result: " + re
⏎
⌘⇧⏎
And then to revert:
- Place the caret at the beginning of the first line of code in method1().
- Press
⌥⌘⇧]
⌫
Walkthrough of steps with explanations
- Open the Java source file, CodeCompletionPractice.java.
- Begin editing at the top of method1().
- Type the following code:
double randomNumber = M
- The editor presents a list of possible completions. The first one is "Math (java.lang)". Press ↩ (Return or Enter) to select that item. The code now reads:
double randomNumber = Math
and the caret is positioned immediatly after the token, "Math".
- Type . (Period or Dot). The editor presents a list of possible completions.
- Type "ran," so that the source line looks like this:
double randomNumber = Math.ran
- The editor reduces the number of possible completions as you type. Only one option remains after you've typed "ran". Press ↩ (Return or Enter) to select it. The source line now looks like this:
double randomNumber = Math.random()
- Press ⌘⇧↩ (Command + Shift + Return) to complete the statement. The editor appends a semicolon, leaving the source line like this:
double randomNumber = Math.random();
Note: You might wonder why you should press three keys instead of just typing in the semicolon yourself. The statement completion feature can fill in much more than just a semicolon in different situations. It's a good habit to use it routinely.
- On the next line in the source file, type the following:
double result = ra
- The editor presents a list of possible completions and reduces the list as you type. At this point, only one suggestion remains. Press ↩ return to select it. The code in method1() now looks like this:
public void method1() {
double randomNumber = Math.random();
double result = randomNumber
}
- Press ⌘⇧↩ (Command + Shift + Return) to complete the statement. The editor appends a semicolon, leaving the source line like this:
double result = randomNumber;
- On the next line, type
S
- The editor presents a list of possible completions, of which the first one is "System (java.lang)". Press ↩ (Return or Enter) to select it. The source line now looks like this:
System
and the caret is positioned immediately after the token, "System".
- Enter . (Period or Dot) and the editor presents a list of possible completions, of which the first one is "out". Press ↩ (Return or Enter) to select it. The source line now looks like this:
System.out
and the caret is positioned immediately after the token, "out".
- Type . (Period or Dot) and the editor presents a list of possible completions, of which the first one is "println (int x)". Don't worry about the argument type. Press ↩ (Return or Enter) to select the item.
- Start to fill in the argument to the println() method:
System.out.println("Result: " + re
- The editor presents a list of possible completions and reduces the list as you type. At this point, only one option remains: "result". Press ↩ (Return or Enter) to select it. The source line now looks like this:
System.out.println("Result: " + result
- Press ⌘⇧↩ (Command + Shift + Return) to complete the statement. Method method1() now looks like this:
public void method1() {
double randomNumber = Math.random();
double result = randomNumber;
System.out.println("Result: " + result);
}
- Position the caret at the beginning of the first line of code in method method1(). Press ⌥⌘⇧] to select the text from the caret position to the end of the code block. Press ⌫ (Delete) to delete the selected text.