feat(api): access node under caret/mouse and jump (PR #1903)

* Access node under caret/mouse and jump
* Apply lint
This commit is contained in:
Yoav Sternberg
2023-06-09 17:50:57 +03:00
committed by GitHub
parent 347d6e2c2e
commit f558203a9d
6 changed files with 217 additions and 0 deletions
@@ -0,0 +1,43 @@
import jadx.api.metadata.ICodeNodeRef
import jadx.core.dex.nodes.MethodNode
val jadx = getJadxInstance()
var savedBookmark: ICodeNodeRef? = null
jadx.gui.ifAvailable {
addPopupMenuAction(
"Set bookmark",
enabled = { true },
keyBinding = "B",
action = ::setBookmark,
)
addMenuAction(
"Jump to bookmark",
action = ::jumpToBookmark,
)
}
fun setBookmark(node: ICodeNodeRef) {
val enclosing = jadx.gui.enclosingNodeUnderCaret ?: run {
jadx.log.info { "No enclosing node" }
return
}
// You can bookmark a field, method or a class
val target = if (enclosing is MethodNode) enclosing else node
jadx.log.info { "Setting bookmark to: $target" }
savedBookmark = target
}
fun jumpToBookmark() {
if (savedBookmark == null) {
jadx.log.info { "No bookmark" }
} else {
val res = jadx.gui.open(savedBookmark!!)
if (!res) {
jadx.log.info { "Failed to jump to bookmark" }
}
}
}
@@ -0,0 +1,19 @@
import jadx.api.metadata.ICodeNodeRef
val jadx = getJadxInstance()
jadx.gui.ifAvailable {
addPopupMenuAction(
"Print enclosing symbols under caret or mouse",
enabled = { true },
keyBinding = "G",
action = ::runAction,
)
}
fun runAction(node: ICodeNodeRef) {
jadx.log.info { "Node under caret: ${jadx.gui.nodeUnderCaret}" }
jadx.log.info { "Enclosing node under caret: ${jadx.gui.enclosingNodeUnderCaret}" }
jadx.log.info { "Node under mouse: ${jadx.gui.nodeUnderMouse}" }
jadx.log.info { "Enclosing Node under mouse: ${jadx.gui.enclosingNodeUnderMouse}" }
}