View | Details | Raw Unified | Return to bug 77632
Collapse All | Expand All

(-)a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/AdvancedQuickAssistTest.java (+134 lines)
Lines 10-15 Link Here
10
 *          (reports 71244 & 74746: New Quick Assist's [quick assist])
10
 *          (reports 71244 & 74746: New Quick Assist's [quick assist])
11
 *   Benjamin Muskalla (buskalla@innoopract.com) - 104021: [quick fix] Introduce
11
 *   Benjamin Muskalla (buskalla@innoopract.com) - 104021: [quick fix] Introduce
12
 *   		new local with casted type applied more than once
12
 *   		new local with casted type applied more than once
13
 *   Billy Huang (billyhuang31@gmail.com) - [quick assist] concatenate/merge
14
 *   		string literals - https://bugs.eclipse.org/bugs/show_bug.cgi?id=77632
13
 *******************************************************************************/
15
 *******************************************************************************/
14
package org.eclipse.jdt.ui.tests.quickfix;
16
package org.eclipse.jdt.ui.tests.quickfix;
15
17
Lines 5502-5505 Link Here
5502
		assertProposalDoesNotExist(proposals, "Pick out selected part of String");
5504
		assertProposalDoesNotExist(proposals, "Pick out selected part of String");
5503
5505
5504
	}
5506
	}
5507
	
5508
	public void testCombineStringsProposals1() throws Exception {
5509
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5510
		StringBuffer buf= new StringBuffer();
5511
		buf.append("package test1;\n");
5512
		buf.append("public class E {\n");
5513
		buf.append("    public void foo() {\n");
5514
		buf.append("        String string = \"Hello\" + \" World\";\n");
5515
		buf.append("    }\n");
5516
		buf.append("}\n");
5517
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
5518
5519
		int offset= buf.toString().indexOf("\"Hello\"");
5520
		int length= "\"Hello\" + \"World\"".length();
5521
		AssistContext context= getCorrectionContext(cu, offset, length);
5522
		List proposals= collectAssists(context, false);
5523
5524
		assertCorrectLabels(proposals);
5525
		
5526
		buf= new StringBuffer();
5527
		buf.append("package test1;\n");
5528
		buf.append("public class E {\n");
5529
		buf.append("    public void foo() {\n");
5530
		buf.append("        String string = \"Hello World\";\n");
5531
		buf.append("    }\n");
5532
		buf.append("}\n");
5533
		String expected= buf.toString();
5534
5535
		assertExpectedExistInProposals(proposals, new String[] { expected });
5536
5537
	}
5538
	
5539
	public void testCombineStringsProposals2() throws Exception {
5540
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5541
		StringBuffer buf= new StringBuffer();
5542
		buf.append("package test1;\n");
5543
		buf.append("public class E {\n");
5544
		buf.append("    public void foo() {\n");
5545
		buf.append("        String string = \"Hello\" + \" \" + \"World\";\n");
5546
		buf.append("    }\n");
5547
		buf.append("}\n");
5548
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
5549
5550
		int offset= buf.toString().indexOf("\"Hello\"");
5551
		int length= "\"Hello\" + \" \" + \"World\"".length();
5552
		AssistContext context= getCorrectionContext(cu, offset, length);
5553
		List proposals= collectAssists(context, false);
5554
5555
		assertCorrectLabels(proposals);
5556
		
5557
		buf= new StringBuffer();
5558
		buf.append("package test1;\n");
5559
		buf.append("public class E {\n");
5560
		buf.append("    public void foo() {\n");
5561
		buf.append("        String string = \"Hello World\";\n");
5562
		buf.append("    }\n");
5563
		buf.append("}\n");
5564
		String expected= buf.toString();
5565
5566
		assertExpectedExistInProposals(proposals, new String[] { expected });
5567
5568
	}
5569
	
