Bug 558700 - [quick fix] convert to enhanced for loop should have better loop var suggestion
Summary: [quick fix] convert to enhanced for loop should have better loop var suggestion
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.15   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: 4.16 M1   Edit
Assignee: Carsten Hammer CLA
QA Contact: Jeff Johnston CLA
URL:
Whiteboard:
Keywords: noteworthy, usability
: 558409 (view as bug list)
Depends on:
Blocks: 560385
  Show dependency tree
 
Reported: 2019-12-31 06:17 EST by Carsten Hammer CLA
Modified: 2020-03-11 13:59 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carsten Hammer CLA 2019-12-31 06:17:00 EST
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.
Comment 1 Michael Keppler CLA 2019-12-31 09:31:58 EST
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.
Comment 2 Carsten Hammer CLA 2019-12-31 10:11:49 EST
(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.
Comment 3 Lars Vogel CLA 2020-02-24 10:45:00 EST
*** Bug 558409 has been marked as a duplicate of this bug. ***
Comment 4 Carsten Hammer CLA 2020-03-06 12:26:10 EST
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.
Comment 5 Carsten Hammer CLA 2020-03-08 06:34:26 EDT
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.
Comment 7 Jeff Johnston CLA 2020-03-11 13:59:25 EDT
Released for 4.16M1