The Extract Method refactoring creates a method out of a code fragment. Select a block of code and press &shortcut:ExtractMethod; (Refactor | Extract | Method):

Extract method

This will result in the following:

Extract method