Bug 71244 New Quick Assist's [quick assist]
I've added several new useful (at least for me) quick assist's in class QuickAssistProcessor (from 3.0 release). Here is list of them: 1. Split condition. For example you have following code: if (condition_1 && condition_2) { do_something2(); } Now you decide that you need also execute do_something3() when condition_1 && condition_3. I.e. you want following code: if (condition_1) { if (condition_2) do_something2(); if (condition_3) do_something3(); } Currently you will need to copy condition_2 by hands. I don't know how all other, but I am very lazy. With new quick assist you can position caret on condition (for example on &&) and transform initial code in: if (condition_1) { if (condition_2) do_something2(); } 2. Join 'if' statements. This is reverse operation to "split condition". You can point on inner or outer "if" statement and ask for joining. Depending on context, quick assist will suggest to join outer "if" to inner or inner to outer, or both, if possible. Join can be used only if there are no "else" parts in "if" statements. 3. Add paranoidal parenthesis for conditions. I don't like to look on conditions and think "And what is order of executing these conditions?", so usually add parenthesis around each expression like: "collection.size() == 1" or (object == null), etc. However sometimes I have to change someone else code and would like to change source to make it more readable for me. So, I've added quick assist for adding parenthesis in such cases. For example this: list.size() == 3 && list instanceof List && c will be converted to: (list.size() == 3) && (list instanceof List) && c Just select block of code and invoke quick asisst, it will change all conditions in selected code. 4. Remove extra parenthesis. One more reverse operation. If you don't like extra parenthesis in your code, you can remove all of them and keep them only where required. For example this: (list.size() == 2) && (a || b) will be converted to: list.size() == 2 && (a || b) As you can see, it keeps parenthesis where needed. It uses precedence table for operators, so knows, where to keep parenthesis. 5. Inverse condition. Select condition and inverse it, for example when you see that there are too many "not" operators or want to exchange "then" and "else" branches in "if" statement (BTW, may be such quick assist also required?...). For example, this: a || !b && !c will be converted to: !a && (b || c) BTW, I often use what I call "fail fast" checking. I.e. when you do chain of checks, each next can be executed only if previous were successful, instead of writing deep chain of "if" statements: prepare_1; if (condition_1) { prepare_2; if (condition_2) { prepare_3; if (condition_3) { ... } } } I prefer to have prepare_1; if (!condition_1) return false; // or may be 'continue' prepare_2; if (!condition_2) return false; prepare_3; if (!condition_3) return false; ... So, condition inversing could be useful to implement assists for changing from one style of chain checking to another... I would like to have these quick assists in Eclipse, so would like to contribute them to Eclipse.
org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/QuickAssistProcessor.java
if (!(firstAccess.getParent() instanceof Assignment)) { 
			return false;
		}
		Assignment assignment= (Assignment) firstAccess.getParent();
		if (assignment.getLeftHandSide() != firstAccess) {
			return false;
		}
		
		ASTNode assignParent= assignment.getParent();
		if (!(assignParent instanceof ExpressionStatement || assignParent instanceof ForStatement && ((ForStatement) assignParent).initializers().contains(assignment))) {
			return false;
		}
				
		if (resultingCollections == null) {
			return true;
		}
		AST ast= statement.getAST();
		ASTRewrite rewrite= ASTRewrite.create(ast);
		
		String label= CorrectionMessages.getString("QuickAssistProcessor.joindeclaration.description"); 
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, 1, image);
		Expression placeholder= (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
		rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
		
		if (assignParent instanceof ExpressionStatement) {
			int statementParent= assignParent.getParent().getNodeType();
			if (statementParent == ASTNode.IF_STATEMENT || statementParent == ASTNode.WHILE_STATEMENT || statementParent == ASTNode.DO_STATEMENT
				|| statementParent == ASTNode.FOR_STATEMENT) {