Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Surround with try/catch" Code Action #2727

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.VariableDeclaration;
Expand Down Expand Up @@ -93,6 +94,7 @@
import org.eclipse.jdt.internal.ui.fix.MultiFixMessages;
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.corext.refactoring.surround.SurroundWithTryCatchRefactoring;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.ASTRewriteRemoveImportsCorrectionProposal;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal;
import org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal;
Expand Down Expand Up @@ -154,6 +156,7 @@ public List<ChangeCorrectionProposal> getProposals(CodeActionParams params, IInv
getIntroduceParameterProposals(params, context, coveringNode, locations, proposals);
getExtractInterfaceProposal(params, context, proposals);
getChangeSignatureProposal(params, context, proposals);
getSurroundWithTryCatchProposal(context, proposals);
}
return proposals;
}
Expand Down Expand Up @@ -1020,4 +1023,60 @@ private boolean getChangeSignatureProposal(CodeActionParams params, IInvocationC
proposals.add(proposal);
return true;
}

private boolean getSurroundWithTryCatchProposal(IInvocationContext context, Collection<ChangeCorrectionProposal> proposals) {
if (proposals == null) {
return false;
}

if(context.getSelectionLength() <= 0) {
return false;
}

ICompilationUnit cu = context.getCompilationUnit();

CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = context.getCoveredNode();
if (selectedNode == null) {
return false;
}

while (selectedNode != null && !(selectedNode instanceof Statement) && !(selectedNode instanceof VariableDeclarationExpression) && !(selectedNode.getLocationInParent() == LambdaExpression.BODY_PROPERTY)
&& !(selectedNode instanceof MethodReference)) {
selectedNode = selectedNode.getParent();
}
if (selectedNode == null) {
return false;
}

int offset = selectedNode.getStartPosition();
int length = selectedNode.getLength();
int selectionEnd = context.getSelectionOffset() + context.getSelectionLength();

if (selectionEnd > offset + length) {
// extend the selection if more than one statement is selected (bug 72149)
length = selectionEnd - offset;
}

try {
SurroundWithTryCatchRefactoring refactoring = SurroundWithTryCatchRefactoring.create(cu, offset, length);

if (!refactoring.checkActivationBasics(astRoot).isOK()) {
return false;
}

refactoring.setLeaveDirty(true);

String label = CorrectionMessages.LocalCorrectionsSubProcessor_surroundwith_trycatch_description;
RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.SURROUND_WITH_TRY_CATCH);
proposal.setLinkedProposalModel(refactoring.getLinkedProposalModel());

proposals.add(proposal);
return true;
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public void testExtractMethodInStaticBlock() throws Exception {
//@formatter:on
Range range = new Range(new Position(4, 14), new Position(4, 32));
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
assertEquals(4, codeActions.size());
assertEquals(5, codeActions.size());
List<Either<Command, CodeAction>> extractMethod = codeActions.stream().filter((c) -> c.getRight().getTitle().equals("Extract to method")).collect(Collectors.toList());
Expected e1 = new Expected("Extract to method", expected, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD);
assertCodeActions(extractMethod, e1);
Expand Down