use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.
public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
while (parent instanceof ExpressionStatement) {
selectedNode = parent;
parent = selectedNode.getParent();
}
if (parent instanceof WhileStatement) {
addRemoveIncludingConditionProposal(context, parent, null, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
Statement elseStatement = ((IfStatement) parent).getElseStatement();
addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
Statement thenStatement = ((IfStatement) parent).getThenStatement();
addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
} else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
Statement body = ((ForStatement) parent).getBody();
addRemoveIncludingConditionProposal(context, parent, body, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
} else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
// also offer split && / || condition proposals:
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
Expression replacement = leftOperand;
while (replacement instanceof ParenthesizedExpression) {
replacement = ((ParenthesizedExpression) replacement).getExpression();
}
Expression toReplace = infixExpression;
while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = (Expression) toReplace.getParent();
}
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (leftOperand instanceof ParenthesizedExpression) {
replacement = (Expression) replacement.getParent();
} else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = ((ParenthesizedExpression) toReplace).getExpression();
}
}
rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
addRemoveProposal(context, rewrite, label, proposals);
InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
assistContext.setASTRoot(root);
AdvancedQuickAssistProcessor.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
AdvancedQuickAssistProcessor.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
} else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
// remove all statements following the unreachable:
List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
int idx = statements.indexOf(selectedNode);
ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
if (idx > 0) {
Object prevStatement = statements.get(idx - 1);
if (prevStatement instanceof IfStatement) {
IfStatement ifStatement = (IfStatement) prevStatement;
if (ifStatement.getElseStatement() == null) {
// remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
}
}
}
for (int i = idx; i < statements.size(); i++) {
ASTNode statement = statements.get(i);
if (statement instanceof SwitchCase) {
// stop at case *: and default:
break;
}
rewrite.remove(statement, null);
}
addRemoveProposal(context, rewrite, label, proposals);
} else {
// no special case, just remove the node:
addRemoveProposal(context, selectedNode, proposals);
}
}
use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method getUnreachableCodeProposals.
public static void getUnreachableCodeProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
CompilationUnit root = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(root);
if (selectedNode == null) {
return;
}
ASTNode parent = selectedNode.getParent();
while (parent instanceof ExpressionStatement) {
selectedNode = parent;
parent = selectedNode.getParent();
}
if (parent instanceof WhileStatement) {
addRemoveIncludingConditionProposal(context, parent, null, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.THEN_STATEMENT_PROPERTY) {
Statement elseStatement = ((IfStatement) parent).getElseStatement();
addRemoveIncludingConditionProposal(context, parent, elseStatement, proposals);
} else if (selectedNode.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
Statement thenStatement = ((IfStatement) parent).getThenStatement();
addRemoveIncludingConditionProposal(context, parent, thenStatement, proposals);
} else if (selectedNode.getLocationInParent() == ForStatement.BODY_PROPERTY) {
Statement body = ((ForStatement) parent).getBody();
addRemoveIncludingConditionProposal(context, parent, body, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.THEN_EXPRESSION_PROPERTY) {
Expression elseExpression = ((ConditionalExpression) parent).getElseExpression();
addRemoveIncludingConditionProposal(context, parent, elseExpression, proposals);
} else if (selectedNode.getLocationInParent() == ConditionalExpression.ELSE_EXPRESSION_PROPERTY) {
Expression thenExpression = ((ConditionalExpression) parent).getThenExpression();
addRemoveIncludingConditionProposal(context, parent, thenExpression, proposals);
} else if (selectedNode.getLocationInParent() == InfixExpression.RIGHT_OPERAND_PROPERTY) {
// also offer split && / || condition proposals:
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
ASTRewrite rewrite = ASTRewrite.create(parent.getAST());
Expression replacement = leftOperand;
while (replacement instanceof ParenthesizedExpression) {
replacement = ((ParenthesizedExpression) replacement).getExpression();
}
Expression toReplace = infixExpression;
while (toReplace.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = (Expression) toReplace.getParent();
}
if (NecessaryParenthesesChecker.needsParentheses(replacement, toReplace.getParent(), toReplace.getLocationInParent())) {
if (leftOperand instanceof ParenthesizedExpression) {
replacement = (Expression) replacement.getParent();
} else if (infixExpression.getLocationInParent() == ParenthesizedExpression.EXPRESSION_PROPERTY) {
toReplace = ((ParenthesizedExpression) toReplace).getExpression();
}
}
rewrite.replace(toReplace, rewrite.createMoveTarget(replacement), null);
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
addRemoveProposal(context, rewrite, label, proposals);
InnovationContext assistContext = new InnovationContext(context.getCompilationUnit(), infixExpression.getRightOperand().getStartPosition() - 1, 0);
assistContext.setASTRoot(root);
InvertBooleanUtility.getSplitAndConditionProposals(assistContext, infixExpression, proposals);
InvertBooleanUtility.getSplitOrConditionProposals(assistContext, infixExpression, proposals);
} else if (selectedNode instanceof Statement && selectedNode.getLocationInParent().isChildListProperty()) {
// remove all statements following the unreachable:
List<Statement> statements = ASTNodes.<Statement>getChildListProperty(selectedNode.getParent(), (ChildListPropertyDescriptor) selectedNode.getLocationInParent());
int idx = statements.indexOf(selectedNode);
ASTRewrite rewrite = ASTRewrite.create(selectedNode.getAST());
String label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_description;
if (idx > 0) {
Object prevStatement = statements.get(idx - 1);
if (prevStatement instanceof IfStatement) {
IfStatement ifStatement = (IfStatement) prevStatement;
if (ifStatement.getElseStatement() == null) {
// remove if (true), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=261519
rewrite.replace(ifStatement, rewrite.createMoveTarget(ifStatement.getThenStatement()), null);
label = CorrectionMessages.LocalCorrectionsSubProcessor_removeunreachablecode_including_condition_description;
}
}
}
for (int i = idx; i < statements.size(); i++) {
ASTNode statement = statements.get(i);
if (statement instanceof SwitchCase) {
// stop at case *: and default:
break;
}
rewrite.remove(statement, null);
}
addRemoveProposal(context, rewrite, label, proposals);
} else {
// no special case, just remove the node:
addRemoveProposal(context, selectedNode, proposals);
}
}
use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.
the class OrganizeImportsCommand method organizeImportsInCompilationUnit.
public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
try {
InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", CodeActionKind.SourceOrganizeImports, unit, null, IProposalRelevance.ORGANIZE_IMPORTS) {
@Override
protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
CompilationUnit astRoot = context.getASTRoot();
OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
TextEdit edit = op.createTextEdit(null);
TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit);
if (staticEdit.getChildrenSize() > 0) {
editRoot.addChild(staticEdit);
}
}
};
addWorkspaceEdit(unit, proposal, rootEdit);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem organize imports ", e);
}
}
use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.
the class MoveHandler method getSelectedMethodDeclaration.
private static MethodDeclaration getSelectedMethodDeclaration(ICompilationUnit unit, CodeActionParams params) {
int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
InnovationContext context = new InnovationContext(unit, start, end - start);
context.setASTRoot(CodeActionHandler.getASTRoot(unit));
ASTNode node = context.getCoveredNode();
if (node == null) {
node = context.getCoveringNode();
}
while (node != null && !(node instanceof BodyDeclaration)) {
node = node.getParent();
}
if (node != null && node instanceof MethodDeclaration) {
return (MethodDeclaration) node;
}
return null;
}
use of org.eclipse.jdt.ls.core.internal.corrections.InnovationContext in project eclipse.jdt.ls by eclipse.
the class MoveHandler method getSelectedTypeDeclaration.
private static AbstractTypeDeclaration getSelectedTypeDeclaration(ICompilationUnit unit, CodeActionParams params) {
int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
InnovationContext context = new InnovationContext(unit, start, end - start);
context.setASTRoot(CodeActionHandler.getASTRoot(unit));
ASTNode node = context.getCoveredNode();
if (node == null) {
node = context.getCoveringNode();
}
while (node != null && !(node instanceof AbstractTypeDeclaration)) {
node = node.getParent();
}
return (AbstractTypeDeclaration) node;
}
Aggregations