-
-
Notifications
You must be signed in to change notification settings - Fork 52
ADFA-5046: Add Java code action: surround with try/catch #1686
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
lsp/api/src/main/java/com/itsaky/androidide/lsp/actions/SurroundWithTryCatchAction.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package com.itsaky.androidide.lsp.actions | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.drawable.Drawable | ||
| import com.itsaky.androidide.actions.ActionData | ||
| import com.itsaky.androidide.actions.ActionItem | ||
| import com.itsaky.androidide.actions.EditorActionItem | ||
| import com.itsaky.androidide.actions.hasRequiredData | ||
| import com.itsaky.androidide.actions.markInvisible | ||
| import com.itsaky.androidide.actions.requireContext | ||
| import com.itsaky.androidide.actions.requireEditor | ||
| import com.itsaky.androidide.actions.requireFile | ||
| import com.itsaky.androidide.lsp.api.ILanguageServerRegistry | ||
| import com.itsaky.androidide.lsp.models.CodeActionItem | ||
| import com.itsaky.androidide.lsp.models.CodeActionKind | ||
| import com.itsaky.androidide.lsp.models.Command | ||
| import com.itsaky.androidide.lsp.models.DocumentChange | ||
| import com.itsaky.androidide.lsp.models.TextEdit | ||
| import com.itsaky.androidide.resources.R | ||
| import org.slf4j.LoggerFactory | ||
| import java.io.File | ||
|
|
||
| class SurroundWithTryCatchAction( | ||
| lang: String, | ||
| private val targetFileExtensions: List<String>, | ||
| private val serverId: String, | ||
| private val catchClause: String, | ||
| private val catchBody: String, | ||
| tag: String, | ||
| ) : EditorActionItem { | ||
| companion object { | ||
| /** The id is per-language, since one instance is registered per language. */ | ||
| fun idFor(lang: String) = "ide.editor.lsp.$lang.surroundWithTryCatch" | ||
|
|
||
| private val logger = LoggerFactory.getLogger(SurroundWithTryCatchAction::class.java) | ||
| } | ||
|
|
||
| constructor( | ||
| lang: String, | ||
| extension: String, | ||
| serverId: String, | ||
| catchClause: String, | ||
| catchBody: String, | ||
| tag: String, | ||
| ) : this(lang, listOf(extension), serverId, catchClause, catchBody, tag) | ||
|
|
||
| override val id: String = idFor(lang) | ||
| override var label: String = "" | ||
|
|
||
| override var visible = true | ||
| override var enabled = true | ||
| override var icon: Drawable? = null | ||
| override var location: ActionItem.Location = ActionItem.Location.EDITOR_CODE_ACTIONS | ||
|
|
||
| // Reads the editor selection, so it must run on the UI thread (as CommentLineAction does). | ||
| override var requiresUIThread: Boolean = true | ||
|
|
||
| // Required, not defaulted: one instance is registered per language, and a default would let a | ||
| // new language silently inherit another language's tooltip. | ||
| override var tooltipTag: String = tag | ||
|
|
||
| override fun prepare(data: ActionData) { | ||
| super.prepare(data) | ||
|
|
||
| if (!data.hasRequiredData(Context::class.java, File::class.java)) { | ||
| markInvisible() | ||
| return | ||
| } | ||
|
|
||
| val context = data.requireContext() | ||
| label = context.getString(R.string.action_surround_with_try_catch) | ||
|
|
||
| val file = data.requireFile() | ||
| if (file.extension !in targetFileExtensions) { | ||
| markInvisible() | ||
| return | ||
| } | ||
| } | ||
|
|
||
| override suspend fun execAction(data: ActionData): List<TextEdit> { | ||
| val editor = data.requireEditor() | ||
| val cursor = editor.cursor | ||
| val (startLine, endLine) = | ||
| resolveSurroundLines( | ||
| cursor.leftLine, | ||
| cursor.leftColumn, | ||
| cursor.rightLine, | ||
| cursor.rightColumn, | ||
| ) | ||
| val edit = | ||
| computeSurroundWithTryCatchEdit( | ||
| editor.text.toString(), | ||
| startLine, | ||
| endLine, | ||
| catchClause, | ||
| catchBody, | ||
| ) ?: return emptyList() | ||
| return listOf(edit) | ||
| } | ||
|
|
||
| override fun postExec( | ||
| data: ActionData, | ||
| result: Any, | ||
| ) { | ||
| super.postExec(data, result) | ||
|
|
||
| if (result !is List<*> || result.isEmpty()) { | ||
| return | ||
| } | ||
|
|
||
| @Suppress("UNCHECKED_CAST") | ||
| val edits = result as List<TextEdit> | ||
|
|
||
| val client = | ||
| ILanguageServerRegistry.default.getServer(serverId)?.client | ||
| ?: run { | ||
| logger.warn("No language client set. Cannot complete action.") | ||
| return | ||
| } | ||
|
|
||
| val file = data.requireFile() | ||
| client.performCodeAction( | ||
| CodeActionItem( | ||
| title = label, | ||
| changes = listOf(DocumentChange(file = file.toPath(), edits = edits)), | ||
| kind = CodeActionKind.QuickFix, | ||
| command = Command.CMD_FORMAT_CODE, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.