Community
Participate
Working Groups
Created attachment 283425 [details] Short video to show the issue. Please check the attached GIF to clearly understand the issue. Twitter: https://twitter.com/JournalDev/status/1276378303272632322 It's a simple enhancement to save our time.
I think the capability to do this is partly there. Though I wonder if it's possible to do some on-demand organize imports based on the statement that was changed and not based on the editor being dirty. As for workarounds : 1) CTRL + SHIFT + O (organize imports) would work. 2) From the workspace preferences, enable autosave for dirty editors (General -> Editors -> AutoSave) and set it to some small amount (eg. 2 seconds). Enable "Perform the selected actions on save" (Java -> Editor -> Save Actions), and ensure that "Organize Imports" is also enabled.
Based on the GIF, List is not auto imported as it was just typed in the editor without using any IDE feature. The same will be true for ArrayList also if you don't invoke content assist as can be seen in the recording. If the content assist is invoked while typing List, then you will get the options to choose the import or it will get auto imported on pressing Ctrl+Space if you have set your Type Filters preferences to ignore unwanted types. Please let me know if I missed something in the bug request and provide steps to reproduce your issue.
(In reply to Noopur Gupta from comment #2) > Please let me know if I missed something in the bug request and provide > steps to reproduce your issue. As far as I understood, the request is more that when importing ArrayList as part of an assignment operation, the IDE understands that it's an assignment and "cascades" the auto-import to the LValue type, which in that case, given the context, is most probably a java.util.List and not a swing List. ie completion at that point should add imports for java.util.ArrayList and java.util.List.
OK, so the request is to add the import for the LHS also while adding it for the RHS if there is no ambiguity. The import for RHS can be added via content assist or quick fix. These should be extended to check if there's a missing import on the LHS in that statement and do the needful. The LHS import should take into account the Type Filters preferences and also the type hierarchy of the RHS type.
I like this proposal. Could be indeed a useful extension. The logic in comment#4 sounds correct to me.
ImportRewrite seems to be the place to search for. Here is a stack that I hit when applying the proposal. ------------------------------------------------ Thread [main] (Suspended (breakpoint at line 104 in CodeStyleConfiguration)) CodeStyleConfiguration.configureImportRewrite(ImportRewrite) line: 104 CodeStyleConfiguration.createImportRewrite(CompilationUnit, boolean) line: 99 StubUtility.createImportRewrite(CompilationUnit, boolean) line: 1574 LazyGenericTypeProposal(LazyJavaTypeCompletionProposal).createImportRewrite() line: 177 LazyGenericTypeProposal(LazyJavaTypeCompletionProposal).computeReplacementString() line: 139 LazyGenericTypeProposal(LazyJavaCompletionProposal).getReplacementString() line: 335 LazyGenericTypeProposal.apply(IDocument, char, int) line: 236 LazyGenericTypeProposal(AbstractJavaCompletionProposal).apply(IDocument) line: 342 JavaMethodCompletionProposal(AbstractJavaCompletionProposal).apply(IDocument, char, int) line: 355 JavaMethodCompletionProposal.apply(IDocument, char, int) line: 62 JavaMethodCompletionProposal(AbstractJavaCompletionProposal).apply(ITextViewer, char, int, int) line: 516 JavaMethodCompletionProposal(LazyJavaCompletionProposal).apply(ITextViewer, char, int, int) line: 446 AsyncCompletionProposalPopup(CompletionProposalPopup).insertProposal(ICompletionProposal, char, int, int) line: 995 AsyncCompletionProposalPopup(CompletionProposalPopup).insertSelectedProposalWithMask(int) line: 944 AsyncCompletionProposalPopup(CompletionProposalPopup).verifyKey(VerifyEvent) line: 1369 ContentAssistant$InternalListener.verifyKey(VerifyEvent) line: 809 TextViewer$VerifyKeyListenersManager.verifyKey(VerifyEvent) line: 481
I had a look as this is interesting to me, but did not found a way how this good be hooked into there. This is beyond my knowledge ATM. Would be great when someone could have a look and may give some ideas, or better try to work on this.
(In reply to Karsten Thoms from comment #7) > I had a look as this is interesting to me, but did not found a way how this > good be hooked into there. This is beyond my knowledge ATM. Would be great > when someone could have a look and may give some ideas, or better try to > work on this. I had a look inside LazyJavaTypeCompletionProposal#computeReplacementString(..), where 'fImportRewrite' is created near the bottom. From there one can use NodeFinder.perform(..) along with the fInvocationContext.getInvocationOffset() and getASTRoot(ICompilationUnit) to get some ASTNode around the invocation, and travel up the parent until a VariableDeclarationStatement node is reached. That should contain the type to add to the imports. I wouldn't mind working on this but figured I'd just add what I found so far in case someone wants to go further.