Implemented 'Field to block' action

This commit is contained in:
Akash Yadav
2022-04-10 21:29:37 +05:30
parent 94b84b61b7
commit 8aa676fd2b
3 changed files with 79 additions and 3 deletions
@@ -54,7 +54,7 @@ interface ActionItem {
*
* @param data The data containing various information about the event.
*/
fun postExec(data: ActionData, result: Any) = Any()
fun postExec(data: ActionData, result: Any) = Unit
fun getShowAsActionFlags(data: ActionData): Int = -1
@@ -126,8 +126,6 @@ public class DiagnosticsCodeActionProvider implements ActionProvider {
private List<Pair<String, Rewrite>> codeActionForDiagnostic(
CompilerProvider compiler, CompileTask task, Path file, @NonNull DiagnosticItem d) {
switch (d.getCode()) {
case "unused_local":
return handleUnusedLocal(task, file, d);
case "unused_field":
return handleUnusedField(task, file, d);
case "unused_class":
@@ -0,0 +1,78 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.lsp.java.actions
import com.itsaky.androidide.actions.ActionData
import com.itsaky.androidide.utils.Logger
import com.itsaky.lsp.java.JavaLanguageServer
import com.itsaky.lsp.java.rewrite.ConvertFieldToBlock
import com.itsaky.lsp.java.utils.CodeActionUtils.findPosition
import com.itsaky.lsp.models.DiagnosticItem
import java.io.File
/** @author Akash Yadav */
class FieldToBlockAction : BaseCodeAction() {
override val id: String = "lsp_java_fieldToBlock"
override var label: String = "Convert to block"
private val diagnosticCode = "unused_field"
private val log = Logger.newInstance(javaClass.simpleName)
override fun prepare(data: ActionData) {
super.prepare(data)
if (!visible) {
return
}
if (!hasRequiredData(
data, JavaLanguageServer::class.java, DiagnosticItem::class.java, File::class.java)) {
markInvisible()
return
}
val diagnostic = data.get(DiagnosticItem::class.java)!!
if (diagnosticCode != diagnostic.code) {
markInvisible()
return
}
}
override fun execAction(data: ActionData): Any {
val server = data[JavaLanguageServer::class.java]!!
val diagnostic = data[DiagnosticItem::class.java]!!
val file = requirePath(data)
return server.compiler.compile(file).get {
ConvertFieldToBlock(file, findPosition(it, diagnostic.range.start))
}
}
override fun postExec(data: ActionData, result: Any) {
if (result !is ConvertFieldToBlock) {
log.warn("Unable to convert field to block")
return
}
val server = data[JavaLanguageServer::class.java]!!
val client = server.client!!
val file = requireFile(data)
client.performCodeAction(file, result.asCodeActions(server.compiler, label))
}
}