Bug 287123 - [quick assist] Join variable declaration quick fix should be proposed for initialized variables
Summary: [quick assist] Join variable declaration quick fix should be proposed for ini...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.7 M4   Edit
Assignee: Deepak Azad CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 344569
  Show dependency tree
 
Reported: 2009-08-19 18:25 EDT by Willian Mitsuda CLA
Modified: 2011-05-03 09:45 EDT (History)
5 users (show)

See Also:


Attachments
proposed fix for the null initializer case (1.23 KB, patch)
2010-11-11 12:44 EST, Deepak Azad CLA
no flags Details | Diff
fix + tests (3.94 KB, patch)
2010-11-11 22:07 EST, Deepak Azad CLA
no flags Details | Diff
final fix (5.21 KB, patch)
2010-11-26 02:55 EST, Deepak Azad CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Willian Mitsuda CLA 2009-08-19 18:25:06 EDT
The "join variable declaration" quick fix is offered for code like this:

String something;
something = "blah";

But it is not for initialized variables, like this:

String something = null;
something = "blah";
Comment 1 Olivier Thomann CLA 2009-08-20 08:41:03 EDT
Move to JDT/UI
Comment 2 Markus Keller CLA 2009-08-20 09:28:56 EDT
In this case, the quick assist name should tell that the initializer gets removed, e.g. 'Join variable declaration, remove initializer'.

The 'Split/join variable declaration' command on the declaration should stay with 'Split' when the quick assist is invoked on the declaration (but the 'Join' quick assist should be offered as well).

If anyone is up for a patch:
The code is in QuickAssistProcessor.getJoinVariableProposals(..).
Comment 3 Deepak Azad CLA 2010-11-10 09:31:21 EST
(In reply to comment #2)
> In this case, the quick assist name should tell that the initializer gets
> removed, e.g. 'Join variable declaration, remove initializer'.

I agree with this for a non null initializer
>String something="initializer";
>something = "blah";

But is the 'remove initializer' message to the user really necessary when the initializer is 'null'?
>String something = null;
>something = "blah";

You initialize a variable to null to make sure that it is initialized and the compiler no longer complains about it. Also in this case there is no real loss of data. (If the initializer is non null then the user might want to preserve that and hence should be informed before removing it)
Comment 4 Markus Keller CLA 2010-11-11 08:11:45 EST
(In reply to comment #3)
Agreed.
Comment 5 Deepak Azad CLA 2010-11-11 12:44:21 EST
Created attachment 182924 [details]
proposed fix for the null initializer case

Patch does 2 things
- Offer 'Join variable declaration' quick assist when the initializer is null
- *Not* offer 'Split variable declaration' quick assist when the initializer is null.

It looks odd if you see both join and split assists together, and secondly splitting away null initializer is not useful. e.g. 
String something;
something = null;
Comment 6 Deepak Azad CLA 2010-11-11 22:07:05 EST
Created attachment 182946 [details]
fix + tests

added tests.
Comment 7 Deepak Azad CLA 2010-11-11 22:16:31 EST
(In reply to comment #5)
> - *Not* offer 'Split variable declaration' quick assist when the initializer is
> null.
> 
> It looks odd if you see both join and split assists together, and secondly
> splitting away null initializer is not useful. e.g. 
> String something;
> something = null;
Markus, you are ok with this bit?
Comment 8 Deepak Azad CLA 2010-11-22 08:28:55 EST
(In reply to comment #7)
> (In reply to comment #5)
> > - *Not* offer 'Split variable declaration' quick assist when the initializer is
> > null.
> > 
> > It looks odd if you see both join and split assists together, and secondly
> > splitting away null initializer is not useful. e.g. 
> > String something;
> > something = null;
> Markus, you are ok with this bit?
ping.
Comment 9 Markus Keller CLA 2010-11-25 14:30:49 EST
> - Offer 'Join variable declaration' quick assist when the initializer is null

That's good.

> - *Not* offer 'Split variable declaration' quick assist when the initializer is
> null.
> 
> It looks odd if you see both join and split assists together, and secondly
> splitting away null initializer is not useful. e.g. 
> String something;
> something = null;

It's not useful in itself, but 'Split variable declaration' is always just the first step of a larger code change. There are cases where you want to split off a null initializer, e.g. here:

    private void foo() {
        String something= null;
        if (equals(null)) {
            System.out.println("b'");
        } else if (hashCode() == 1) {
            something= "blah";
            System.out.println("a'");
        } else {
            System.out.println("a'");
            System.out.println("something");
        }
        System.out.println(something);
    }

If I want to change this code to be sure that 'something' is initialized in all branches, I can split the declaration and move it into the first block (with a simple Alt+Down).

Please add the 'Split variable declaration' quick assist back in this case. I know that this will introduce a conflict, since the two quick assist use the same commandID, so their key bindings will be duplicated. But this can easily be solved in the second proposal by checking whether resultingCollections already contains a proposal with that commandID.

If you want the 'join' proposal to win the race for the commandID, you can also change the order in QuickAssistProcessor#getAssists(..).

OK to release with these changes.
Comment 10 Deepak Azad CLA 2010-11-26 02:55:30 EST
Created attachment 183903 [details]
final fix

(In reply to comment #9)
> It's not useful in itself, but 'Split variable declaration' is always just the
> first step of a larger code change. 
Fair enough.

> If you want the 'join' proposal to win the race for the commandID, you can also
> change the order in QuickAssistProcessor#getAssists(..).
Done this as well.
Comment 11 Deepak Azad CLA 2010-11-26 02:56:34 EST
Fixed in HEAD.
Comment 12 Dani Megert CLA 2011-05-03 09:44:58 EDT
Support is missing for primitives. Filed bug 344569.
Comment 13 Dani Megert CLA 2011-05-03 09:45:13 EDT
Verified in 3.7 M7.