Added actions for finding definition and references to elements in Java source code

This commit is contained in:
Akash Yadav
2022-04-15 08:54:54 +05:30
parent dd42c5109a
commit 3602e5be5e
4 changed files with 129 additions and 0 deletions
@@ -0,0 +1,62 @@
/*
* 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.R
import io.github.rosemoe.sora.widget.CodeEditor
import java.io.File
/**
* Action that allows the user to find references to a variable, field, method or class.
*
* @author Akash Yadav
*/
class FindReferencesAction : BaseCodeAction() {
override val titleTextRes: Int = R.string.action_find_references
override val id: String = "lsp_java_findReferences"
override var label: String = "Find references"
override var requiresUIThread: Boolean = true
private val log = Logger.newInstance(javaClass.simpleName)
override fun prepare(data: ActionData) {
super.prepare(data)
if (!visible || !hasRequiredData(data, CodeEditor::class.java, File::class.java)) {
markInvisible()
return
}
}
override fun execAction(data: ActionData): Any {
val editor = data[CodeEditor::class.java]!!
return tryExecFindDefinition(editor)
}
private fun tryExecFindDefinition(editor: CodeEditor): Boolean {
return try {
val method = editor::class.java.getDeclaredMethod("findReferences")
method.isAccessible = true
method.invoke(editor)
true
} catch (error: Throwable) {
false
}
}
}
@@ -0,0 +1,63 @@
/*
* 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.R
import io.github.rosemoe.sora.widget.CodeEditor
import java.io.File
/**
* Action that allows the user to navigate to the definition of a variable, field, method, class,
* etc.
*
* @author Akash Yadav
*/
class GoToDefinitionAction : BaseCodeAction() {
override val titleTextRes: Int = R.string.action_goto_definition
override val id: String = "lsp_java_gotoDefinition"
override var label: String = "Go to definition"
override var requiresUIThread: Boolean = true
private val log = Logger.newInstance(javaClass.simpleName)
override fun prepare(data: ActionData) {
super.prepare(data)
if (!visible || !hasRequiredData(data, CodeEditor::class.java, File::class.java)) {
markInvisible()
return
}
}
override fun execAction(data: ActionData): Any {
val editor = data[CodeEditor::class.java]!!
return tryExecFindDefinition(editor)
}
private fun tryExecFindDefinition(editor: CodeEditor): Boolean {
return try {
val method = editor::class.java.getDeclaredMethod("findDefinition")
method.isAccessible = true
method.invoke(editor)
true
} catch (error: Throwable) {
false
}
}
}
@@ -24,6 +24,8 @@ class JavaCodeActionsMenu : CodeActionsMenu() {
init {
addAction(CommentAction())
addAction(UncommentAction())
addAction(GoToDefinitionAction())
addAction(FindReferencesAction())
addAction(AddImportAction())
addAction(ImplementAbstractMethodsAction())
addAction(VariableToStatementAction())
+2
View File
@@ -39,4 +39,6 @@
<string name="msg_cannot_generate_setters_getters">Unable to generate setters and getters</string>
<string name="msg_select_methods">Select methods to override</string>
<string name="msg_no_overridable_methods">Unable to find any overridable method</string>
<string name="action_goto_definition">Go to definition</string>
<string name="action_find_references">Find references</string>
</resources>