use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getConvertStringConcatenationProposals.
private static boolean getConvertStringConcatenationProposals(IInvocationContext context, Collection<ICommandAccess> resultingCollections) {
ASTNode node = context.getCoveringNode();
BodyDeclaration parentDecl = ASTResolving.findParentBodyDeclaration(node);
if (!(parentDecl instanceof MethodDeclaration || parentDecl instanceof Initializer))
return false;
AST ast = node.getAST();
//$NON-NLS-1$
ITypeBinding stringBinding = ast.resolveWellKnownType("java.lang.String");
if (node instanceof Expression && !(node instanceof InfixExpression)) {
node = node.getParent();
}
if (node instanceof VariableDeclarationFragment) {
node = ((VariableDeclarationFragment) node).getInitializer();
} else if (node instanceof Assignment) {
node = ((Assignment) node).getRightHandSide();
}
InfixExpression oldInfixExpression = null;
while (node instanceof InfixExpression) {
InfixExpression curr = (InfixExpression) node;
if (curr.resolveTypeBinding() == stringBinding && curr.getOperator() == InfixExpression.Operator.PLUS) {
// is a infix expression we can use
oldInfixExpression = curr;
} else {
break;
}
node = node.getParent();
}
if (oldInfixExpression == null)
return false;
if (resultingCollections == null) {
return true;
}
LinkedCorrectionProposal stringBufferProposal = getConvertToStringBufferProposal(context, ast, oldInfixExpression);
resultingCollections.add(stringBufferProposal);
ASTRewriteCorrectionProposal messageFormatProposal = getConvertToMessageFormatProposal(context, ast, oldInfixExpression);
if (messageFormatProposal != null)
resultingCollections.add(messageFormatProposal);
return true;
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getJoinVariableProposals.
private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
ASTNode parent = node.getParent();
VariableDeclarationFragment fragment = null;
boolean onFirstAccess = false;
if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
onFirstAccess = true;
SimpleName name = (SimpleName) node;
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
if (declaring instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) declaring;
} else {
return false;
}
} else if (parent instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) parent;
} else {
return false;
}
IVariableBinding binding = fragment.resolveBinding();
Expression initializer = fragment.getInitializer();
if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
return false;
}
if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
return false;
}
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
if (names.length <= 1 || names[0] != fragment.getName()) {
return false;
}
SimpleName firstAccess = names[1];
if (onFirstAccess) {
if (firstAccess != node) {
return false;
}
} else {
if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
return false;
}
}
Assignment assignment = (Assignment) firstAccess.getParent();
if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
return false;
}
ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
if (resultingCollections == null) {
return true;
}
AST ast = statement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
sourceRangeComputer.addTightSourceNode(assignParent);
rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION, image);
proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
if (onFirstAccess) {
// replace assignment with variable declaration
rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
} else {
// different scopes -> remove assignments, set variable initializer
if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
Block block = ast.newBlock();
rewrite.replace(assignParent, block, null);
} else {
rewrite.remove(assignParent, null);
}
}
proposal.setEndPosition(rewrite.track(fragment.getName()));
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.
the class QuickAssistProcessor method getInvertEqualsProposal.
private static boolean getInvertEqualsProposal(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
if (!(node instanceof MethodInvocation)) {
node = node.getParent();
if (!(node instanceof MethodInvocation)) {
return false;
}
}
MethodInvocation method = (MethodInvocation) node;
String identifier = method.getName().getIdentifier();
if (!"equals".equals(identifier) && !"equalsIgnoreCase".equals(identifier)) {
//$NON-NLS-1$ //$NON-NLS-2$
return false;
}
List<Expression> arguments = method.arguments();
if (arguments.size() != 1) {
//overloaded equals w/ more than 1 argument
return false;
}
Expression right = arguments.get(0);
ITypeBinding binding = right.resolveTypeBinding();
if (binding != null && !(binding.isClass() || binding.isInterface() || binding.isEnum())) {
//overloaded equals w/ non-class/interface argument or null
return false;
}
if (resultingCollections == null) {
return true;
}
Expression left = method.getExpression();
AST ast = method.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (left == null) {
// equals(x) -> x.equals(this)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(ast.newThisExpression());
replacement.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(method, replacement, null);
} else if (right instanceof ThisExpression) {
// x.equals(this) -> equals(x)
MethodInvocation replacement = ast.newMethodInvocation();
replacement.setName((SimpleName) rewrite.createCopyTarget(method.getName()));
replacement.arguments().add(rewrite.createCopyTarget(left));
rewrite.replace(method, replacement, null);
} else {
ASTNode leftExpression = left;
while (leftExpression instanceof ParenthesizedExpression) {
leftExpression = ((ParenthesizedExpression) left).getExpression();
}
rewrite.replace(right, rewrite.createCopyTarget(leftExpression), null);
if (right instanceof CastExpression || right instanceof Assignment || right instanceof ConditionalExpression || right instanceof InfixExpression) {
ParenthesizedExpression paren = ast.newParenthesizedExpression();
paren.setExpression((Expression) rewrite.createCopyTarget(right));
rewrite.replace(left, paren, null);
} else {
rewrite.replace(left, rewrite.createCopyTarget(right), null);
}
}
String label = CorrectionMessages.QuickAssistProcessor_invertequals_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_EQUALS, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project che by eclipse.
the class AdvancedQuickAssistProcessor method getInverseLocalVariableProposals.
private static boolean getInverseLocalVariableProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
final AST ast = covering.getAST();
// cursor should be placed on variable name
if (!(covering instanceof SimpleName)) {
return false;
}
SimpleName coveringName = (SimpleName) covering;
if (!coveringName.isDeclaration()) {
return false;
}
// prepare bindings
final IBinding variableBinding = coveringName.resolveBinding();
if (!(variableBinding instanceof IVariableBinding)) {
return false;
}
IVariableBinding binding = (IVariableBinding) variableBinding;
if (binding.isField()) {
return false;
}
// we operate only on boolean variable
if (!isBoolean(coveringName)) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
// find linked nodes
final MethodDeclaration method = ASTResolving.findParentMethodDeclaration(covering);
SimpleName[] linkedNodes = LinkedNodeFinder.findByBinding(method, variableBinding);
//
final ASTRewrite rewrite = ASTRewrite.create(ast);
// create proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseBooleanVariable;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
//$NON-NLS-1$
final String KEY_NAME = "name";
final LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_BOOLEAN_VARIABLE, image);
// prepare new variable identifier
final String oldIdentifier = coveringName.getIdentifier();
//$NON-NLS-1$
final String notString = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, "");
final String newIdentifier;
if (oldIdentifier.startsWith(notString)) {
int notLength = notString.length();
if (oldIdentifier.length() > notLength) {
newIdentifier = Character.toLowerCase(oldIdentifier.charAt(notLength)) + oldIdentifier.substring(notLength + 1);
} else {
newIdentifier = oldIdentifier;
}
} else {
newIdentifier = Messages.format(CorrectionMessages.AdvancedQuickAssistProcessor_negatedVariableName, Character.toUpperCase(oldIdentifier.charAt(0)) + oldIdentifier.substring(1));
}
//
proposal.addLinkedPositionProposal(KEY_NAME, newIdentifier, null);
proposal.addLinkedPositionProposal(KEY_NAME, oldIdentifier, null);
// iterate over linked nodes and replace variable references with negated reference
final HashSet<SimpleName> renamedNames = new HashSet<SimpleName>();
for (int i = 0; i < linkedNodes.length; i++) {
SimpleName name = linkedNodes[i];
if (renamedNames.contains(name)) {
continue;
}
// prepare new name with new identifier
SimpleName newName = ast.newSimpleName(newIdentifier);
proposal.addLinkedPosition(rewrite.track(newName), name == coveringName, KEY_NAME);
//
StructuralPropertyDescriptor location = name.getLocationInParent();
if (location == SingleVariableDeclaration.NAME_PROPERTY) {
// set new name
rewrite.replace(name, newName, null);
} else if (location == Assignment.LEFT_HAND_SIDE_PROPERTY) {
Assignment assignment = (Assignment) name.getParent();
Expression expression = assignment.getRightHandSide();
int exStart = expression.getStartPosition();
int exEnd = exStart + expression.getLength();
// collect all names that are used in assignments
HashSet<SimpleName> overlapNames = new HashSet<SimpleName>();
for (int j = 0; j < linkedNodes.length; j++) {
SimpleName name2 = linkedNodes[j];
if (name2 == null) {
continue;
}
int name2Start = name2.getStartPosition();
if (exStart <= name2Start && name2Start < exEnd) {
overlapNames.add(name2);
}
}
// prepare inverted expression
SimpleNameRenameProvider provider = new SimpleNameRenameProvider() {
public SimpleName getRenamed(SimpleName simpleName) {
if (simpleName.resolveBinding() == variableBinding) {
renamedNames.add(simpleName);
return ast.newSimpleName(newIdentifier);
}
return null;
}
};
Expression inversedExpression = getInversedExpression(rewrite, expression, provider);
// if any name was not renamed during expression inverting, we can not already rename it, so fail to create assist
for (Iterator<SimpleName> iter = overlapNames.iterator(); iter.hasNext(); ) {
Object o = iter.next();
if (!renamedNames.contains(o)) {
return false;
}
}
// check operator and replace if needed
Assignment.Operator operator = assignment.getOperator();
if (operator == Assignment.Operator.BIT_AND_ASSIGN) {
Assignment newAssignment = ast.newAssignment();
newAssignment.setLeftHandSide(newName);
newAssignment.setRightHandSide(inversedExpression);
newAssignment.setOperator(Assignment.Operator.BIT_OR_ASSIGN);
rewrite.replace(assignment, newAssignment, null);
} else if (operator == Assignment.Operator.BIT_OR_ASSIGN) {
Assignment newAssignment = ast.newAssignment();
newAssignment.setLeftHandSide(newName);
newAssignment.setRightHandSide(inversedExpression);
newAssignment.setOperator(Assignment.Operator.BIT_AND_ASSIGN);
rewrite.replace(assignment, newAssignment, null);
} else {
rewrite.replace(expression, inversedExpression, null);
// set new name
rewrite.replace(name, newName, null);
}
} else if (location == VariableDeclarationFragment.NAME_PROPERTY) {
// replace initializer for variable
VariableDeclarationFragment vdf = (VariableDeclarationFragment) name.getParent();
Expression expression = vdf.getInitializer();
if (expression != null) {
rewrite.replace(expression, getInversedExpression(rewrite, expression), null);
}
// set new name
rewrite.replace(name, newName, null);
} else if (name.getParent() instanceof PrefixExpression && ((PrefixExpression) name.getParent()).getOperator() == PrefixExpression.Operator.NOT) {
rewrite.replace(name.getParent(), newName, null);
} else {
PrefixExpression expression = ast.newPrefixExpression();
expression.setOperator(PrefixExpression.Operator.NOT);
expression.setOperand(newName);
rewrite.replace(name, expression, null);
}
}
// add correction proposal
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal in project flux by eclipse.
the class QuickAssistProcessor method getJoinVariableProposals.
private static boolean getJoinVariableProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
ASTNode parent = node.getParent();
VariableDeclarationFragment fragment = null;
boolean onFirstAccess = false;
if (node instanceof SimpleName && node.getLocationInParent() == Assignment.LEFT_HAND_SIDE_PROPERTY) {
onFirstAccess = true;
SimpleName name = (SimpleName) node;
IBinding binding = name.resolveBinding();
if (!(binding instanceof IVariableBinding)) {
return false;
}
ASTNode declaring = context.getASTRoot().findDeclaringNode(binding);
if (declaring instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) declaring;
} else {
return false;
}
} else if (parent instanceof VariableDeclarationFragment) {
fragment = (VariableDeclarationFragment) parent;
} else {
return false;
}
IVariableBinding binding = fragment.resolveBinding();
Expression initializer = fragment.getInitializer();
if ((initializer != null && initializer.getNodeType() != ASTNode.NULL_LITERAL) || binding == null || binding.isField()) {
return false;
}
if (!(fragment.getParent() instanceof VariableDeclarationStatement)) {
return false;
}
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
SimpleName[] names = LinkedNodeFinder.findByBinding(statement.getParent(), binding);
if (names.length <= 1 || names[0] != fragment.getName()) {
return false;
}
SimpleName firstAccess = names[1];
if (onFirstAccess) {
if (firstAccess != node) {
return false;
}
} else {
if (firstAccess.getLocationInParent() != Assignment.LEFT_HAND_SIDE_PROPERTY) {
return false;
}
}
Assignment assignment = (Assignment) firstAccess.getParent();
if (assignment.getLocationInParent() != ExpressionStatement.EXPRESSION_PROPERTY) {
return false;
}
ExpressionStatement assignParent = (ExpressionStatement) assignment.getParent();
if (resultingCollections == null) {
return true;
}
AST ast = statement.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// TightSourceRangeComputer sourceRangeComputer= new TightSourceRangeComputer();
// sourceRangeComputer.addTightSourceNode(assignParent);
// rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
String label = CorrectionMessages.QuickAssistProcessor_joindeclaration_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_VARIABLE_DECLARATION);
proposal.setCommandId(SPLIT_JOIN_VARIABLE_DECLARATION_ID);
Expression placeholder = (Expression) rewrite.createMoveTarget(assignment.getRightHandSide());
rewrite.set(fragment, VariableDeclarationFragment.INITIALIZER_PROPERTY, placeholder, null);
if (onFirstAccess) {
// replace assignment with variable declaration
rewrite.replace(assignParent, rewrite.createMoveTarget(statement), null);
} else {
// different scopes -> remove assignments, set variable initializer
if (ASTNodes.isControlStatementBody(assignParent.getLocationInParent())) {
Block block = ast.newBlock();
rewrite.replace(assignParent, block, null);
} else {
rewrite.remove(assignParent, null);
}
}
proposal.setEndPosition(rewrite.track(fragment.getName()));
resultingCollections.add(proposal);
return true;
}
Aggregations