use of org.eclipse.jdt.core.dom.SwitchExpression in project eclipse.jdt.ls by eclipse.
the class LocalCorrectionsSubProcessor method getMissingEnumConstantCaseProposals.
public static void getMissingEnumConstantCaseProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
for (ChangeCorrectionProposal proposal : proposals) {
if (CorrectionMessages.LocalCorrectionsSubProcessor_add_missing_cases_description.equals(proposal.getName())) {
return;
}
}
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode instanceof Expression) {
StructuralPropertyDescriptor locationInParent = selectedNode.getLocationInParent();
ASTNode parent = selectedNode.getParent();
ITypeBinding binding;
List<Statement> statements;
if (locationInParent == SwitchStatement.EXPRESSION_PROPERTY) {
SwitchStatement statement = (SwitchStatement) parent;
binding = statement.getExpression().resolveTypeBinding();
statements = statement.statements();
} else if (locationInParent == SwitchExpression.EXPRESSION_PROPERTY) {
SwitchExpression switchExpression = (SwitchExpression) parent;
binding = switchExpression.getExpression().resolveTypeBinding();
statements = switchExpression.statements();
} else {
return;
}
if (binding == null || !binding.isEnum()) {
return;
}
ArrayList<String> missingEnumCases = new ArrayList<>();
boolean hasDefault = evaluateMissingSwitchCases(binding, statements, missingEnumCases);
if (missingEnumCases.size() == 0 && hasDefault) {
return;
}
createMissingCaseProposals(context, parent, missingEnumCases, proposals);
}
}
use of org.eclipse.jdt.core.dom.SwitchExpression in project eclipse.jdt.ls by eclipse.
the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.
private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
if (covering instanceof Block) {
List<Statement> statements = ((Block) covering).statements();
int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
if (startIndex == -1 || startIndex >= statements.size()) {
return false;
}
covering = statements.get(startIndex);
} else {
while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
covering = covering.getParent();
}
}
SwitchStatement switchStatement;
if (covering instanceof SwitchStatement) {
switchStatement = (SwitchStatement) covering;
} else {
return false;
}
SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
if (fix == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
// add correction proposal
try {
CompilationUnitChange change = fix.createChange(null);
ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
resultingCollections.add(proposal);
} catch (CoreException e) {
// continue
}
return true;
}
Aggregations