Top => IntelliJ on Windows/Linux
Kata for practicing keyboard shortcuts for basic code completion features
Key sequences to practice
- (Ctrl + Space) - basic code completion
- (Ctrl + Shift + Enter) - complete statement
- (Ctrl + 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
(Enter)
.
ran
(Enter)
(Ctrl + Shift + Enter)
(Enter)
double result = ra
(Enter)
(Ctrl + Shift + Enter)
(Enter)
S
(Enter)
.
(Enter)
.
(Enter)
System.out.println("Result: " + re
(Enter)
(Ctrl + Shift + Enter)
And then to revert:
- Place the caret at the beginning of the first line of code in method1().
- Press
(Ctrl + Shift + ])
(Delete)
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 (Enter) to select that item. The code now reads:
double randomNumber = Mathand 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 (Enter) to select it. The source line now looks like this:
double randomNumber = Math.random()- Press (Ctrl + Shift + Enter) 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 (Enter) to select it. The code in method1() now looks like this:
public void method1() {
double randomNumber = Math.random();
double result = randomNumber
}- Press (Ctrl + Shift + Enter) 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 (Enter) to select it. The source line now looks like this:
Systemand 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 (Enter) to select it. The source line now looks like this:
System.outand 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 (Enter) to select it. The source line now looks like this:
System.out.println("Result: " + result- Press (Ctrl + Shift + Enter) 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 (Ctrl + Shift +]) to select the text from the caret position to the end of the code block. Press (Delete) to delete the selected text.