“Surround with”-template in Eclipse: foreach

后端 未结 4 785
借酒劲吻你
借酒劲吻你 2021-02-04 14:58

I am new to Eclipse which I use primarily for Java. I have previously used IntelliJ Idea in which it is possible to select a variable which extends Iteratable (Collection, List

4条回答
  •  孤独总比滥情好
    2021-02-04 15:47

    Update 2:

    In Eclipse 4.4 What's New in Luna (JDT) a QuickFix was added for this issue. This can be also used for arrays, collections and maps (key sets and values).

    enter image description here

    Update 1

    I recently found an eclipse plugin that provides postfix code completion (https://github.com/trylimits/Eclipse-Postfix-Code-Completion). This is how it works for the foreach loop:

    Beside, the plugin provides more helpful competitions :)

    For Eclipse before 4.4:

    I'm also having the same issue but I wasn't able to find a solution. So, for the moment I perform following steps to get the desired loop.

    fore[Ctrl-space] and select the foreach template, I get the following:

    for (iterable_type iterable_element : iterable) {
    
    }
    

    then I perform a double click on iterable and replace it with the method:

    for (iterable_type iterable_element : functionWhichReturnsList()) {
    
    }
    

    on the next step just click on functionWhichReturnsList() and hit Ctrl-1. Eclipse will suggest to change the type iterable_element to TypeOfItemsInList. This is want you get at the end:

    for (TypeOfItemsInList iterable_element : functionWhichReturnsList()) {
    
    }
    

    Now, you just need to find a proper name for iterable_element. Just double click on it and start typing.

提交回复
热议问题