5570
	public void testCombineStringsProposals3() throws Exception {
5571
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5572
		StringBuffer buf= new StringBuffer();
5573
		buf.append("package test1;\n");
5574
		buf.append("public class E {\n");
5575
		buf.append("    public void foo() {\n");
5576
		buf.append("        String string = \"Hello World\";\n");
5577
		buf.append("    }\n");
5578
		buf.append("}\n");
5579
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
5580
5581
		int offset= buf.toString().indexOf("\"Hello World\"");
5582
		AssistContext context= getCorrectionContext(cu, offset, 0);
5583
		List proposals= collectAssists(context, false);
5584
5585
		assertProposalDoesNotExist(proposals, "Combine selected strings");
5586
5587
	}
5588
	
5589
	public void testCombineStringsProposals4() throws Exception {
5590
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5591
		StringBuffer buf= new StringBuffer();
5592
		buf.append("package test1;\n");
5593
		buf.append("public class E {\n");
5594
		buf.append("    public void foo() {\n");
5595
		buf.append("        System.out.println(\"Hello\" + \" \" + \"World\");\n");
5596
		buf.append("    }\n");
5597
		buf.append("}\n");
5598
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
5599
5600
		int offset= buf.toString().indexOf("\"Hello\"");
5601
		int length= "\"Hello\" + \" \" + \"World\"".length();
5602
		AssistContext context= getCorrectionContext(cu, offset, length);
5603
		List proposals= collectAssists(context, false);
5604
5605
		assertCorrectLabels(proposals);
5606
		
5607
		buf= new StringBuffer();
5608
		buf.append("package test1;\n");
5609
		buf.append("public class E {\n");
5610
		buf.append("    public void foo() {\n");
5611
		buf.append("        System.out.println(\"Hello World\");\n");
5612
		buf.append("    }\n");
5613
		buf.append("}\n");
5614
		String expected= buf.toString();
5615
5616
		assertExpectedExistInProposals(proposals, new String[] { expected });
5617
5618
	}
5619
	
5620
	public void testCombineStringsProposals5() throws Exception {
5621
		IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
5622
		StringBuffer buf= new StringBuffer();
5623
		buf.append("package test1;\n");
5624
		buf.append("public class E {\n");
5625
		buf.append("    public void foo() {\n");
5626
		buf.append("        String string = \"Hello\" + \"World\" + 2;\n");
5627
		buf.append("    }\n");
5628
		buf.append("}\n");
5629
		ICompilationUnit cu= pack1.createCompilationUnit("E.java", buf.toString(), false, null);
5630
5631
		int offset= buf.toString().indexOf("\"Hello\" + \"World\"");
5632
		int length= "\"Hello\" + \"World\"".length();
5633
		AssistContext context= getCorrectionContext(cu, offset, length);
5634
		List proposals= collectAssists(context, false);
5635
5636
		assertProposalDoesNotExist(proposals, "Combine selected strings");
5637
5638
	}
5505
}
5639
}
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/AdvancedQuickAssistProcessor.java (+63 lines)
Lines 9-14 Link Here
9
 *   Konstantin Scheglov (scheglov_ke@nlmk.ru) - initial API and implementation
9
 *   Konstantin Scheglov (scheglov_ke@nlmk.ru) - initial API and implementation
10
 *          (reports 71244 & 74746: New Quick Assist's [quick assist])
10
 *          (reports 71244 & 74746: New Quick Assist's [quick assist])
11
 *   IBM Corporation - implementation
11
 *   IBM Corporation - implementation
12
 *   Billy Huang (billyhuang31@gmail.com) - [quick assist] concatenate/merge
13
 *   		string literals - https://bugs.eclipse.org/bugs/show_bug.cgi?id=77632
12
 *******************************************************************************/
14
 *******************************************************************************/
13
package org.eclipse.jdt.internal.ui.text.correction;
15
package org.eclipse.jdt.internal.ui.text.correction;
14
16
Lines 144-149 Link Here
144
					|| getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, null)
146
					|| getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, null)
145
					|| getExchangeOperandsProposals(context, coveringNode, null)
