Bug 150980 - [API] Selecting import declaration with space in outline highlights wrong range
Summary: [API] Selecting import declaration with space in outline highlights wrong range
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: All All
: P3 minor (vote)
Target Milestone: 3.7 M1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-18 13:27 EDT by Markus Keller CLA
Modified: 2010-08-05 01:44 EDT (History)
3 users (show)

See Also:


Attachments
Proposed fix + regression tests (26.37 KB, patch)
2010-07-27 22:11 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix + regression tests (39.23 KB, patch)
2010-07-28 12:04 EDT, Olivier Thomann CLA
no flags Details | Diff
Adoption of the new API in the JavaEditor (1.97 KB, patch)
2010-07-28 12:05 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2006-07-18 13:27:34 EDT
I20060718-0800

package xy;
import java. lang. Object;
public class Try { }

Select import declaration in Outline
-> expected: selects "java. lang. Object" or whole line in editor
-> was: selects "import java. lan" in editor

Reason: indexOf(..) does not work in JavaEditor.setSelection(.., ..):

String name= ((IImportDeclaration) reference).getElementName();
if (name != null && name.length() > 0) {
	String content= reference.getSource();
	if (content != null) {
		offset= range.getOffset() + content.indexOf(name);
		length= name.length();
	}
}
Comment 1 Dani Megert CLA 2006-07-19 04:22:47 EDT
This is ugly for us to parse. Can JDT Core give is getElementRange() (like we have for ILocalVariable and others) for this?

other cases:
import           java.lang.Math;
import           java.lang.Math           ;

And of course a mix of all those cases.
Comment 2 Dani Megert CLA 2006-07-19 05:11:17 EDT
>Can JDT Core give is getElementRange()
It should of course be "getNameRange()".

NOTE: as of now you can no longer reproduce this since I improved our heuristics.
Comment 3 Markus Keller CLA 2006-07-19 05:17:30 EDT
The same problem appears for IPackageDeclarations, which are also missing a getNameRange().
Comment 4 Olivier Thomann CLA 2009-06-25 14:05:32 EDT
So basically you want getNameRange() for IPackageDeclaration and IImportDeclaration.
None of them are actually IMember where the method getNameRange() is defined. So you just want the getNameRange() method to be added for these two types ?
Comment 5 Dani Megert CLA 2009-06-26 02:01:17 EDT
>So basically you want getNameRange() for IPackageDeclaration and
>IImportDeclaration.
Yes.
Comment 6 Olivier Thomann CLA 2010-07-27 16:35:37 EDT
Do you expect the import declaration on demand to include the '*' in its name source range ?
Comment 7 Olivier Thomann CLA 2010-07-27 22:11:43 EDT
Created attachment 175364 [details]
Proposed fix + regression tests
Comment 8 Olivier Thomann CLA 2010-07-27 22:12:40 EDT
This patch doesn't include the trailing '*' as part of the name for on demand imports. This can be done, but we need to store an extra position to do so.
It could also be patched at the UI side if needed.
Comment 9 Dani Megert CLA 2010-07-28 06:17:53 EDT
>This patch doesn't include the trailing '*' as part of the name for on demand
>imports.
You mean it ends with (or before) the '.'? Since we need the API to select the whole declaration I would expect to get the '*' as well. If that's not the case then the new API still forces us to scan the code and hence we wouldn't spend time to adopt it.
Comment 10 Olivier Thomann CLA 2010-07-28 08:45:22 EDT
(In reply to comment #9)
> You mean it ends with (or before) the '.'? Since we need the API to select the
> whole declaration I would expect to get the '*' as well. If that's not the case
> then the new API still forces us to scan the code and hence we wouldn't spend
> time to adopt it.
This is all I wanted to know. If you need the '*' as part of the name source range, I'll add it.
Thanks.
Comment 11 Dani Megert CLA 2010-07-28 08:46:41 EDT
Thanks! ;-)
Comment 12 Olivier Thomann CLA 2010-07-28 12:04:34 EDT
Created attachment 175422 [details]
Proposed fix + regression tests

New patch that includes the trailing '*' as part of the import on demand name range.
Comment 13 Olivier Thomann CLA 2010-07-28 12:05:56 EDT
Created attachment 175423 [details]
Adoption of the new API in the JavaEditor

This patch enables the new API in the java editor and makes it possible to select the proper source code from the outline view when selecting an import declaration or a package declaration with spaces inside the name.
Markys, let me know what you think.
Comment 14 Olivier Thomann CLA 2010-07-28 12:18:23 EDT
Released for 3.7M1.
Added regression tests in:
org.eclipse.jdt.core.tests.model.GetSourceTests#testNameRange09
org.eclipse.jdt.core.tests.model.GetSourceTests#testNameRange10
org.eclipse.jdt.core.tests.model.GetSourceTests#testNameRange11
Comment 15 Dani Megert CLA 2010-07-29 03:47:28 EDT
Thanks for the patch. Committed to HEAD.

Olivier, when you now look at the code in JavaEditor you see the huge if-then-else clause. Maybe we could add it to ISourceRange. I think the only deal breaker for that would be the import container. Otherwise we might want to add a new interface. What do you think?
Comment 16 Markus Keller CLA 2010-08-04 10:17:11 EDT
(In reply to comment #15)
> Maybe we could add it to ISourceRange.

I guess you meant ISourceReference. That would mean that getNameRange() has to be added to IImportContainer and ITypeRoot. We could just say it returns null in those types.
Comment 17 Dani Megert CLA 2010-08-04 10:24:35 EDT
>I guess you meant ISourceReference.
Yep, of course.
Comment 18 Olivier Thomann CLA 2010-08-04 13:47:30 EDT
We could do that post M1. I don't want to change this now.
Comment 19 Olivier Thomann CLA 2010-08-04 13:49:20 EDT
Opened bug 321764 to take care of this.
Comment 20 Satyam Kandula CLA 2010-08-05 00:52:01 EDT
Verified for 3.7M1 using build I20100802-1800
Comment 21 Dani Megert CLA 2010-08-05 01:44:58 EDT
>We could do that post M1. I don't want to change this now.
Yes, of course.