Skip to content

Commit

Permalink
Added code action proposal to surround selection with try/catch
Browse files Browse the repository at this point in the history
Signed-off-by: lbrayner <lbrayner@users.noreply.github.com>
  • Loading branch information
lbrayner authored and rgrunber committed Oct 20, 2023
1 parent 5cb8f7a commit b1774c6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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

0 comments on commit b1774c6

Please sign in to comment.