Skip to content

Latest commit

 

History

History
212 lines (173 loc) · 6.68 KB

ij-osx-smart-code-completion.md

File metadata and controls

212 lines (173 loc) · 6.68 KB

Top => IntelliJ on OS X

JetBrains IntelliJ IDEA on Mac OS X

Kata for practicing keyboard shortcuts for smart code completion features. From the documentation: "Smart code completion filters the suggestions list and shows only the types applicable to the current context."

Kata: Smart code completion

Key sequences to practice

  • ⌃⇧␣ (Ctrl + Shift + Space) - smart 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.
Lis
⏎
<Int
→
␣
⏎
⌃⇧␣
⏎
⌃⇧
⏎
⏎
i
⏎
.
⏎
5
⌘⇧⏎
⏎
if (i
⏎
.is
⏎
{
␣
⏎
thro
⏎
n
⏎
R
⏎
"WTF??"
⌘⇧⏎

And then to revert:

  1. Place the caret at the beginning of the first line of code in method1().
  2. Press
⌥⌘⇧]
⌫

Walkthrough of steps with explanations

  1. Open the Java source file, CodeCompletionPractice.java.
  2. Begin editing at the top of method(1).
  3. Type
        Lis

The editor presents a list of possibilities and narrows the list as you type. Once you have typed "Lis" only one entry remains: List. Press ⏎ (Return or Enter) to select it.

Unfortunately, this leaves you with

        List

instead of

        List<
  1. Type "<Int" so that the source line looks like this:
        List<Int

The editor presents a list of possibilities and narrows the list as you type. Once you have typed "Int" only one entry remains: List. Press ⏎ (Return or Enter) to select it.

The source line now looks like this:

        List<Integer>

with the caret positioned before the closing ">".

  1. Press → (Right Arrow) to move the caret past the closing ">".
  2. Press ␣ (Space) to insert a space after the closing ">".
  3. Press ⌃⇧␣ (Ctrl + Shift + Space) to activate smart code completion. The editor presents a list of somewhat reasonable variable names to follow the List declaration.
  4. Press ⏎ (Return or Enter) to select the first suggestion, "integers". The source line now looks like this:
        List<Integer> integers

with the caret positioned just after the token, "integers".

  1. Type this much of the source line (ending with a space, which you can't see in the sample):
        List<Integer> integers = new
  1. Press ⌃⇧␣ (Ctrl + Shift + Space). The editor presents a list of suggestions. The highlighted entry is "ArrayList (java.lang)".

  2. Press ⏎ (Return or Enter) to select te highlighted suggestion. The source line now looks like this:

        List<Integer> integers = new ArrayList<Integer>();
  1. Start entering the next line of source code. Type:
        i

The editor presents a list of suggestions. The first one is "integers", the name of the local variable declared on the previous source line.

  1. Press ⏎ (Return or Enter) to select "integers". The source line now looks like this:
        integers
  1. Type . (Period or Dot). The editor presents a list of suggestions. The first one is "add(Integer e)".

  2. Press ⏎ (Return or Enter) to select "add(Integer e)". The source line now looks like this:

        integers.add()

with the caret positioned between the parentheses.

  1. Type "5". The source line now looks like this:
        integers.add(5)

with the caret positioned after the 5 and before the closing parenthesis.

  1. Press ⌘⇧⏎ (Command + Shift + Return) to auto-complete the source statement. The editor skips to the end of the line and adds a semicolon. The source line now looks like this:
        integers.add(5);
  1. On the next line, type "if (i". The editor will present suggestions as you type. After typing the characters shown, the first suggestion will be "integers", the name of the local variable you declared earlier in the method.

  2. Press ⏎ (Return or Enter) to select "integers". The source line now looks like this:

        if (integers)

with the caret positioned between "integers" and ")".

  1. Type ".is". The editor will present suggestions as you type. After typing the characters shown, the first suggestion will be "isEmpty()".

  2. Press ⏎ (Return or Enter) to select "isEmpty()". The source line now looks like this:

        if (integers.isEmpty())
  1. Type " {⏎" (Space, then "{", then Return). The source statement now looks like this:
        if (integers.isEmpty()) {

        }

with the caret appropriately indented on the blank line between the curly braces.

  1. Type "thro". The editor will present suggestions as you type. After typing the characters shown, the only suggestion remaining is "throw".

  2. Press ⏎ (Return or Enter) to select the suggestion, "throw". The editor completes the token "throw" and adds a blank space after it. The source statement now looks like this:

        if (integers.isEmpty()) {
            throw
        }
  1. Type "n" and the first suggestion is "new".

  2. Press ⏎ (Return or Enter) to select "new". The editor completes the token "new" and adds a blank space after it. The source statement now looks like this:

        if (integers.isEmpty()) {
            throw new
        }
  1. Type "R" and the first suggestion is "RuntimeException".

  2. Press ⏎ (Return or Enter) to select "RuntimeException". The source statement now looks like this:

        if (integers.isEmpty()) {
            throw new RuntimeException()
        }

with the caret positioned between the parentheses.

  1. Type "WTF??" (in quotes) as the message for the exception. The editor will add the closing quotation mark when you type the first one. The caret will end up between the second "?" and the closing parenthesis.

  2. Press ⌘⇧⏎ (Command + Shift + Return) to complete the statement. The editor will skip to the end of the statement and add a semicolon. The code in method1() now looks like this:

    public void method1() {
        List<Integer> integers = new ArrayList<Integer>();
        integers.add(5);
        if (integers.isEmpty()) {
            throw new RuntimeException("WTF??");
        }
    }
  1. Reverse the changes so you can repeat the sequence for practice. Position the caret at the start of the first line of code in method1(). Press ⌥⌘⇧] (Option + Command + Shift + ]) to select text up to the end of the block. Press ⌫ (Delete) to delete the code.