Bug 564692 - Auto import the left type when importing the right type in an assignment
Summary: Auto import the left type when importing the right type in an assignment
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.17   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-06-26 13:04 EDT by Pankaj Kumar CLA
Modified: 2020-07-02 14:48 EDT (History)
6 users (show)

See Also:


Attachments
Short video to show the issue. (194.85 KB, image/gif)
2020-06-26 13:04 EDT, Pankaj Kumar CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pankaj Kumar CLA 2020-06-26 13:04:59 EDT
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.
Comment 1 Roland Grunberg CLA 2020-06-26 17:03:25 EDT
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.
Comment 2 Noopur Gupta CLA 2020-06-29 07:26:06 EDT
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.
Comment 3 Mickael Istria CLA 2020-06-29 07:30:31 EDT
(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.
Comment 4 Noopur Gupta CLA 2020-06-29 09:25:07 EDT
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.
Comment 5 Karsten Thoms CLA 2020-07-01 03:13:35 EDT
I like this proposal. Could be indeed a useful extension. The logic in comment#4 sounds correct to me.
Comment 6 Karsten Thoms CLA 2020-07-01 04:21:56 EDT
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
Comment 7 Karsten Thoms CLA 2020-07-02 02:54:24 EDT
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.
Comment 8 Roland Grunberg CLA 2020-07-02 14:48:38 EDT
(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.