Intellij IDEA generate for-each/for keyboard shortcut

前端 未结 7 850
我寻月下人不归
我寻月下人不归 2020-12-07 07:58

Is there a keyboard shortcut generating a foreach and also for loop?

相关标签:
7条回答
  • 2020-12-07 08:12

    Pressing Ctrl+J opens the list of live templates within your code context. Then type 'itin'.

    0 讨论(0)
  • 2020-12-07 08:16

    You can use Postfix Completion too. Press Ctrl + Alt + S to open the Settings. For example:

    int[] list = {1, 2, 3};    
    list.for -> for(int i : list) {}
    
    0 讨论(0)
  • 2020-12-07 08:17

    you can use 'live templates' to generate several types of code snippets, loop iteration is done by following -

    iter    Iterate (for each..in) 
    itin    Iterate (for..in) 
    itli    Iterate over a List
    itar    Iterate elements of array 
    ritar   Iterate elements of array in reverse order 
    

    There are probably many more, just lookup 'Live Templates' in help documentation.

    0 讨论(0)
  • 2020-12-07 08:24

    Create for loop from an existing list statement:

    1. Highlight the list
    2. Cmd+Alt+J or Ctrl+Alt+J
    3. Choose Iterate Iterable from the dropdown (i)

    https://www.jetbrains.com/phpstorm/help/creating-code-constructs-using-surround-templates.html

    0 讨论(0)
  • 2020-12-07 08:26

    You can also use Surround with!
    Mac : Command+Option+T
    Windows : Ctrl+Alt+T
    https://www.jetbrains.com/phpstorm/webhelp/surrounding-blocks-of-code-with-language-constructs.html

    There is also plenty of live templates in the internet ! you can just google it.

    0 讨论(0)
  • 2020-12-07 08:27

    Aditionally to other answers, you don't even have to use Ctrl + J (but nice for checking what are your possibilities..) Just start type and press Enter:

    Foreach: type "iter" then press Enter.

    For loop: type "fori" then press Enter.

    Idea will recognize the context and suggest the most likely var to iter through, if you like the given options, just press Enters!

    0 讨论(0)
提交回复
热议问题