Community
Participate
Working Groups
Currently the loop var name that the quick fix uses is always suggesting "element" as basename for the loop var. While not wrong sometimes it is easy to guess a better name taking the loop array or list name into account and derive a better name. For a name "children" the name "child" might be suitable. For array or list names ending on "s" the single name could be derived by cutting the "s" from the variable name.
That does not fit my experience with converting index-based loops to enhanced for loops. If a local variable is assigned using the loop index (at the beginning of the loop inner statement block), the quickfix uses that name, not "element". Also, you can use Ctrl-Space on the variable name while still in linked mode and it will suggest type based names, like binding, typeBinding and iTypeBinding for an ITypeBinding typed loop var. I'm not sure, but org.eclipse.jdt.internal.corext.fix.ConvertForLoopOperation.getVariableNameProposalsCollection(MethodInvocation, IJavaProject) might be what you are looking for. It does have handling of plurals already, but does not specifically deal with "children", AFAICS.
(In reply to Michael Keppler from comment #1) > That does not fit my experience with converting index-based loops to > enhanced for loops. If a local variable is assigned using the loop index (at > the beginning of the loop inner statement block), the quickfix uses that > name, not "element". Also, you can use Ctrl-Space on the variable name while > still in linked mode and it will suggest type based names, like binding, > typeBinding and iTypeBinding for an ITypeBinding typed loop var. > > I'm not sure, but > org.eclipse.jdt.internal.corext.fix.ConvertForLoopOperation. > getVariableNameProposalsCollection(MethodInvocation, IJavaProject) might be > what you are looking for. It does have handling of plurals already, but does > not specifically deal with "children", AFAICS. Thanks for the hints. The plural handling with 's' is in fact there, you are right. I added some rudimentary handling of other plural cases in https://git.eclipse.org/r/#/c/155119/ Feel free to review.
*** Bug 558409 has been marked as a duplicate of this bug. ***
I added a small change to get rid of loop vars "string". It now uses "element" instead. I had to change some junit tests for that. Please check if that is ok for you. Otherwise I will rollback.
Preventing "string" as variable could be done alternatively in a step earlier. Maybe that would be better than catching this after creating the variable name based on the datatype - not sure about this. See https://git.eclipse.org/r/#/c/158146/1/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/OrganizeImportsOperation.java for a sample of such a "string" variable that is created currently and that I want to prevent. I added a check and cut of a "all" prefix. See the last refactoring on https://git.eclipse.org/r/#/c/158151/3/org.eclipse.jdt.core.manipulation/core+extension/org/eclipse/jdt/internal/corext/refactoring/structure/MemberCheckUtil.java do see what I mean. Of course in this case I changed it to "m" afterwards.
Gerrit change https://git.eclipse.org/r/155119 was merged to [master]. Commit: http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/?id=86d2764bcb0a4c37c9865a2df7655dab5665b951
Released for 4.16M1