Bug 489650 - [organize imports] resolves star imports
Summary: [organize imports] resolves star imports
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.5.2   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-15 09:36 EDT by Marvin Fröhlich CLA
Modified: 2023-03-09 04:25 EST (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marvin Fröhlich CLA 2016-03-15 09:36:14 EDT
The organize imports functionality resolves all the star imports and replaces them with (many) concrete imports. I think, it should let those untouched. It should be as little invasive as possible.

Maybe even an option to not touch the exiting order would be welcome.
Comment 1 Nitin Dahyabhai CLA 2016-03-15 10:56:28 EDT
The default preferences do not Organize Imports automatically. Everything except not changing the exit order can be controlled from the Organize Imports preference page.
Comment 2 Dani Megert CLA 2016-03-15 12:06:56 EDT
(In reply to Marvin  Fröhlich from comment #0)
> The organize imports functionality resolves all the star imports and
> replaces them with (many) concrete imports. I think, it should let those
> untouched. It should be as little invasive as possible.
> 
> Maybe even an option to not touch the exiting order would be welcome.

You can also define the order in the preferences.


There are no plans to add an option to preserve the existing imports.
Comment 3 Marvin Fröhlich CLA 2018-02-09 03:18:04 EST
I had a look at the source code. And it is pretty simple to handle this.

See org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.processTypeInfo

I have a code change suggestion:

#######################################################
         private TypeNameMatch[] processTypeInfo(List<TypeNameMatch> typeRefsFound) {
             int nFound= typeRefsFound.size();
             if (nFound == 0) {
                 // nothing found
                 return null;
             } else if (fDoPreserveDemandImports && fOldDemandImports.contains(typeRefsFound.get(0).getTypeContainerName())) {
                 // The original code had a * import for this type. Preserve it!
                 TypeNameMatch typeRef= typeRefsFound.get(0);
                 String containerName= typeRef.getTypeContainerName();
                 fImpStructure.addImport(containerName);
                 return null;
             } else if (nFound == 1) {
                 TypeNameMatch typeRef= typeRefsFound.get(0);
                 fImpStructure.addImport(typeRef.getFullyQualifiedName());
                 return null;
             } else {
                 String typeToImport= null;
                 boolean ambiguousImports= false;

                 // multiple found, use old imports to find an entry
                 for (int i= 0; i < nFound; i++) {
                     TypeNameMatch typeRef= typeRefsFound.get(i);
                     String fullName= typeRef.getFullyQualifiedName();
                     String containerName= typeRef.getTypeContainerName();
                     if (fOldSingleImports.contains(fullName)) {
                         // was single-imported
                         fImpStructure.addImport(fullName);
                         return null;
                     } else if (fOldDemandImports.contains(containerName) || fImplicitImports.contains(containerName)) {
                         if (typeToImport == null) {
                             typeToImport= fullName;
                         } else {  // more than one import-on-demand
                             ambiguousImports= true;
                         }
                     }
                 }

                 if (typeToImport != null && !ambiguousImports) {
                     fImpStructure.addImport(typeToImport);
                     return null;
                 }
                 // return the open choices
                 return typeRefsFound.toArray(new TypeNameMatch[nFound]);
             }
         }
#######################################################

There would have to be a new preferences option: Preserve star imports
This option would make the organize imports process add a star import for any type, that is covered by an already (previously) existing star import.
Comment 4 Pierre-Yves Bigourdan CLA 2019-07-17 09:29:42 EDT
This seems like a useful option Marvin, why not submit a patch? :)
Comment 5 Marvin Fröhlich CLA 2019-07-19 09:01:22 EDT
Well, the code above should be some kind of a patch. I don't have the whole eclipse code checked out here. So providing an actual patch is a little difficult.

But if this would lead to this change being accepted, I will try. What do you say?
Comment 6 Pierre-Yves Bigourdan CLA 2019-07-19 15:52:51 EDT
Well, if the patch is concise, tested and disabled by default, I see no reason not to accept it. However, this is only my personal point of view, I'm not part of the team, simply an occasional contributor. :)

FYI the contribution docs are here: https://wiki.eclipse.org/JDT_Core_Committer_FAQ
Comment 7 Jay Arthanareeswaran CLA 2019-08-20 01:14:37 EDT
Since the suggested code change is in UI, moving the bug accordingly.
Comment 8 Eclipse Genie CLA 2021-08-10 16:16:19 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 9 Marvin Fröhlich CLA 2021-08-11 01:30:50 EDT
The problem is not solved. The description remains unchanged. My suggested fix will still work, I suppose.

If I have a little time, I will add it myself. But the hurdle of checking out Eclipse is high due to missing documentation. If you can help me out here, I would happily add the fix myself.
Comment 10 Jay Arthanareeswaran CLA 2021-08-24 00:18:32 EDT
(In reply to Marvin  Fröhlich from comment #9)
> The problem is not solved. The description remains unchanged. My suggested
> fix will still work, I suppose.
> 
> If I have a little time, I will add it myself. But the hurdle of checking
> out Eclipse is high due to missing documentation. If you can help me out
> here, I would happily add the fix myself.

I don't use Oomph myself, but lot of people think it is fairly easy to set up JDT with Oomph:

https://wiki.eclipse.org/JDT_Code_Setup_Using_Oomph
Comment 11 Marvin Fröhlich CLA 2022-12-01 12:18:46 EST
I have created a pull request for this improvement/fix. Please have a look.

https://github.com/azachar/org.eclipse.jdt.ui/pull/1

Would be great to see this in the next release.

Cheers!
Comment 12 Pierre-Yves Bigourdan CLA 2022-12-04 09:48:52 EST
I believe https://github.com/eclipse-jdt/eclipse.jdt.ui is the correct repository to submit pull requests against. :)
Comment 13 Marvin Fröhlich CLA 2022-12-05 02:55:53 EST
Ok, I searched via google to find it. Sorry for using the wrong repo.

I am not too familiar with this repo. Would you mind helping me to find the OrganizeImportsOperation.java class in the correct repo?
Comment 14 Marvin Fröhlich CLA 2022-12-05 03:01:50 EST
Ok, got it. I downloaded the zip archive and found hte file in there and derived the path to the web repo.
Comment 15 Marvin Fröhlich CLA 2022-12-05 03:22:34 EST
Ok, I think, I have created a new pull request for the change.

https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/342

I hope, that worked, because the UI tell me, I  am not covered by legal agreement, even though I have agreed the new terms.

Could you please check, if the pull request is online.
Comment 16 Marvin Fröhlich CLA 2023-03-09 04:12:52 EST
Any news here? Would be a pity, if this patch was not included in the upcoming release.
Comment 17 Andrey Loskutov CLA 2023-03-09 04:25:02 EST
(In reply to Marvin  Fröhlich from comment #16)
> Any news here? Would be a pity, if this patch was not included in the
> upcoming release.

Marvin, *this* bug tracker is the wrong place to discuss changes on the PR you've submitted at github.

We should discuss that on https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/342.
There were changes requested, I see you've fixed that.

I will comment on https://github.com/eclipse-jdt/eclipse.jdt.ui/pull/342 what else is missing now. Thanks.