147
					|| getExchangeOperandsProposals(context, coveringNode, null)
146
					|| getCastAndAssignIfStatementProposals(context, coveringNode, null)
148
					|| getCastAndAssignIfStatementProposals(context, coveringNode, null)
149
					|| getCombineStringProposals(context, coveringNode, null)
147
					|| getPickOutStringProposals(context, coveringNode, null)
150
					|| getPickOutStringProposals(context, coveringNode, null)
148
					|| getReplaceIfElseWithConditionalProposals(context, coveringNode, null)
151
					|| getReplaceIfElseWithConditionalProposals(context, coveringNode, null)
149
					|| getReplaceConditionalWithIfElseProposals(context, coveringNode, null)
152
					|| getReplaceConditionalWithIfElseProposals(context, coveringNode, null)
Lines 187-192 Link Here
187
				getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, resultingCollections);
190
				getExchangeInnerAndOuterIfConditionsProposals(context, coveringNode, resultingCollections);
188
				getExchangeOperandsProposals(context, coveringNode, resultingCollections);
191
				getExchangeOperandsProposals(context, coveringNode, resultingCollections);
189
				getCastAndAssignIfStatementProposals(context, coveringNode, resultingCollections);
192
				getCastAndAssignIfStatementProposals(context, coveringNode, resultingCollections);
193
				getCombineStringProposals(context, coveringNode, resultingCollections);
190
				getPickOutStringProposals(context, coveringNode, resultingCollections);
194
				getPickOutStringProposals(context, coveringNode, resultingCollections);
191
				getReplaceIfElseWithConditionalProposals(context, coveringNode, resultingCollections);
195
				getReplaceIfElseWithConditionalProposals(context, coveringNode, resultingCollections);
192
				getInverseLocalVariableProposals(context, coveringNode, resultingCollections);
196
				getInverseLocalVariableProposals(context, coveringNode, resultingCollections);
Lines 1527-1532 Link Here
1527
		return StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), binding, null, excluded);
1531
		return StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), binding, null, excluded);
1528
	}
1532
	}
1529
1533
1534
	private static boolean getCombineStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
1535
		// we work with InfixExpressions
1536
		if(!(node instanceof InfixExpression)) {
1537
			return false;
1538
		}
1539
		
1540
		InfixExpression infixExpression= (InfixExpression) node;
1541
		
1542
		// only + is valid for combining strings
1543
		if(!(infixExpression.getOperator().equals(InfixExpression.Operator.PLUS))) {
1544
			return false;
1545
		}
1546
		
1547
		// all expressions must be strings
1548
		Expression leftOperand= infixExpression.getLeftOperand();
1549
		Expression rightOperand= infixExpression.getRightOperand();
1550
		if(!(leftOperand instanceof StringLiteral && rightOperand instanceof StringLiteral)) {
1551
			return false;
1552
		}
1553
		
1554
		StringLiteral leftString= (StringLiteral) leftOperand;
1555
		StringLiteral rightString= (StringLiteral) rightOperand;
1556
		
1557
		if (resultingCollections == null) {
1558
			return true;
1559
		}
1560
		
1561
		// begin building combined string
1562
		StringBuilder stringBuilder= new StringBuilder(leftString.getLiteralValue());
1563
		stringBuilder.append(rightString.getLiteralValue());
1564
		
1565
		// append extended string literals
1566
		for (Object operand : infixExpression.extendedOperands()) {
1567
			if(!(operand instanceof StringLiteral)) {
1568
				return false;
1569
			} else {
1570
				StringLiteral stringLiteral= (StringLiteral) operand;
1571
				stringBuilder.append(stringLiteral.getLiteralValue());
1572
			}
1573
		}
1574
		
1575
		// prepare new string literal
1576
		AST ast= node.getAST();
1577
		StringLiteral combinedStringLiteral= ast.newStringLiteral();
1578
		
1579
		combinedStringLiteral.setLiteralValue(stringBuilder.toString());
