Top => IntelliJ IDEA on Mac OS X
In this kata, we select several lines and extract them into a method. The automated extract method refactoring in IntelliJ IDEA does not generate exactly the result we want, and we must edit the new method manually to complete the refactoring.
- ⌃⇥ (Ctrl + Tab) - navigate from the project view pane to an editor pane
- ⌘↖︎ (Command + Home) or fn⌘← (Fn + Command + Left Arrow) - navigate to the top of the file (in editor)
- ⌥→ (Option + Right Arrow) - move forward by token
- ⌥⇧→ (Option + Shift + Right Arrow) - move forward by token while selecting
- ⌥← (Option + Left Arrow) - move backward by token
- → (Right Arrow) - move the caret one position to the right
- ⌘⌥⇧] (Command + Option + Shift + ]) - select from caret through end of block
- ⇧ and ↓ (Shift) with (Down Arrow) - select lines
- ⌘⌥M (Command + Option + M) - open the "extract method" dialog
- fnF2 (Fn + F2) - go to the next error in the file
- ⌘⏎ (Command + Return/Enter) - "smart" line split (preserves formatting)
- ⌘⌫ (Command + Delete/Backspace) - delete the current line (with nothing selected)
- ⌃↓ (Ctrl + Up Arrow) - Move backward to the previous method
Keystrokes without explanations
⌃⇥
⌘↖︎ (or fn⌘←)
⌘L 12
hold ⇧
↓ (repeat through line 29)
release ⇧
⌘⌥M
Visibility: Public
Name: validateArguments
Arguments: stringArg
OK
fnF2
⌥→
⌥→
⌘⏎
⌘⌫
⌥←
"
fnF2
⌘⌫
fnF2
⌘⌫
fnF2
⌥⇧→
⌥⇧→
⌥⇧→
!result.equals("")
fnF2
⌘⌫
⌃↑
⌥⇧←
void
fnF2
⌘⌫
fn⇧F2
⌥⇧→
⌥⇧→
⌫
Walkthrough of steps with explanations
- Open the Java source file, LongConditional.java.
- If focus is not already in the editor pane for LongConditional.java, press ⌃⇥ (Ctrl + Tab) and select the editor pane for LongConditional.java.
- Press ⌘L (Command +L) to open the "go to line number" dialog and navigate to line 12.
- Hold ⇧ (Shift) and press ↓ (Down Arrow) until lines 12 through 29 inclusive are selected.
- Press ⌘⌥M (Command + Option +M) to open the "extract method" dialog.
- In the "extract method" dialog, select:
- Visibility: Public
- Name: validateArguments
- Arguments: stringArg, intArg
- Choose OK. The generated method has syntax errors.
- Press fnF2 (Fn + F2) to go to the next error in the file. That should take you to line 58, which should look like this:
int errorCount = ; String result = ;
with the caret positioned just after the first equals sign (=).
- We don't need the variable errorCount at all. Press ⌥→ (Option + Right Arrow) twice to advance the caret up to the token, "String".
- Press ⌘⏎ (Command + Return/Enter) to do a "smart" line split. The caret remains on the first line, which we now want to delete.
- Press ⌘⌫ (Command + Delete/Backspace) to delete the current line. The caret is left at the end of the line reading, "String result = ;".
- Press ⌥← (Option + Left Arrow) to move the caret before the semicolon.
- Initialize the variable result to an empty string, so it looks like this:
String result = "";
- Now we can delete the lines that refer to errorCount. Press fnF2 (Fn + F2) to move focus to the next error. Press ⌘⌫ (Command + Delete) to delete that line. Do this sequence of keystrokes twice to delete the lines where errorCount is incremented.
- Press fnF2 (Fn + F2) one more time to position the caret at the beginning of the token, "errorCount" on line 69, which should read
if (errorCount > 0) {
- Press ⌥⇧→ (Option + Shift + Right Arrow) three times to select the text inside the parentheses. The selected text should be
errorCount > 0
- Overtype the selection with the text, "!result.equals("")", resulting in:
if (!result.equals("")) {
-
Now we will delete the remaining line that contains a reference to errorCount. Press fnF2 (Fn + F2) to move the caret to the next error, and ⌘⌫ (Command + Delete/Backspace) to delete that line.
-
The method should not return anything. Press ⌃↑ (Ctrl + Up Arrow) to move the caret to the method declaration.
-
Press ⌥⇧← (Option + Shift + Left Arrow) to move the caret ahead to the token "String" (method return type) and select it.
-
Overtype the selection with "void", so the line now looks like this:
private void validateArguments(String stringArg, int intArg) {
at this point, the return statement is marked as an error.
- Press fnF2 (Fn + F2) to advance the caret to the next error.
- Press ⌘⌫ (Command + Delete/Backspace) to delete the line containing the return statement.
- By changing this method to return void, we introduced an error on the line that calls the method. Press fn⇧F2 (Fn + Shift + F2) to move the caret to the previous error. This is the line that calls the validateArguments method.
- Press ⌥⇧→ (Option + Shift + Right Arrow) twice to select the text, "result = ".
- Press ⌫ (Delete/Backspace) to delete the selection. The line should now look like this:
validateArguments(stringArg, intArg);
To reverse the changes:
- Press ⌘Z (Command + Z) repeatedly until the changes have been reversed. Should be 30 times.