Bug 283539 - NamingConventions.suggestVariableNames doesn't work if name contains '_'
Summary: NamingConventions.suggestVariableNames doesn't work if name contains '_'
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 3.6 M3   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-15 08:39 EDT by Dani Megert CLA
Modified: 2009-10-26 10:57 EDT (History)
4 users (show)

See Also:


Attachments
Proposed Patch (9.57 KB, patch)
2009-09-14 04:11 EDT, Jay Arthanareeswaran CLA
srikanth_sankaran: iplog+
srikanth_sankaran: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2009-07-15 08:39:40 EDT
R3.5 and I20090714-0800 (introduced in R3.5).

NamingConventions.suggestVariableNames(int, int, String, IJavaProject, int, String[], boolean) doesn't work if name contains '_'.

Test Case, see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=263058#c6

Basically, we call the method like this:

NamingConventions.suggestVariableNames(NamingConventions.VK_PARAMETER, NamingConventions.BK_TYPE_NAME, "lMinTriggerPeriod_usec", project, 0, [], true);

And get this back:
["lMinTriggerPeriodUsec", "minTriggerPeriodUsec", "triggerPeriodUsec", "periodUsec", "usec"]

The Javadoc says:

	 * The different kinds of base names are:
	 * <ul>
	 * <li>{@link #BK_NAME}: the base name is a Java name and the whole base name is considered to compute the variable names. A prefix and a suffix can be added.<br>
	 * There is a heuristic by variable kind.
	 * <ul>
	 * <li>{@link #VK_PARAMETER}, {@link #VK_LOCAL}, {@link #VK_INSTANCE_FIELD} and {@link #VK_STATIC_FIELD}:<br>
	 * In this case the first character will be converted to lower case and the other characters won't be changed.<br>
	 * If the base name is <code>SimpleName</code> then the suggested name will be <code>simpleName</code>.<br></li>
Comment 1 Olivier Thomann CLA 2009-07-15 09:23:20 EDT
So you expect the '_' character to be there in the proposals ?
Comment 2 Dani Megert CLA 2009-07-15 09:25:32 EDT
Right, that's what I would expect from reading the Javadoc.
Comment 3 Olivier Thomann CLA 2009-07-15 09:29:34 EDT
This might impact existing users. So we might want to consider fixing the documentation as well.
Comment 4 Dani Megert CLA 2009-07-15 09:35:07 EDT
>So we might want to consider fixing the documentation as well.
You mean instead of fixing the bug?
Comment 5 Olivier Thomann CLA 2009-07-15 09:37:17 EDT
Yes. If existing users have some code that relies on removing the '_', they would be broken once this is fixed.
Comment 6 Dani Megert CLA 2009-07-15 09:41:46 EDT
The API was made on request from JDT UI and most likely they are the only clients of it and currently it's breaking their features. If JDT Core only intends to update the doc then you have to provide a new working API for JDT UI and deprecate the old one.
Comment 7 Olivier Thomann CLA 2009-07-15 09:44:11 EDT
Ok, I was not aware of this. We should then fix the bug.
Comment 8 Jay Arthanareeswaran CLA 2009-09-10 03:58:18 EDT
So, what do we expect for inputs like these - "MY_FIRST_Type" and "my_first_Type".

Right now the output is {"myFIRSTType", "firstType","type"} and 
{"myFirstType", "firstType", "type"} respectively. 

I think we need to come up with the scenarios where '_' will be considered a delimiter.
Comment 9 Markus Keller CLA 2009-09-10 11:48:52 EDT
I don't expect any special processing of '_', since '_' has no special meaning inside a base name. If the original name contains '_' and is the name of a constant, clients should already have passed it to NamingConventions.getBaseName(NamingConventions.VK_STATIC_FINAL_FIELD, ..) to convert it into a baseName and get rid of the '_'s.

So, I expect what the Javadoc says:
- "lMinTriggerPeriod_usec" -> "lMinTriggerPeriod_usec", "minTriggerPeriod_usec", "triggerPeriod_usec", "period_usec"
- "MY_FIRST_Type" -> "mY_FIRST_Type"
- "my_first_Type" -> "my_first_Type"

As an alternative, you could also consider '_' as a word-separator, i.e.:
- "lMinTriggerPeriod_usec" -> "lMinTriggerPeriod_usec", "minTriggerPeriod_usec", "triggerPeriod_usec", "period_usec", "usec"
- "MY_FIRST_Type" -> "mY_FIRST_Type", "fIRST_TYPE", "tYPE"
- "my_first_Type" -> "my_first_Type", "first_Type", "type"
Comment 10 Jay Arthanareeswaran CLA 2009-09-11 07:30:25 EDT
I had a look at the NamingConvensions code and the tests and it looks like the code has been written to convert the first word to lower case instead of the first letter, as mentioned in the doc. I just confirmed this with David.

So, do we want to keep this behavior and fix the '_' problem? This will produce outputs like these:

MY_FIRST_Type -> "my_FIRST_Type", "first_Type", "type"
my_first_Type -> "my_First_Type", "first_Type", "type"
Comment 11 Markus Keller CLA 2009-09-11 08:15:25 EDT
That would also be fine with me, iff you fix the Javadoc and
> my_first_Type -> "my_First_Type", [..]
was a typo and really meant:
                -> "my_first_Type"
Comment 12 Jay Arthanareeswaran CLA 2009-09-14 04:11:22 EDT
Created attachment 147076 [details]
Proposed Patch

Attaching the patch with documentation change and tests. The fix required changes to some of the existing tests.
Comment 13 Dani Megert CLA 2009-09-17 12:10:00 EDT
I didn't verify the patch in detail but the general direction looks good and I verified that
- it fixes the original issue from bug 263058 comment 6
- our JDT Text and JDT UI tests still pass
Comment 14 Srikanth Sankaran CLA 2009-10-07 06:40:03 EDT
Released in HEAD for 3.6M3 (after minor indentation changes)
Comment 15 Olivier Thomann CLA 2009-10-20 10:45:20 EDT
Set target milestone.
Comment 16 Frederic Fusier CLA 2009-10-26 10:57:53 EDT
Verified for 3.6M3 using build I20091026-2000