1580
		
1581
		ASTRewrite rewrite= ASTRewrite.create(ast);
1582
		
1583
		rewrite.replace(infixExpression, combinedStringLiteral, null);
1584
		
1585
		// add correction proposal
1586
		String label= CorrectionMessages.AdvancedQuickAssistProcessor_combineSelectedStrings;
1587
		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
1588
		LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_STRINGS, image);
1589
		resultingCollections.add(proposal);
1590
		return true;
1591
	}
1592
	
1530
	private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
1593
	private static boolean getPickOutStringProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
1531
		// we work with String's
1594
		// we work with String's
1532
		if (!(node instanceof StringLiteral)) {
1595
		if (!(node instanceof StringLiteral)) {
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java (+1 lines)
Lines 294-299 Link Here
294
	public static String TypeChangeCompletionProposal_method_name;
294
	public static String TypeChangeCompletionProposal_method_name;
295
	public static String ImplementInterfaceProposal_name;
295
	public static String ImplementInterfaceProposal_name;
296
	public static String AddUnimplementedMethodsOperation_AddMissingMethod_group;
296
	public static String AddUnimplementedMethodsOperation_AddMissingMethod_group;
297
	public static String AdvancedQuickAssistProcessor_combineSelectedStrings;
297
	public static String AdvancedQuickAssistProcessor_convertToIfElse_description;
298
	public static String AdvancedQuickAssistProcessor_convertToIfElse_description;
298
	public static String AdvancedQuickAssistProcessor_inverseIf_description;
299
	public static String AdvancedQuickAssistProcessor_inverseIf_description;
299
	public static String AdvancedQuickAssistProcessor_inverseBooleanVariable;
300
	public static String AdvancedQuickAssistProcessor_inverseBooleanVariable;
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties (+1 lines)
Lines 374-379 Link Here
374
ImplementInterfaceProposal_name=Let ''{0}'' implement ''{1}''
374
ImplementInterfaceProposal_name=Let ''{0}'' implement ''{1}''
375
375
376
AddUnimplementedMethodsOperation_AddMissingMethod_group=Add missing method
376
AddUnimplementedMethodsOperation_AddMissingMethod_group=Add missing method
377
AdvancedQuickAssistProcessor_combineSelectedStrings=Combine selected strings
377
AdvancedQuickAssistProcessor_convertToIfElse_description=Convert to 'if-else'
378
AdvancedQuickAssistProcessor_convertToIfElse_description=Convert to 'if-else'
378
AdvancedQuickAssistProcessor_inverseIf_description=Invert 'if' statement
379
AdvancedQuickAssistProcessor_inverseIf_description=Invert 'if' statement
379
AdvancedQuickAssistProcessor_inverseBooleanVariable=Invert local variable
380
AdvancedQuickAssistProcessor_inverseBooleanVariable=Invert local variable
(-)a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/IProposalRelevance.java (+1 lines)
Lines 234-239 Link Here
234
	public static final int CONVERT_IF_ELSE_TO_SWITCH= 1;
234
	public static final int CONVERT_IF_ELSE_TO_SWITCH= 1;
235
	public static final int DOCUMENT_UNUSED_ITEM= 1;
235
	public static final int DOCUMENT_UNUSED_ITEM= 1;
236
	public static final int PICK_SELECTED_STRING= 1;
236
	public static final int PICK_SELECTED_STRING= 1;
237
	public static final int COMBINE_STRINGS= 1;
237
	public static final int INVERSE_BOOLEAN_VARIABLE= 1;
238
	public static final int INVERSE_BOOLEAN_VARIABLE= 1;
238
	public static final int REMOVE_UNUSED_ALLOCATED_OBJECT= 1;
239
	public static final int REMOVE_UNUSED_ALLOCATED_OBJECT= 1;
239
	public static final int UNWRAP_STATEMENTS= 1;
240
	public static final int UNWRAP_STATEMENTS= 1;

Return to bug 77632