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

Conversation

lbrayner
Copy link
Contributor

This PR adds a "Surround with try/catch" proposal to the RefactorProcessor. That way, one can surround a block of code (selection range) with a try/catch block. This is especially useful when one is guarding against RuntimeExceptions.

The code in RefactorProcessor.getSurroundWithTryCatchProposal was based on LocalCorrectionsSubProcessor.addUncaughtExceptionProposals.

@eclipse-ls-bot
Copy link

Can one of the admins verify this patch?

@lbrayner lbrayner force-pushed the surround-try-catch branch from bdc50a9 to 345bffa Compare June 26, 2023 21:35
@lbrayner
Copy link
Contributor Author

@lbrayner lbrayner force-pushed the surround-try-catch branch from 345bffa to 8b1cc80 Compare June 27, 2023 00:54
@rgrunber
Copy link
Contributor

rgrunber commented Jun 27, 2023

Would be good to compare to https://github.com/eclipse/eclipse.jdt.ls/blob/230d39431588d500f3a87162f0e50a486879b770/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java#L122 as that might be similar functionality but for the case of proposing the fix only where a thrown exception is not being caught.

@gayanper how would this PR affect redhat-developer/vscode-java#3104 (technically even that one could live server-side because a lot of snippet syntax is part of the LSP spec) ? I imagine we might be able to get rid of it ? It seems to me like code that uses the JDT model/ast would always usually be smarter than the snippet support.

@gayanper
Copy link
Contributor

@rgrunber agree and we can remove the snippet from vscode-java

@hopehadfield
Copy link
Contributor

Taking a look at this

@hopehadfield
Copy link
Contributor

LGTM.

The snippet has already been removed from vscode-java with redhat-developer/vscode-java#3253 and moved to JDT-LS with #2811. This made the "Surround with" feature much easier to use, as the user could simply highlight the text and prompt the try_catch snippet to surround with a try/catch block, rather than select it from the command palette. I think it's best to leave the snippet alongside this addition, as they are two different workflows but both equally convenient and useful.

@rgrunber rgrunber added this to the End October 2023 milestone Oct 20, 2023
Signed-off-by: lbrayner <lbrayner@users.noreply.github.com>
@rgrunber rgrunber merged commit 0e5ab02 into eclipse-jdtls:master Oct 21, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants