Community
Participate
Working Groups
Any comments between import statements, eg, copyrights, are lost when doing organize imports. They should be preserved.
The problem is, how? Are these comments belonging to an import, to a group? I'm afraid there can't be done much.
There are plans to support comments in ASTs. Postponing until we have more support here.
*** Bug 48461 has been marked as a duplicate of this bug. ***
*** Bug 61695 has been marked as a duplicate of this bug. ***
*** Bug 242042 has been marked as a duplicate of this bug. ***
*** Bug 102939 has been marked as a duplicate of this bug. ***
*** Bug 34823 has been marked as a duplicate of this bug. ***
*** Bug 3772 has been marked as a duplicate of this bug. ***
*** Bug 32604 has been marked as a duplicate of this bug. ***
ImportRewrite is now owned by JDT Core.
*** Bug 263640 has been marked as a duplicate of this bug. ***
The problem is that import reorganize moves the imports, add some and remove some according to the preferences (especially the reordering). My problem is that we use comments to specify a block that should not be reorganized : import java.util.List; //Start of user code import java.util.ArrayList; import mycompany.MyObject; //End of user code i understand that is difficult to do, and there is lot of questions ( how to do with just one comment ? remove unused imports ? where to add new ones ?? ) cheers, Stéphane
*** Bug 332879 has been marked as a duplicate of this bug. ***
This is an important issue and should finally be addressed. There are pragmatic solutions to the problems mentioned. Say, divide the import statements into ''import groups'' which are separated by comments, then * Do not move imports out of their import group, only order the imports in each import group relatively to each other. * If imports are merged place the merged imports where the first of the original imports has been. * If new imports are added, add them below the best matching exisitng import. (Or more pragmatically at the end of the list.) * If a comment is on the same line behind an import, do not merge that import with others. This may not be perfect but is certainly better than just throwing away comments anywhere near the imports!
(In reply to comment #14) > This is an important issue and should finally be addressed. There are pragmatic > solutions to the problems mentioned. > > Say, divide the import statements into ''import groups'' which are separated by > comments, then > * Do not move imports out of their import group, only order the imports in > each import group relatively to each other. > * If imports are merged place the merged imports where the first of the > original imports has been. > * If new imports are added, add them below the best matching exisitng import. > (Or more pragmatically at the end of the list.) > * If a comment is on the same line behind an import, do not merge that import > with others. > > This may not be perfect but is certainly better than just throwing away > comments anywhere near the imports! There is some limited support for "import groups" on the Organize Imports preference page. It seems to me conceptually user-organized imports such as you describe is incompatible with the concept of Organize Imports. Personally I would be happy if the comments were preserved in any form (such as all comments moved above first import, or each comment stays with the import immediately below it). To me the major bug is the pure loss of user data today, such as comment #0 where the copyright was accidentally placed between imports and got deleted. I find it doubtful that an automatic import tool can preserve the correct intended position of all comments in all cases.
John Arthorne, what options on the Organize Import do you mean? There should really be a way to not delete the comments when rewriting the imports. Simple example: // --- begin code -- package bla; import java.util.ArrayList; // added by user xy: import java.util.Collection; public class Foo { ArrayList<Integer> foo; } // --- end code -- Pressing organize imports will remove the comment "// added by user xy:" as well as the import below. Why not just leave the comment as is?
(In reply to comment #16) > John Arthorne, what options on the Organize Import do you mean? The Organize Imports preference page allows some limited amount of grouping - for example com.* imports before java.* imports, etc. > Pressing organize imports will remove the comment "// added by user xy:" as > well as the import below. Why not just leave the comment as is? I completely agree we should not delete the comments. I'm just saying it won't be possible for an automated tool to always preserve the correct comment position. To modify your example: // --- begin code -- package bla; import java.util.ArrayList; // added by user xy: import java.util.Collection; import java.util.List; public class Foo { List<Integer> foo = new ArrayList<Integer>(); } // --- end code -- Using a simple rule to preserve comments we might end up with: // --- begin code -- package bla; import java.util.ArrayList; // added by user xy: import java.util.List; public class Foo { List<Integer> foo = new ArrayList<Integer>(); } // --- end code -- Now it looks like the java.util.List was added by user xy, which is not what the comment author intended. Guessing the right place for all comments in the general case is not possible. I am just arguing that fixing the data loss problem is more important than trying to come up with a complicated algorithm to preserve intended comment positions.
At least the data loss needs to be fixed for 3.8.
I'll take a look.
Created attachment 201821 [details] First draft I haven't heavily tested it, but this is one step in the right direction. Since the organize import operation is called to not preserve existing imports it is difficult to position the comments relative to the previous imports even if they are the same. At least with this patch, the comments are not lost anymore. They are all moved to the end of the imports after the rewrite.
I have a small issue with the comment before the import (between the package declaration and the first import). With the patch, it ends up being duplicated. I am working on it.
Created attachment 201822 [details] Patch v2 Fix the issue with first comment.
Now I need to add support for comments inside the imports that are not part of any import extended range. Will be fixed with the next patch.
Created attachment 201837 [details] Patch v3 This seems to preserve all existing comments. I'll test it more in the case the existing imports are preserved inside the ImportRewriteAnalyzer.
Created attachment 202555 [details] Patch v4 Same with minor typos fixed. I won't work on relocating comments around existing imports. The current patch makes sure that no comments end up being lost. I'll release it for now. This should be good enough to close this bug.
Markus, 3 tests are failing in the org.eclipse.jdt.ui.tests.core.ImportOrganizeTest tests. I'll provide a patch for those.
Created attachment 202562 [details] Patch v5 This patch removes the whitespaces in front of comments when preserving them and it contains the patch for jdt.ui test failures (tests with comments inside imports that were expected to be lost). Markus, let me know when you want to release the fix in the tests. We need to synchronize the release of this patch.
If comments between imports are important to the developer, the developer should be able to turn off reorganization for those files. In the spirit of Bug 311617, can something like //@organizeimports:off be implemented? If that comment exists before the imports, skip the file.
(In reply to comment #28) > If comments between imports are important to the developer, the developer > should be able to turn off reorganization for those files. In the spirit of Bug > 311617, can something like //@organizeimports:off be implemented? If that > comment exists before the imports, skip the file. There are no plans to do this, but if someone signs up to implement this, then we would look at a good quality patch.
I haven't reviewed the whole patch, but I played around with it and the behavior looked good (just accumulates all comments inside the imports section at the end, in original order). Released the UI test to HEAD.
Released for 3.8M2.
Verified for 3.8M2 using build I20110912-0800